import sys
PAIRS = [
("""That move only takes effect after one full daemon restart; until then your
node keeps serving pages exactly as before.""",
"""That move only takes effect after one full daemon restart; until then the
node keeps serving pages exactly as before."""),
("""  daemon's worker, and adapter updates, install automatically with no command
  from you; anything that would interrupt running sessions is offered for your
  consent instead and left untouched until you apply it. Choose which kinds
  apply unattended with the daemon's `auto_classes` setting. The old""",
"""  daemon's worker, and adapter updates, install automatically with no command
  needed; anything that would interrupt running sessions is offered for
  consent instead and left untouched until it is applied. The daemon's
  `auto_classes` setting chooses which kinds apply unattended. The old"""),
("""  bundled adapters. Applying the
  update installs any of them you do not have""", None),
]
# Pairs below are keyed on exact line-wrapped text from 8d402d6b.
PAIRS = PAIRS[:2] + [
("""  update installs any of them you do not have, and upgrades them when the
  bundled copy is newer. A newer copy you already have is never downgraded,
  and an adapter you removed stays removed. Bundled adapters show""",
"""  update installs any of them that are missing, and upgrades them when the
  bundled copy is newer. A newer installed copy is never downgraded, and a
  removed adapter stays removed. Bundled adapters show"""),
("""- Adapters travel between machines on your subnet. `spt adapter add <name>
  --via-subnet` (or `-vs`) installs an adapter another node already has, and
  `spt adapter update` asks your peers for a newer copy before going to the
  internet.""",
"""- Adapters travel between machines on a subnet. `spt adapter add <name>
  --via-subnet` (or `-vs`) installs an adapter another node already has, and
  `spt adapter update` asks subnet peers for a newer copy before going to the
  internet."""),
("""- `spt update source list | pin | unpin` shows which nodes have served you
  updates and lets you prefer one.""",
"""- `spt update source list | pin | unpin` shows which nodes have served
  updates and pins a preferred one."""),
("""- `spt update status` shows what each part of your install is at, and says so""",
"""- `spt update status` shows the version of each part of the install, and says so"""),
("""  their versions and profiles, and ends with "Available on subnet": adapters
  your peers have that you do not.""",
"""  their versions and profiles, and ends with "Available on subnet": adapters
  subnet peers have that this node does not."""),
]

def section(t):
    a = t.index("## [0.73.0]")
    b = t.index("\n## [", a + 5)
    return a, b

for path in sys.argv[1:]:
    raw = open(path, "rb").read()
    crlf = b"\r\n" in raw
    t = raw.decode("utf-8").replace("\r\n", "\n")
    a, b = section(t)
    sec = t[a:b]
    for old, new in PAIRS:
        n = sec.count(old)
        if n != 1:
            sys.exit(f"REFUSE {path}: match count {n} for {old[:60]!r}")
        sec = sec.replace(old, new)
    t2 = t[:a] + sec + t[b:]
    assert t2[:a] == t[:a] and t2[a+len(sec):] == t[b:]
    import re
    left = re.findall(r"\b(?:you|your)\b", sec, re.I)
    print(f"{path}: crlf={crlf} you/your left in section={len(left)}")
    if crlf:
        t2 = t2.replace("\n", "\r\n")
    open(path, "wb").write(t2.encode("utf-8"))
