#!/bin/bash

# Define where our temporary node shim lives
SHIM_DIR="/tmp/antigravity_bin"
export PATH="$SHIM_DIR:$PATH"

# Create shim if node is missing but bun exists
if ! command -v node &> /dev/null && command -v bun &> /dev/null; then
    mkdir -p "$SHIM_DIR"
    echo '#!/bin/sh' > "$SHIM_DIR/node"
    echo 'exec bun "$@"' >> "$SHIM_DIR/node"
    chmod +x "$SHIM_DIR/node"
fi

# Kill any existing go server on port 36969
lsof -ti :36969 | xargs kill -9 2>/dev/null

echo "🚀 Starting Antigravity (Go Edition)..."

# Start Go Backend
echo "📦 Building Backend..."
go run backend/main.go &
GO_PID=$!

# Start Frontend
echo "✨ Starting Frontend..."
# Check for pnpm or fallback
if command -v pnpm &> /dev/null; then
    pnpm run dev
else
    # Fallback if pnpm is also missing from PATH but bun is there
    bun run dev
fi

# Cleanup on exit
kill $GO_PID
