#!/bin/bash
set -euo pipefail

# Convenience wrapper: build locally + deploy to plum
# Usage:
#   ./deploy-to-plum-dist-from-src              # build + deploy with default server URL
#   ./deploy-to-plum-dist-from-src --skip-app   # skip Swift app rebuild (just deploy)
#   ./deploy-to-plum-dist-from-src --uninstall  # uninstall from plum
#   ./deploy-to-plum-dist-from-src --frontend   # re-sync frontend only

SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"

# Parse args
SKIP_APP=false
for arg in "$@"; do
    case "$arg" in
        --skip-app) SKIP_APP=true ;;
        --uninstall|--frontend|*) break ;;
    esac
done

# If not skipping app build, build server locally first
if [[ "$SKIP_APP" == "false" ]]; then
    echo "Building server..."
    (cd "$SCRIPT_DIR/src/server" && bun install --frozen-lockfile >/dev/null 2>&1)
fi

# Delegate to deploy-remote.sh
"$SCRIPT_DIR/deploy/deploy-remote.sh" "$@"
