#!/bin/bash
# ================================================================
#  Marine Compass Display — Launcher para Linux / Raspberry Pi
# ================================================================
#
#  Uso:
#    ./launch.sh              → hardware (lee config.py)
#    ./launch.sh --sim        → simulador de maniobras
#    ./launch.sh --kiosk      → kiosk: fullscreen, sin cursor
#    ./launch.sh --sim --kiosk
#    ./launch.sh --port /dev/ttyUSB0 --baud 4800
#
#  Auto-arranque en RPi — agregar en /etc/rc.local antes de 'exit 0':
#    su -c "DISPLAY=:0 /home/pi/compass/launch.sh --kiosk &" pi
# ================================================================

set -e
cd "$(dirname "$0")"

echo "╔══════════════════════════════════════════════╗"
echo "║        MARINE COMPASS DISPLAY v1.0           ║"
echo "╠══════════════════════════════════════════════╣"
echo "║  Uso:                                        ║"
echo "║    ./launch.sh           → hardware          ║"
echo "║    ./launch.sh --sim     → simulador         ║"
echo "║    ./launch.sh --kiosk   → kiosk RPi         ║"
echo "╚══════════════════════════════════════════════╝"
echo

# Verificar Python 3
if ! command -v python3 &>/dev/null; then
    echo "[ERROR] python3 no encontrado."
    echo "Instala: sudo apt install python3 python3-pip"
    exit 1
fi

# Dependencias
echo "Verificando dependencias..."
pip3 install -r requirements.txt -q
echo "Dependencias OK."
echo

# Display (necesario en rc.local / cron)
export DISPLAY="${DISPLAY:-:0}"

# Kiosk: deshabilitar screensaver
for arg in "$@"; do
    if [ "$arg" = "--kiosk" ]; then
        xset s off    2>/dev/null || true
        xset -dpms    2>/dev/null || true
        xset s noblank 2>/dev/null || true
        command -v unclutter &>/dev/null && unclutter -idle 0 &
        break
    fi
done

# Mostrar modo
if [[ " $* " == *" --sim "* ]] || [[ "$1" == "--sim" ]]; then
    echo "Modo: SIMULADOR"
    echo "  INITIAL 60s → TURN STBD → 225° → TURN PORT → 045° → NORMAL"
else
    echo "Modo: HARDWARE — leyendo puertos en config.py"
fi
echo

python3 main.py "$@"
