#!/usr/bin/env bash
# Wrap dist/DataTools.app into a no-install portable .zip.
#
# Usage:
#   bash build/macos/build_zip.sh <version>
#
# Why a portable .zip in addition to the .dmg:
#   * Buyers who don't want an installer can unzip and double-click the
#     .app directly — no drag-to-/Applications step, no installer
#     chrome. Self-contained: the .app holds Python + every dep.
#   * IT-locked-down machines often block .dmg auto-mount but allow
#     .zip download + extraction.
#
# Run after ``pyinstaller build/datatools.spec --clean --noconfirm``
# has produced ``dist/DataTools.app``. Output goes to
# ``dist/DataTools-<version>-mac-portable.zip``.
#
# Tesseract bundling: no-op here. The bundled Tesseract binary +
# dylibs + tessdata are already inside DataTools.app/Contents/Resources/tesseract/
# (placed by PyInstaller's BUNDLE/datas mechanism). ``ditto -c -k``
# preserves the whole .app tree.

set -euo pipefail

VERSION="${1:-0.0.0-dev}"
APP="dist/DataTools.app"
ZIP="dist/DataTools-${VERSION}-mac-portable.zip"

if [[ ! -d "$APP" ]]; then
  echo "Error: $APP not found. Run pyinstaller build/datatools.spec first." >&2
  exit 1
fi

# ``ditto`` preserves the .app bundle's extended attributes and
# resource forks (a plain ``zip`` strips them and can break code
# signatures + Info.plist resolution on the buyer's machine).
#
# --sequesterRsrc keeps the AppleDouble metadata inside the archive
# rather than as parallel ._ files on disk after extraction.
rm -f "$ZIP"
ditto -c -k --sequesterRsrc --keepParent "$APP" "$ZIP"

echo "Built $ZIP ($(du -h "$ZIP" | cut -f1))"
