#!/usr/bin/env python3
"""Create a portable reading release; original artifacts remain unchanged."""
from pathlib import Path
import hashlib,json,zipfile
ROOT=Path(__file__).resolve().parent.parent
inventory=json.loads((ROOT/'deliverables/490d_Release_Inventory_C1431.json').read_text())
payloads={};bindings=[]
prefix='sandbox:/workspace/scratch/1b40da62dcbd/c1132_c1431/deliverables/'
for item in inventory['artifacts']:
    original=(ROOT/item['path']).read_bytes()
    assert hashlib.sha256(original).hexdigest()==item['sha256']
    name=Path(item['path']).name;data=original
    if name.endswith('.md'):data=original.decode().replace(prefix,'').encode()
    assert name not in payloads;payloads[name]=data
    bindings.append({'name':name,'original_path':item['path'],'original_sha256':item['sha256'],'packaged_sha256':hashlib.sha256(data).hexdigest(),'bytes':len(data),'link_projection':data!=original})
payloads['README.md']=("""# Chronological families — reader release

Start with 490d_Chronological_Families_Explanation_C1431_Print.pdf (14 pages).
The technical companion is 490d_Technical_Companion_C1431_Print.pdf (7 pages).
490d_Start_Here_C1431.md links to the editable readers, map and supporting references.

Markdown links have been made local to this folder. The PDFs, images and JSON files are unchanged.
SOURCE_TO_PACKAGE.json records original and packaged hashes.
The separate full evidence archive contains the frozen source texts, proof scripts, sequential journal and verifier.
The reader bundle is a convenient presentation package; detailed indexes refer to that full evidence archive.

Latest File52c governs the Rounded/inverse branch. The inverse of the inverse is deferred.
""").encode()
payloads['SOURCE_TO_PACKAGE.json']=(json.dumps(bindings,indent=2)+'\n').encode()
payloads['MANIFEST.sha256']=''.join(hashlib.sha256(data).hexdigest()+'  '+name+'\n' for name,data in sorted(payloads.items())).encode()
out=ROOT/'deliverables/490d_Chronological_Families_Reader_Package_C1431.zip'
assert not out.exists(),'Reader package is immutable; create a new version rather than overwrite.'
with zipfile.ZipFile(out,'w',zipfile.ZIP_DEFLATED) as z:
    for name,data in sorted(payloads.items()):
        info=zipfile.ZipInfo(name,date_time=(2026,9,28,0,0,0));info.compress_type=zipfile.ZIP_DEFLATED
        z.writestr(info,data)
print(json.dumps({'path':str(out),'sha256':hashlib.sha256(out.read_bytes()).hexdigest(),'bytes':out.stat().st_size,'entries':len(payloads)}))
