#!/usr/bin/env python3
import sys
src, dst = sys.argv[1], sys.argv[2]
with open(src, "r", encoding="utf-8") as f:
c = f.read()
if "V131-BROKEN-COUNT" in c:
print("ALREADY", file=sys.stderr)
sys.exit(0)
old = """ if (countEl) countEl.textContent = filtered.length + ' / ' + items.length + ' tuiles' + (pinned.size ? ' (' + pinned.size + ' pin' + (pinned.size>1?'s':'') + ')' : '');"""
new = """ /* V131-BROKEN-COUNT: aggregate status from registry */
const brokenCount = items.filter(e => e.http_status && e.http_status >= 400).length;
if (countEl) {
const brokenSuffix = brokenCount > 0
? ' ● ' + brokenCount + ' broken'
: ' ● all OK';
countEl.innerHTML = filtered.length + ' / ' + items.length + ' tuiles' + (pinned.size ? ' (' + pinned.size + ' pin' + (pinned.size>1?'s':'') + ')' : '') + brokenSuffix;
}"""
if old not in c:
print("anchor missing", file=sys.stderr)
sys.exit(1)
c = c.replace(old, new, 1)
with open(dst, "w", encoding="utf-8") as f:
f.write(c)
print(f"OK size={len(c)}", file=sys.stderr)