#!/bin/zsh
#
# ./run - convenient dev command for mac-sync
#
# Builds the web UI (so localhost:8765 gets fresh assets)
# then builds + launches the Mac agent.
#
# Usage:
#   ./run                 # full build + launch
#   ./run web             # only rebuild web UI (then hard-refresh 8765)
#   ./run swift           # only rebuild the Swift app
#
# Before running:
#   - Quit any existing "Mac Sync" menu bar agent first.
#   - This launches the dev binary from .build/debug/, which
#     serves http://localhost:8765 using the just-built web/dist/.

set -e

cd "$(dirname "$0")"

cmd=${1:-all}

case "$cmd" in
  web)
    echo "→ Building web UI only (skipping typecheck for quick release)..."
    (cd web && bunx vite build)
    echo "✅ web/dist/ updated with latest assets."
    echo "   Hard-refresh http://localhost:8765 (Cmd+Shift+R) or restart the agent to pick up new dist/."
    ;;

  swift)
    echo "→ Building MacSyncApp (Swift) only..."
    swift build --product MacSyncApp
    echo "✅ .build/debug/MacSyncApp updated."
    ;;

  all|*)
    echo "→ Building web UI (skipping typecheck for quick release)..."
    (cd web && bunx vite build)

    echo "→ Building MacSyncApp..."
    swift build --product MacSyncApp

    echo ""
    echo "→ Launching dev MacSyncApp"
    echo "   (serves http://localhost:8765 using the fresh web/dist/)"
    echo "   Quit any previous 'Mac Sync' agent first if it's still running."
    echo ""

    exec ./.build/debug/MacSyncApp
    ;;
esac
