#!/bin/bash
# Builds AlienHUD.app — a release binary wrapped in a proper bundle so macOS
# can grant it Calendar permission and remember the answer.
set -euo pipefail

cd "$(dirname "$0")/.."
ROOT="$PWD"
APP="$ROOT/build/Alien Reminder by Incuv8or.app"

echo "==> Compiling (release)"
swift build -c release

echo "==> Assembling bundle"
rm -rf "$APP"
mkdir -p "$APP/Contents/MacOS" "$APP/Contents/Resources"
cp "$ROOT/.build/release/AlienHUD" "$APP/Contents/MacOS/AlienHUD"
if [ -f "$ROOT/Resources/AppIcon.icns" ]; then
  cp "$ROOT/Resources/AppIcon.icns" "$APP/Contents/Resources/AppIcon.icns"
fi
cp "$ROOT/Resources/Info.plist"    "$APP/Contents/Info.plist"

# Ad-hoc signature. macOS ties Calendar permission to the code identity, so
# without this the permission prompt reappears on every rebuild.
echo "==> Signing (ad-hoc)"
codesign --force --sign - --timestamp=none "$APP" >/dev/null

echo
echo "Built: $APP"
echo "Run it:      open \"$APP\""
echo "Watch logs:  log stream --predicate 'eventMessage CONTAINS \"AlienHUD\"'"
