#!/bin/zsh
# Build ClaudeHUD.app — a double-clickable Liquid-Glass menu-bar app that
# runs and visualizes the Claude voice engine.
set -e
cd "$(dirname "$0")"

APP="ClaudeHUD.app"
BIN="$APP/Contents/MacOS/ClaudeHUD"

echo "compiling ClaudeHUD (SwiftUI + AppKit, target macOS 26)..."
mkdir -p "$APP/Contents/MacOS" "$APP/Contents/Resources"
swiftc -O -target arm64-apple-macosx26.0 -parse-as-library \
  -framework SwiftUI -framework AppKit -framework Combine -framework ApplicationServices \
  -framework Network -framework AVFoundation -framework WebKit \
  -o "$BIN" ClaudeHUD.swift WindowManager.swift GestureController.swift

# app icon (build it if missing)
if [ ! -f Claude.icns ] && command -v iconutil >/dev/null; then
  swiftc -O -o /tmp/genicon genicon.swift -framework AppKit && /tmp/genicon /tmp
  iconutil -c icns /tmp/Claude.iconset -o Claude.icns
fi
[ -f Claude.icns ] && cp Claude.icns "$APP/Contents/Resources/Claude.icns"

cat > "$APP/Contents/Info.plist" <<'PLIST'
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
  <key>CFBundleName</key><string>Jarvis</string>
  <key>CFBundleDisplayName</key><string>Jarvis</string>
  <key>CFBundleIdentifier</key><string>com.ahmed.jarvis</string>
  <key>CFBundleVersion</key><string>1.0</string>
  <key>CFBundleShortVersionString</key><string>1.0</string>
  <key>CFBundlePackageType</key><string>APPL</string>
  <key>CFBundleExecutable</key><string>ClaudeHUD</string>
  <key>CFBundleIconFile</key><string>Claude</string>
  <key>LSMinimumSystemVersion</key><string>26.0</string>
  <key>LSUIElement</key><true/>
  <key>NSMicrophoneUsageDescription</key>
  <string>Claude listens to your voice so you can talk to it hands-free.</string>
  <key>NSCameraUsageDescription</key>
  <string>Jarvis uses the camera for hand-gesture control.</string>
  <key>NSHighResolutionCapable</key><true/>
</dict>
</plist>
PLIST

# Sign with the persistent self-signed identity "Jarvis Code Signing" if it
# exists — its cert leaf is baked into the designated requirement, so the
# app's TCC identity (Accessibility, Microphone, Screen Recording) stays
# STABLE across rebuilds and grants never need re-approving. Falls back to
# ad-hoc if the identity is missing (grants would then reset each build).
SIGN_ID="Jarvis Code Signing"
if security find-identity -p codesigning 2>/dev/null | grep -q "$SIGN_ID"; then
  SIGN_ARG="$SIGN_ID"
  echo "signing with persistent identity: $SIGN_ID"
else
  SIGN_ARG="-"
  echo "WARNING: '$SIGN_ID' identity not found — ad-hoc signing (grants reset per build)"
fi
codesign --force --deep --sign "$SIGN_ARG" \
  --entitlements /dev/stdin "$APP" <<'ENT' 2>/dev/null || codesign --force --deep --sign "$SIGN_ARG" "$APP"
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0"><dict>
  <key>com.apple.security.device.audio-input</key><true/>
  <key>com.apple.security.device.camera</key><true/>
</dict></plist>
ENT

echo "built $APP"
