#!/usr/bin/env python3
"""
Create a minimal release packet under release/packet/ suitable for sharing.

This does not modify sources; it copies a curated subset and writes a RELEASE_README.

Usage:
  python tools/make_release_packet.py
"""

from __future__ import annotations
import os, shutil, argparse

DOCS = [
  "Abstract.md",
  "RU_Engine_Overview.md",
  "Referee_Guide.md",
  "Artifacts.md",
  "Claims.md",
  "Scope.md",
  "Glossary.md",
  "Literature.md",
  "Submission_Checklist.md",
  "References.md",
  "OG3_ToricCode_NoDoeblin.md",
  "Self_Check.md",
  "Global_Report_Fibonacci_Alpha.md",
  "StringTheory_Index.md",
]

TOOLS = [
  "ci_selfcheck.py",
  "run_all_examples.py",
  "ru_bridge_extractor.py",
  "validate_certificates.py",
  "build_artifact_index.py",
  "build_fibonacci_alpha_chambers_report.py",
  "REPRODUCIBILITY.md",
]

def main()->int:
    ap=argparse.ArgumentParser()
    ap.add_argument("--root", default=".", help="Project root")
    args=ap.parse_args()
    root=os.path.abspath(args.root)

    packet=os.path.join(root,"release","packet")
    if os.path.exists(packet):
        shutil.rmtree(packet)
    os.makedirs(packet, exist_ok=True)

    # Copy docs
    docs_in=os.path.join(root,"docs")
    docs_out=os.path.join(packet,"docs")
    os.makedirs(docs_out, exist_ok=True)
    for fn in DOCS:
        src=os.path.join(docs_in, fn)
        if os.path.exists(src):
            shutil.copy2(src, os.path.join(docs_out, fn))

    # Copy key sources
    src_out=os.path.join(packet,"sources")
    os.makedirs(src_out, exist_ok=True)
    for sub in ["ru","ru_bridge","host_data","theorem_submission_v19","context"]:
        s=os.path.join(root,"sources",sub)
        if os.path.exists(s):
            shutil.copytree(s, os.path.join(src_out,sub), dirs_exist_ok=True)

    # Copy tools
    tools_in=os.path.join(root,"tools")
    tools_out=os.path.join(packet,"tools")
    os.makedirs(tools_out, exist_ok=True)
    for fn in TOOLS:
        src=os.path.join(tools_in, fn)
        if os.path.exists(src):
            shutil.copy2(src, os.path.join(tools_out, fn))

    readme=os.path.join(packet,"RELEASE_README.txt")
    with open(readme,"w",encoding="utf-8") as f:
        f.write("Release packet\n\n")
        f.write("Start here:\n")
        f.write("- docs/Abstract.md\n")
        f.write("- docs/RU_Engine_Overview.md\n")
        f.write("- docs/Referee_Guide.md\n\n")
        f.write("Self-check:\n")
        f.write("- python tools/ci_selfcheck.py\n\n")
        f.write("Artifact index:\n")
        f.write("- python tools/build_artifact_index.py\n\n")
        f.write("Global report:\n")
        f.write("- python tools/build_fibonacci_alpha_chambers_report.py\n")
    print("Wrote:", os.path.relpath(packet, root))
    return 0

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