def load(p):
    raw = open(p, encoding='utf-8', newline='').read()
    return raw.replace('\r\n', '\n'), '\r\n' in raw

def save(p, s, crlf):
    open(p, 'w', encoding='utf-8', newline='').write(s.replace('\n', '\r\n') if crlf else s)

def once(s, old, new):
    assert s.count(old) == 1, (old[:80], s.count(old))
    return s.replace(old, new)

p = 'docs/MANIFEST.md'
s, c = load(p)
old = '''valid. The `changelog` segment of the adapter facet is core's: an adapter's own
`web/` file named `changelog` is not reachable at that exact path.
'''
s = once(s, old, old + '''
<!-- [doc->REQ-DOCS-ADAPTER-PAGE] -->
**The adapter page.** Every installed adapter also has a page at
`http://localhost:5474/<node>/a/<adapter>/about`, whether or not it declares
either key, and the docs sidebar's *Installed on \\<node\\>* part links it. The
header carries the update facts: `v<version> · installed <HH:MMAM YYYY-MM-DD> ·
via <built-in|subnet|release>`, the same clock the update divulge prints. Then
come the `summary`, the install-time changelog page (embedded as-is, never
re-rendered) and a link to the adapter's hosted docs when it declares
`docs_dir`. Whatever is not declared is named on one dim line. The *via* clause
comes from the retained archive's record and is written on every install and
update. An adapter installed from a `<path>` or `--github`, and any adapter
installed before the record carried it, names no source. The page groups the
adapter as the sidebar does: **Gateway** when `hostable_types` contains
`"Gateway"` (even alongside agent types, and listed once), **Shell** when
`kind = "shell"`, otherwise **Harness**. Like `changelog`, the `about` segment
of the adapter facet is core's.
''')
save(p, s, c)

p = 'docs-site/src/harness-contract/manifest.md'
s, c = load(p)
s = once(s, '| `hostable_types` | no | Endpoint types this adapter can host |',
         '| `hostable_types` | no | Endpoint types this adapter can host, spelled exactly (`"LiveAgent"`, `"ReadyAgent"`, `"Worker"`, `"Gateway"`, …). An adapter hosting `"Gateway"` is listed as a Gateway adapter in the docs sidebar and its adapter page. <!-- [doc->REQ-DOCS-ADAPTER-PAGE] --> |')
save(p, s, c)

p = 'CONTEXT.md'
s, c = load(p)
s = once(s, 'Each installed adapter has a page there: its update facts',
         'Each installed adapter has a page there (`/<node>/a/<adapter>/about`, a template over its records, rendered per request with no markdown rendering) <!-- [doc->REQ-DOCS-ADAPTER-PAGE] -->: its update facts')
save(p, s, c)
print('ok')
