#!/usr/bin/env bash
# Wrap dist/DataTools.app into a distributable .dmg.
#
# Usage:
#   bash build/macos/build_dmg.sh <version>
#
# Run after ``pyinstaller build/datatools.spec --clean --noconfirm``
# has produced ``dist/DataTools.app``. The output DMG goes to
# ``dist/DataTools-<version>-mac.dmg``.
#
# Code signing + notarization happen separately (see build/README.md
# "Signing"). This script only handles the packaging step.
#
# Tesseract bundling: no-op here. The .app already contains
# Contents/Resources/tesseract/{tesseract, *.dylib, tessdata/} thanks
# to PyInstaller's BUNDLE() carrying the spec's datas through. This
# script just wraps the finished .app — no extra steps for OCR.

set -euo pipefail

VERSION="${1:-0.0.0-dev}"
APP="dist/DataTools.app"
DMG="dist/DataTools-${VERSION}-mac.dmg"

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

# Drag-target convenience: a /Applications symlink inside the DMG so
# the buyer can drag the app icon to it without leaving the DMG.
STAGE="$(mktemp -d)"
trap 'rm -rf "$STAGE"' EXIT

cp -R "$APP" "$STAGE/"
ln -s /Applications "$STAGE/Applications"

# UDZO = compressed read-only DMG, the standard distribution format.
hdiutil create \
  -volname "DataTools" \
  -srcfolder "$STAGE" \
  -ov \
  -format UDZO \
  "$DMG"

echo "Built $DMG"
