#!/bin/bash

# AIPT Laravel Development Server Startup Script
# This script ensures the server runs with proper memory configuration

echo "Starting AIPT Laravel Development Server..."

# Kill any existing server processes
echo "Stopping any existing server processes..."
pkill -f "php artisan serve" 2>/dev/null || true

# Kill any processes using port 8000
echo "Freeing up port 8000..."
lsof -ti:8000 | xargs kill -9 2>/dev/null || true

# Wait for processes to stop
sleep 3

# Check if port is free
if lsof -i:8000 >/dev/null 2>&1; then
    echo "ERROR: Port 8000 is still in use. Please manually kill the process and try again."
    exit 1
fi

# Start the server with custom PHP configuration
echo "Starting server with optimized memory settings..."
php -d memory_limit=2048M -d max_execution_time=600 -c php.ini artisan serve --host=127.0.0.1 --port=8000

echo "Server started successfully!"
