#!/usr/bin/env -S python3 -I
"""A stand-in for the datasets repository's scripts/promote (flyloft's tier one): the real one's need of HF_TOKEN,
its guard that the record is on origin/main as it is here, and its last line; there is no Hub to tag, so the record's
commit stands for the tag."""

import os
import subprocess
import sys
from pathlib import Path

sys.path.insert(0, str(Path(__file__).resolve().parent))
import check  # noqa: E402


def main() -> int:
    if len(sys.argv) != 3:
        print("usage: scripts/promote <name> <version>", file=sys.stderr)
        return 2
    name, version = sys.argv[1:]
    if not os.environ.get("HF_TOKEN"):
        print("HF_TOKEN is not set", file=sys.stderr)
        return 1
    rel = f"results/{name}/{version}.txt"
    merged = subprocess.run(["git", "show", f"origin/main:{rel}"], cwd=check.ROOT, capture_output=True)
    here = check.ROOT / rel
    if merged.returncode or not here.is_file() or merged.stdout != here.read_bytes():
        print(f"{rel} is not on origin/main; merge its pull request and pull first, in a full clone", file=sys.stderr)
        return 1
    rec = check.parse_record(here)
    print(f"promoted: {check.hub_org()}/{name} v{version} at {rec['commit']}")
    return 0


if __name__ == "__main__":
    sys.exit(main())
