#!/usr/bin/env python3
"""Regression: autopatch logging is per-launcher/user writable.

The watcher is run with a definitely-absent launcher PID, so it writes its
startup/ownership-exit diagnostics and terminates without touching FIFA.
"""
import os
import pathlib
import subprocess
import sys
import tempfile


def main():
    script = pathlib.Path(__file__).with_name("autopatch.py")
    with tempfile.TemporaryDirectory(prefix="openfut-autopatch-test-") as root:
        log_path = pathlib.Path(root) / "autopatch.log"
        env = os.environ.copy()
        env["OPENFUT_AUTOPATCH_LOG"] = str(log_path)
        result = subprocess.run(
            [sys.executable, str(script), "--launcher-pid", "999999999"],
            env=env,
            text=True,
            capture_output=True,
            timeout=5,
        )
        assert result.returncode == 0, result.stderr or result.stdout
        assert log_path.is_file(), "OPENFUT_AUTOPATCH_LOG was ignored"
        text = log_path.read_text()
        assert "watching for FIFA17.exe" in text
        assert "launcher pid 999999999 exited" in text
    print("autopatch writable-log override: PASS")
    return 0


if __name__ == "__main__":
    raise SystemExit(main())
