#!/usr/bin/env python3
"""Standalone verifier (stdlib only). Recomputes hashes and the rank from raw files."""
import hashlib, json, sys
from pathlib import Path
here = Path(__file__).resolve().parent
def sha(p):
    h = hashlib.sha256(); h.update(p.read_bytes()); return h.hexdigest()
man = json.loads((here/"MANIFEST.json").read_text())
bad = [f for f, d in man["files"].items() if sha(here/f) != d]
live = json.loads((here/"rank-live.json").read_text())
lb = json.loads((here/"rank-live.json").read_text())
rank = 1 + sum(1 for c in live["competitors"] if not c["is_cain"] and c["resolved"] > live["run"]["resolved"])
ok = (not bad) and rank == live["rank"]["rank"] and live["run"]["n"] == 500
print(f"files={len(man['files'])} bad={len(bad)} rank_recomputed={rank} rank_feed={live['rank']['rank']}")
print("VERIFIED" if ok else "FAILED")
sys.exit(0 if ok else 1)
