#!/bin/bash
#
# install.sh - Script d'installation de SSH Launcher
# Installe l'extension GNOME Shell, l'application GTK et le handler SSH
#

set -e

# Couleurs pour l'affichage
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
NC='\033[0m' # No Color

# Répertoire du projet
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
PROJECT_DIR="$(dirname "$SCRIPT_DIR")"

echo -e "${GREEN}=== Installation de SSH Launcher ===${NC}"
echo "Répertoire du projet: $PROJECT_DIR"
echo ""

# Vérifier les dépendances
echo -e "${YELLOW}Vérification des dépendances...${NC}"

check_dependency() {
    if ! command -v "$1" &> /dev/null; then
        echo -e "${RED}Erreur: $1 n'est pas installé${NC}"
        echo "Installez-le avec: $2"
        exit 1
    fi
    echo -e "  ✓ $1"
}

check_python_module() {
    if ! python3 -c "import $1" 2>/dev/null; then
        echo -e "${RED}Erreur: module Python '$1' non trouvé${NC}"
        echo "Installez-le avec: $2"
        exit 1
    fi
    echo -e "  ✓ Python module: $1"
}

check_dependency "python3" "sudo apt install python3"
check_dependency "gnome-shell" "Installez GNOME Shell"

check_python_module "gi" "sudo apt install python3-gi"
check_python_module "yaml" "sudo apt install python3-yaml"

# Vérifier GTK3 et WebKit2
python3 -c "import gi; gi.require_version('Gtk', '3.0')" 2>/dev/null || {
    echo -e "${RED}Erreur: GTK3 non disponible${NC}"
    echo "Installez avec: sudo apt install gir1.2-gtk-3.0"
    exit 1
}
echo -e "  ✓ GTK3"

python3 -c "import gi; gi.require_version('WebKit2', '4.1')" 2>/dev/null || {
    echo -e "${RED}Erreur: WebKit2 4.1 non disponible${NC}"
    echo "Installez avec: sudo apt install gir1.2-webkit2-4.1"
    exit 1
}
echo -e "  ✓ WebKit2 4.1"

echo ""

# Rendre les scripts exécutables
echo -e "${YELLOW}Configuration des permissions...${NC}"
chmod +x "$PROJECT_DIR/scripts/ssh-url"
chmod +x "$PROJECT_DIR/app/ssh-launcher-gtk.py"
echo -e "  ✓ Scripts rendus exécutables"

# Mettre à jour les chemins dans les fichiers
echo -e "${YELLOW}Configuration des chemins...${NC}"

# Mettre à jour extension.js avec le bon chemin
sed -i "s|const APP_PATH = .*|const APP_PATH = GLib.build_filenamev(['$PROJECT_DIR', 'app', 'ssh-launcher-gtk.py']);|" \
    "$PROJECT_DIR/extension/extension.js"
echo -e "  ✓ extension.js mis à jour"

# Mettre à jour ssh-url.desktop
sed -i "s|Exec=.*|Exec=$PROJECT_DIR/scripts/ssh-url %u|" \
    "$PROJECT_DIR/desktop/ssh-url.desktop"
echo -e "  ✓ ssh-url.desktop mis à jour"

# Mettre à jour ssh-launcher-gtk.desktop
sed -i "s|Exec=.*|Exec=python3 $PROJECT_DIR/app/ssh-launcher-gtk.py|" \
    "$PROJECT_DIR/app/ssh-launcher-gtk.desktop"
echo -e "  ✓ ssh-launcher-gtk.desktop mis à jour"

echo ""

# Installer le handler SSH
echo -e "${YELLOW}Installation du handler SSH...${NC}"
cp "$PROJECT_DIR/desktop/ssh-url.desktop" ~/.local/share/applications/
xdg-mime default ssh-url.desktop x-scheme-handler/ssh
echo -e "  ✓ Handler ssh:// installé"

# Installer l'application dans les applications locales
echo -e "${YELLOW}Installation de l'application...${NC}"
cp "$PROJECT_DIR/app/ssh-launcher-gtk.desktop" ~/.local/share/applications/
echo -e "  ✓ Application ajoutée au menu"

# Mettre à jour la base de données des applications
update-desktop-database ~/.local/share/applications/ 2>/dev/null || true
echo -e "  ✓ Base de données mise à jour"

echo ""

# Installer l'extension GNOME Shell
echo -e "${YELLOW}Installation de l'extension GNOME Shell...${NC}"
EXTENSION_DIR="$HOME/.local/share/gnome-shell/extensions/ssh-launcher@local"

mkdir -p "$EXTENSION_DIR"
cp "$PROJECT_DIR/extension/metadata.json" "$EXTENSION_DIR/"
cp "$PROJECT_DIR/extension/extension.js" "$EXTENSION_DIR/"
cp "$PROJECT_DIR/extension/stylesheet.css" "$EXTENSION_DIR/"
echo -e "  ✓ Extension copiée dans $EXTENSION_DIR"

echo ""
echo -e "${GREEN}=== Installation terminée ===${NC}"
echo ""
echo -e "${YELLOW}Prochaines étapes :${NC}"
echo "1. Redémarrez GNOME Shell (Alt+F2, tapez 'r', Entrée) ou déconnectez-vous"
echo "2. Activez l'extension avec:"
echo "   gnome-extensions enable ssh-launcher@local"
echo ""
echo "3. Ou activez via GNOME Extensions / Extension Manager"
echo ""
echo -e "${YELLOW}Test immédiat de l'application :${NC}"
echo "   python3 $PROJECT_DIR/app/ssh-launcher-gtk.py"
echo ""
echo -e "${YELLOW}Configuration :${NC}"
echo "   Éditez $PROJECT_DIR/config/equipements.yaml"
echo ""
