#!/bin/bash
# Start the Aumentum Browser API Server (run from project root or scripts/)

SCRIPT_DIR="$(cd "$(dirname "$0")" && pwd)"
# Project root: parent of scripts/
if [ "$(basename "$SCRIPT_DIR")" = "scripts" ]; then
  PROJECT_ROOT="$(cd "$SCRIPT_DIR/.." && pwd)"
else
  PROJECT_ROOT="$SCRIPT_DIR"
fi
cd "$PROJECT_ROOT"

# Kill any existing process on port 8001
echo "🔧 Checking for existing server on port 8001..."
lsof -ti:8001 | xargs kill -9 2>/dev/null && echo "✅ Cleared port 8001" || echo "✅ Port 8001 is available"

# Start the API server
echo ""
echo "🚀 Starting Aumentum Browser API Server..."
echo "📡 API will be available at: http://localhost:8001"
echo "📚 Interactive docs at: http://localhost:8001/docs"
echo ""
LOG_FILE="${PROJECT_ROOT}/logs/api.log"
LOG_ERR="${PROJECT_ROOT}/logs/api-error.log"
mkdir -p "$(dirname "$LOG_FILE")"
nohup venv/bin/python3 aumentum_api.py >> "$LOG_FILE" 2>> "$LOG_ERR" &
echo "✅ Server started in background (logs: $LOG_FILE and $LOG_ERR)"

