#!/bin/bash

# Create a minimal test script
echo "Starting RealtimeCursor test..."

# Build the SDK
echo "📦 Building the SDK..."
cd fixed-sdk
npm install
bash manual-build.sh
cd ..

# Start the server
echo "🚀 Starting the server..."
cd api
npm install
node server.js &
SERVER_PID=$!
cd ..

# Wait for the server to start
echo "⏳ Waiting for the server to start..."
sleep 2

# Open the HTML test in a browser
echo "🌐 Opening HTML test in browser..."
cd html-test
python3 -m http.server 8000 &
HTTP_PID=$!
cd ..

# Function to handle script termination
cleanup() {
  echo "🛑 Stopping all processes..."
  kill $SERVER_PID $HTTP_PID
  exit 0
}

# Register the cleanup function for script termination
trap cleanup SIGINT SIGTERM

echo "✅ All services started!"
echo "📝 HTML test: http://localhost:8000"
echo "📡 Server: http://localhost:3001"
echo "Press Ctrl+C to stop all services"

# Keep the script running
wait