#!/bin/bash

# This script starts both the backend server and the demo server

# Start the backend server in the background
echo "Starting backend server on port 3001..."
cd "$(dirname "$0")/.."
PORT=3001 node api/server.js &
BACKEND_PID=$!

# Start the demo server
echo "Starting demo server on port 8080..."
echo "Open http://localhost:8080 in your browser"
cd "$(dirname "$0")"
python3 -m http.server 8080 &
DEMO_PID=$!

echo ""
echo "Servers started successfully!"
echo "Backend server: http://localhost:3001"
echo "Demo page: http://localhost:8080"
echo ""
echo "Press Ctrl+C to stop both servers"

# Function to kill both servers on exit
function cleanup {
  echo ""
  echo "Stopping servers..."
  kill $BACKEND_PID
  kill $DEMO_PID
  echo "Servers stopped"
  exit 0
}

# Register the cleanup function for when the script is terminated
trap cleanup SIGINT

# Wait indefinitely
while true; do
  sleep 1
done