#!/usr/bin/env bash
# Installe hermes-voice comme service utilisateur systemd
# Lance automatiquement au démarrage de session, écoute "Ok Hermes, ..."

set -e

SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
VENV_PYTHON="$SCRIPT_DIR/.venv/bin/python"
SERVICE_DIR="$HOME/.config/systemd/user"
SERVICE_FILE="$SERVICE_DIR/hermes-voice.service"

# Vérifications
if [ ! -f "$VENV_PYTHON" ]; then
  echo "❌ venv introuvable — lancez d'abord :"
  echo "   python3 -m venv .venv && source .venv/bin/activate && pip install -r requirements.txt"
  exit 1
fi

mkdir -p "$SERVICE_DIR"

cat > "$SERVICE_FILE" << EOF
[Unit]
Description=Hermes Voice Client
After=network-online.target
Wants=network-online.target

[Service]
ExecStart=$VENV_PYTHON $SCRIPT_DIR/hermes-voice.py
WorkingDirectory=$SCRIPT_DIR
Restart=on-failure
RestartSec=10s
Environment=PYTHONUNBUFFERED=1

StandardOutput=journal
StandardError=journal
SyslogIdentifier=hermes-voice

[Install]
WantedBy=default.target
EOF

systemctl --user daemon-reload
systemctl --user enable hermes-voice
systemctl --user start hermes-voice

echo ""
echo "✓ Service installé et démarré"
echo ""
echo "Commandes utiles :"
echo "  systemctl --user status hermes-voice"
echo "  journalctl --user -u hermes-voice -f"
echo "  systemctl --user stop hermes-voice"
echo "  systemctl --user restart hermes-voice"
