import os
import re
from pathlib import Path

ROOT = Path(__file__).resolve().parents[1]
PAGE_EXTS = {".php", ".html", ".htm"}

RE = re.compile(r"(\.pdf)\.php\b", re.IGNORECASE)

def main():
    changed = 0
    files = 0
    for dirpath, dirnames, filenames in os.walk(ROOT):
        dirnames[:] = [d for d in dirnames if d not in {"_next", "__website-next", "cdn-cgi", "node_modules"}]
        for fn in filenames:
            p = Path(dirpath) / fn
            if p.suffix.lower() not in PAGE_EXTS:
                continue
            try:
                data = p.read_text(encoding="utf-8", errors="ignore")
            except Exception:
                continue
            files += 1
            new = RE.sub(r"\1", data)
            if new != data:
                p.write_text(new, encoding="utf-8")
                changed += 1
    print(f"Scanned files: {files}")
    print(f"Updated files: {changed}")

if __name__ == "__main__":
    main()

