#!/bin/bash
# =====================================================================
# prepara.sh - installa quello che serve per registrare le audioguide
#
# Nulla di tutto questo sta nel repository: sono prerequisiti della macchina,
# non del progetto. Chi non registra guide non ha bisogno di installarli.
#
# Uso:
#   ./prepara.sh              installa cio' che manca
#   ./prepara.sh --controlla  dice soltanto cosa manca, non installa nulla
# =====================================================================

DIR="$(cd "$(dirname "$0")" && pwd)"
SOLO_CONTROLLO=0
[ "$1" = "--controlla" ] && SOLO_CONTROLLO=1
[ "$1" = "-h" ] || [ "$1" = "--help" ] && { sed -n '2,11p' "$0" | sed 's/^# \{0,1\}//'; exit 0; }

ok()   { echo "  [ok]   $*"; }
manca(){ echo "  [--]   $*"; }
err()  { echo "  [KO]   $*" >&2; }

MANCANTI=0

echo "=== Prerequisiti per le audioguide ==="

# --- Node e Playwright --------------------------------------------------
if command -v node >/dev/null; then
    ok "node $(node --version)"
else
    err "node non trovato: installarlo dal gestore di pacchetti (serve la versione 18 o successiva)"
    MANCANTI=1
fi

if [ -d "$DIR/node_modules/playwright" ]; then
    ok "playwright installato in $DIR/node_modules"
else
    manca "playwright"
    if [ "$SOLO_CONTROLLO" = "0" ]; then
        echo "         installazione…"
        (cd "$DIR" && npm install --silent) && ok "playwright installato" || { err "installazione fallita"; MANCANTI=1; }
    else
        MANCANTI=1
    fi
fi

# Il browser e' un download a parte (circa 150 MB), tenuto nella cache dell'utente
# e non in node_modules: va chiesto esplicitamente anche quando la libreria c'e' gia'.
if ls "$HOME/.cache/ms-playwright"/chromium-* >/dev/null 2>&1; then
    ok "browser chromium presente nella cache di playwright"
else
    manca "browser chromium"
    if [ "$SOLO_CONTROLLO" = "0" ]; then
        echo "         scaricamento (circa 150 MB)…"
        (cd "$DIR" && npx --yes playwright install chromium) && ok "chromium scaricato" || { err "scaricamento fallito"; MANCANTI=1; }
    else
        MANCANTI=1
    fi
fi

# --- ffmpeg -------------------------------------------------------------
if command -v ffmpeg >/dev/null && command -v ffprobe >/dev/null; then
    ok "ffmpeg $(ffmpeg -version | head -1 | awk '{print $3}')"
else
    manca "ffmpeg (serve per montare la voce sul video)"
    if [ "$SOLO_CONTROLLO" = "0" ]; then
        if command -v dnf >/dev/null;      then dnf install -y -q ffmpeg-free
        elif command -v apt-get >/dev/null; then apt-get install -y -qq ffmpeg
        else err "gestore di pacchetti non riconosciuto: installare ffmpeg a mano"; fi
        command -v ffmpeg >/dev/null && ok "ffmpeg installato" || MANCANTI=1
    else
        MANCANTI=1
    fi
fi

# --- Piper (voce) -------------------------------------------------------
if python3 -c "import piper" 2>/dev/null; then
    ok "piper installato"
else
    manca "piper (sintesi vocale)"
    if [ "$SOLO_CONTROLLO" = "0" ]; then
        pip3 install -q piper-tts && ok "piper installato" || { err "installazione fallita"; MANCANTI=1; }
    else
        MANCANTI=1
    fi
fi

# --- modello vocale -----------------------------------------------------
if ls "$DIR/voci"/*.onnx >/dev/null 2>&1; then
    ok "modello vocale presente ($(ls "$DIR/voci"/*.onnx | head -1 | xargs basename))"
else
    manca "modello vocale italiano (61 MB, non e' in git)"
    if [ "$SOLO_CONTROLLO" = "0" ]; then
        "$DIR/scarica_voce.sh" || MANCANTI=1
    else
        MANCANTI=1
    fi
fi

echo
if [ "$MANCANTI" = "0" ]; then
    echo "  Tutto pronto. Per registrare una guida: vedi README.md"
else
    if [ "$SOLO_CONTROLLO" = "1" ]; then
        echo "  Manca qualcosa: rilanciare senza --controlla per installarlo."
    else
        echo "  Qualcosa non e' stato installato: vedi i messaggi qui sopra."
    fi
fi
exit $MANCANTI
