"""Publish a fresh exact BSD source offer; older versions never change."""
from pathlib import Path
import json,hashlib,shutil
root=Path.cwd();out=root/'third_party/mammoth/1.13.0-text2'
if out.exists():raise RuntimeError('Never overwrite a retained vendor offer')
installed=(root/'node_modules/mammoth').resolve();previous=root/'third_party/mammoth/1.13.0-text1';old=json.loads((previous/'manifest.json').read_text())
pin=lambda b:dict(bytes=len(b),sha256=hashlib.sha256(b).hexdigest())
out.mkdir(parents=True);shutil.copytree(previous/'upstream',out/'upstream')
archive=(out/'upstream/mammoth-1.13.0.tgz').read_bytes()
assert pin(archive)['sha256']==old['upstream']['sha256']
assert (installed/'LICENSE').read_bytes()==(previous/'LICENSE.txt').read_bytes()
records={}
for name in ['lib/raw-text.js','lib/docx/body-reader.js','mammoth.browser.js']:
    data=(installed/name).read_bytes();target=out/'sources'/name;target.parent.mkdir(parents=True,exist_ok=True);target.write_bytes(data);records[name]=pin(data)
patch=(root/'patches/mammoth@1.13.0.patch').read_bytes();(out/'sources/mammoth@1.13.0.patch').write_bytes(patch)
shutil.copy2(installed/'LICENSE',out/'LICENSE.txt');shutil.copy2(installed/'package.json',out/'sources/upstream-package.json')
(out/'sources/build').mkdir();shutil.copy2(Path(__file__),out/'sources/build/package-mammoth-notes.py')
(out/'NOTICE.txt').write_text('''Mammoth1.13.0 with Tabmorph referenced-note and line-break fixes2.
Original BSD-2-Clause license and upstream npm archive retained unchanged.
The patch changes Node raw-text extraction and the actual unminified browser
entry point together; prior w:cr/line-break fixes remain. Referenced footnotes
and endnotes gain matching markers and definitions after body text, in first
reference order, once per note. Each type is renumbered separately; custom
source labels/restarts/number formats are not retained. Unreferenced notes are
omitted. Missing or nested references and more than2,000 referenced notes fail
explicitly. Note-free raw text and HTML/Markdown extraction retain prior behavior.
Source headers/footers/comments/fonts/printed layout remain outside this profile.
No new dependency or GPL/AGPL addition. No uploads, accounts, ads/payments or
engine CSP change. This is a finite fidelity repair, not a competitive parity claim.
''')
(out/'sources/BUILD.txt').write_text('''Use the retained original mammoth1.13.0 npm archive and original BSD license.
Apply sources/mammoth@1.13.0.patch to the extracted package root. Exactly the
three installed entry files in manifest.json must match. Complete patched Node
sources and browser entry bytes are retained under sources/. The unused upstream
minified browser companion remains original and is not the application import.
Repository package.json/pnpm-lock.yaml pin the patch. Use corepack pnpm10.28.0
install --frozen-lockfile with Node22; global pnpm9 uses incompatible patch state.
scripts/copy-mammoth-vendor.mjs verifies installed source/patch/license and every
retained offer before Vite bundles mammoth/mammoth.browser. Older text1 stays
immutable and served. Rebuild this new offer with scripts/package-mammoth-notes.py
from the matching installed patched package in a fresh output directory. Current
Office54/PDF43/MOBI22 offers retain application/build/locks/patch sources. Actual
local and production fixture/reader/privacy evidence is separately required.
''')
(out/'SOURCE.html').write_text('<!doctype html><meta charset="utf-8"><title>Mammoth referenced-note source</title><h1>Mammoth1.13.0 note and line-break patch2</h1><p><a href="LICENSE.txt">Original BSD license</a> · <a href="NOTICE.txt">Scope and limitations</a> · <a href="sources/BUILD.txt">Rebuild</a> · <a href="manifest.json">Complete hashes</a> · <a href="sources/mammoth@1.13.0.patch">Exact patch</a> · <a href="upstream/mammoth-1.13.0.tgz">Original distribution</a></p>')
manifest=dict(version=out.name,baseVersion='1.13.0',license='BSD-2-Clause',upstream=old['upstream'],patch=dict(path='patches/mammoth@1.13.0.patch',**pin(patch)),installedFiles=records,licensePin=pin((installed/'LICENSE').read_bytes()),files={str(p.relative_to(out)):pin(p.read_bytes()) for p in sorted(out.rglob('*')) if p.is_file()})
(out/'manifest.json').write_text(json.dumps(manifest,indent=2)+'\n');print(json.dumps(dict(version=out.name,files=len(manifest['files']),installedFiles=records)))
