from pathlib import Path
from datetime import datetime
path = Path("/etc/caddy/Caddyfile")
text = path.read_text()
marker = "law-servce.tap365.org {\n    root * /srv/law-servce.tap365.org\n    encode zstd gzip\n"
block = """\n    # Claude for Legal ZH static landing page\n    redir /claude-for-legal-zh /claude-for-legal-zh/\n    handle_path /claude-for-legal-zh/* {\n        root * /srv/claude-for-legal-zh-site\n        try_files {path} /index.html\n        file_server\n    }\n"""
if "/claude-for-legal-zh" in text:
    print("already-configured")
else:
    if marker not in text:
        raise SystemExit("marker not found; refuse to edit Caddyfile")
    backup = path.with_name("Caddyfile.backup.claude-for-legal-zh-" + datetime.now().strftime("%Y%m%d_%H%M%S"))
    backup.write_text(text)
    path.write_text(text.replace(marker, marker + block, 1))
    print(f"updated backup={backup}")
