from pathlib import Path
path = Path('/etc/caddy/Caddyfile')
text = path.read_text()
old = '''headless-pm.tap365.org {
    encode zstd gzip

    @api path /api/* /health /status
    handle @api {
        reverse_proxy 127.0.0.1:18069
    }

    handle {
        reverse_proxy 127.0.0.1:13001
    }
}
'''
new = '''headless-pm.tap365.org {
    encode zstd gzip

    @api path /api/* /health /status
    handle @api {
        reverse_proxy 127.0.0.1:18069 {
            header_up X-API-Key 52IazidZgCdVM5TzhhALvPkZPn3G71YFa_kSvW7d1JU
        }
    }

    handle {
        reverse_proxy 127.0.0.1:13001
    }
}
'''
if old not in text:
    raise SystemExit('headless-pm block mismatch, stop patching')
path.write_text(text.replace(old, new, 1))
print('patched')
