#!/bin/zsh
set -euo pipefail
cd "${0:A:h:h}"
scripts/swift.sh build --disable-sandbox --product pose-review
scripts/swift.sh build --disable-sandbox --product pose-batch
review_bin="$(scripts/swift.sh build --disable-sandbox --show-bin-path)"
review_bundle="$PWD/.build/Joint Review.app"
mkdir -p "$review_bundle/Contents/MacOS" "$review_bundle/Contents/Resources"
cp "$review_bin/pose-review" "$review_bundle/Contents/MacOS/pose-review"
cp "$review_bin/pose-batch" "$review_bundle/Contents/MacOS/pose-batch"
python3 - "$review_bundle" "$PWD" <<'PY'
import json, pathlib, plistlib, sys
bundle, root = map(pathlib.Path, sys.argv[1:])
with (bundle/'Contents/Info.plist').open('wb') as f:
    plistlib.dump(dict(CFBundleExecutable='pose-review', CFBundleIdentifier='local.jameslopez.joint-review',
                      CFBundleName='Joint Review', CFBundleDisplayName='Joint Review', CFBundlePackageType='APPL',
                      CFBundleVersion='1', CFBundleShortVersionString='0.1', NSHighResolutionCapable=True,
                      NSPrincipalClass='NSApplication', LSMinimumSystemVersion='14.0'), f)
(bundle/'Contents/Resources/data-directory.txt').write_text(str(root/'data'))
presets = [dict(title='2026 larger screenshots',
                manifest=str(root/'data/2026-09-07-HarrisHillCCW-better/session.json'),
                results=str(root/'artifacts/better-feasibility-selected/results.json')),
           dict(title='2025 original JPEGs',
                manifest=str(root/'data/2025-09-01-HarrisHillCCW-highres/session.json'),
                results=str(root/'artifacts/highres-feasibility/results.json')),
           dict(title='2026 original comparison panels',
                manifest=str(root/'data/2026-09-07-HarrisHillCCW/session.json'),
                results=str(root/'artifacts/initial-feasibility/results.json'))]
(bundle/'Contents/Resources/launch.json').write_text(json.dumps([p for p in presets if pathlib.Path(p['manifest']).exists() and pathlib.Path(p['results']).exists()]))
PY
codesign --force --sign - "$review_bundle"
print -r -- "$review_bundle"
