#!/usr/bin/env bash
# build.sh — compile ArenaNative (release) and wrap it into a double-clickable
# .app bundle at build/ArenaNative.app. Ad-hoc signed.
#
# ./build.sh build build/ArenaNative.app
# ./build.sh --dock build, then pin the app to the Dock
#
# SwiftPM builds the binary; we assemble the bundle by hand (the icon lives at
# Resources/AppIcon.icns — regenerate with make-icon.sh).
set -euo pipefail
cd "$(dirname "$0")"
APP_NAME="ArenaNative"
APP="build/$APP_NAME.app"
echo "› swift build -c release"
swift build -c release
echo "› assembling $APP"
rm -rf "$APP"
mkdir -p "$APP/Contents/MacOS" "$APP/Contents/Resources"
cp ".build/release/$APP_NAME" "$APP/Contents/MacOS/$APP_NAME"
[ -f Resources/AppIcon.icns ] && cp Resources/AppIcon.icns "$APP/Contents/Resources/AppIcon.icns"
cat > "$APP/Contents/Info.plist" <<'PLIST'
CFBundleNameArenaNative
CFBundleDisplayNamearena — native
CFBundleExecutableArenaNative
CFBundleIdentifiercomputer.aesthetic.arena-native
CFBundleVersion1
CFBundleShortVersionString0.1
CFBundlePackageTypeAPPL
CFBundleIconFileAppIcon
LSMinimumSystemVersion12.0
NSHighResolutionCapable
NSPrincipalClassNSApplication
PLIST
codesign --force --sign - "$APP" >/dev/null 2>&1 || true
echo "✓ built $APP"
if [[ "${1:-}" == "--dock" ]]; then
ABS="$(cd "$(dirname "$APP")" && pwd)/$APP_NAME.app"
echo "› pinning to Dock: $ABS"
defaults write com.apple.dock persistent-apps -array-add \
"tile-datafile-data_CFURLStringfile://$ABS_CFURLStringType15"
killall Dock
echo "✓ pinned"
fi