#!/usr/bin/env python3
"""E26 standalone independent settlement verifier (spec §14).

    python3 tools/e26/e26_verify.py <bundle_dir_or_proof.json>

Zero CAIN imports; runs the clean-room verifier from verification/e26/cleanroom/verifier.py.
"""
from __future__ import annotations

import importlib.util
import json
import os
import sys

_HERE = os.path.dirname(os.path.abspath(__file__))
_REPO = os.path.dirname(os.path.dirname(_HERE))
_VERIFIER = os.path.join(_REPO, "verification", "e26", "cleanroom", "verifier.py")


def _load(path: str):
    spec = importlib.util.spec_from_file_location("e26_cleanroom_verifier", path)
    mod = importlib.util.module_from_spec(spec); spec.loader.exec_module(mod); return mod


def main(argv: list) -> int:
    if len(argv) < 2:
        print("usage: e26_verify.py <bundle_dir_or_proof.json>", file=sys.stderr); return 2
    target = argv[1]
    proof_path = target if target.endswith(".json") else os.path.join(target, "E26_SETTLEMENT_PROOF.json")
    if not os.path.exists(proof_path):
        print(f"proof not found: {proof_path}", file=sys.stderr); return 2
    problems = _load(_VERIFIER).verify(json.load(open(proof_path, encoding="utf-8")))
    if problems:
        print("E26 VERIFICATION FAILED:")
        for p in problems:
            print("  -", p)
        return 1
    print("E26 SETTLEMENT PROOF VERIFIED (independent reconstruction, zero CAIN imports)")
    return 0


if __name__ == "__main__":
    raise SystemExit(main(sys.argv))
