#!/bin/bash

# Start script for Beacon Tracker Web Viewer
# This script checks dependencies and starts the Flask server

echo "==================================="
echo "Beacon Tracker Web Viewer"
echo "==================================="
echo ""

# Check if Python 3 is installed
if ! command -v python3 &> /dev/null; then
    echo "ERROR: Python 3 is not installed"
    exit 1
fi

# Check if Flask is installed
if ! python3 -c "import flask" &> /dev/null; then
    echo "Flask is not installed. Installing dependencies..."
    pip install -r requirements.txt
fi

# Check if posix_ipc is installed
if ! python3 -c "import posix_ipc" &> /dev/null; then
    echo "posix_ipc is not installed. Installing dependencies..."
    pip install -r requirements.txt
fi

echo "✓ All dependencies installed"
echo ""

# Check if shared memory exists
if python3 -c "
import posix_ipc
try:
    shm = posix_ipc.SharedMemory('BeaconFrameBuffer')
    shm.close_fd()
    print('✓ Shared memory segment found')
except:
    print('⚠ WARNING: Shared memory segment not found')
    print('  Make sure beacon_track is running with EnableFrameBuffer=true')
"
then
    echo ""
fi

echo "Starting Flask server..."
echo "Open http://localhost:5000 in your browser"
echo ""
echo "Press Ctrl+C to stop"
echo ""

# Start the Flask app
python3 app.py
