#!/usr/bin/env python3
"""Patch Hermes to use one explicitly mounted, lock-protected auth store."""

from __future__ import annotations

import argparse
from pathlib import Path


BEFORE = '''def _auth_file_path() -> Path:
    path = get_hermes_home() / "auth.json"
'''
AFTER = '''def _auth_file_path() -> Path:
    configured = os.environ.get("HERMES_AUTH_FILE", "").strip()
    path = Path(configured) if configured else get_hermes_home() / "auth.json"
'''


def patch(source: Path, destination: Path) -> None:
    """Apply the narrow environment override and fail on upstream drift."""
    content = source.read_text(encoding="utf-8")
    if BEFORE not in content:
        raise RuntimeError("Hermes auth patch context changed")
    destination.parent.mkdir(parents=True, exist_ok=True)
    destination.write_text(content.replace(BEFORE, AFTER, 1), encoding="utf-8")


def main() -> int:
    parser = argparse.ArgumentParser(description=__doc__)
    parser.add_argument("source", type=Path)
    parser.add_argument("destination", type=Path)
    args = parser.parse_args()
    patch(args.source, args.destination)
    return 0


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