import re, sys
root = r'C:\Users\decid\Documents\projects\spt-core\.worktrees\w6-divulge' + '\\'

def patch(rel, pairs):
    p = root + rel
    b = open(p, 'rb').read()
    crlf = b'\r\n' in b
    s = b.decode().replace('\r\n', '\n')
    for a, n in pairs:
        c = s.count(a)
        if c != 1:
            sys.exit(f'{rel}: {c} hits for {a!r}')
        s = s.replace(a, n)
    if crlf:
        s = s.replace('\n', '\r\n')
    open(p, 'wb').write(s.encode())

# A lost continuation: "<text>" + run of spaces joined in one line. Restore "\\\n<indent>".
def cont(before, after, indent):
    return (before + ' ' * 0, before)  # placeholder, unused

man = 'crates/spt-runtime/src/manifest.rs'
patch(man, [
    ('adapter.summary is {n} characters                  (at most',
     'adapter.summary is {n} characters \\\n                 (at most'),
    ('adapter.summary is one plain paragraph                  and must not',
     'adapter.summary is one plain paragraph \\\n                 and must not'),
    ('adapter.changelog must not contain a                          parent-directory',
     'adapter.changelog must not contain a \\\n                         parent-directory'),
    ('adapter.changelog is archive-relative                          and must not',
     'adapter.changelog is archive-relative \\\n                         and must not'),
    ('adapter.changelog must name a                  markdown file',
     'adapter.changelog must name a \\\n                 markdown file'),
])

reg = 'crates/spt-runtime/src/registry.rs'
patch(reg, [
    ('declares changelog                  {path:?} but it {why} — the install is refused so the adapter\'s changelog                  page never serves broken. Ship the file inside the adapter archive, or drop                  [adapter].changelog."',
     'declares changelog \\\n                 {path:?} but it {why} — the install is refused so the adapter\'s changelog \\\n                 page never serves broken. Ship the file inside the adapter archive, or drop \\\n                 [adapter].changelog."'),
    ('"<!DOCTYPE html>\n<html><head><meta charset=\\"utf-8\\"><title>{title} changelog</title>         </head><body>\n{body}</body></html>\n"',
     '"<!DOCTYPE html>\\n<html><head><meta charset=\\"utf-8\\"><title>{title} changelog</title>\\\n         </head><body>\\n{body}</body></html>\\n"'),
    ('HARNESS_WITH_CHANGELOG.replace("changelog = \\"docs/CHANGELOG.md\\"\n", "")',
     'HARNESS_WITH_CHANGELOG.replace("changelog = \\"docs/CHANGELOG.md\\"\\n", "")'),
])

web = 'crates/spt-daemon/src/webserve.rs'
patch(web, [
    ('format!("NOT_FOUND: facet a adapter {adapter} changelog\n")',
     'format!("NOT_FOUND: facet a adapter {adapter} changelog\\n")'),
])

ns = 'crates/spt/src/api/nowsignal.rs'
patch(ns, [
    ('Some("Reload required:\n  run /reload-plugins.")',
     'Some("Reload required:\\n  run /reload-plugins.")'),
    ('09:58AM 2026-09-24 —              changelog: http://localhost:5474/hfenduleam/a/claude-spt/changelog —              Reload required: run /reload-plugins."',
     '09:58AM 2026-09-24 — \\\n             changelog: http://localhost:5474/hfenduleam/a/claude-spt/changelog — \\\n             Reload required: run /reload-plugins."'),
    ('04:58PM 2026-09-24 —              changelog: http://localhost:5474/n/docs/changelog.html"',
     '04:58PM 2026-09-24 — \\\n             changelog: http://localhost:5474/n/docs/changelog.html"'),
    ('Some(" \n "));', 'Some(" \\n "));'),
])
print('ok')
