#!/bin/bash
set -euo pipefail

# ==========================================================
#  Debian 13 Toolbox — Menu principal de post-installation
# ==========================================================


LOGFILE="$HOME/postinstall.log"
> "$LOGFILE"

# --- Fonction d’exécution avec log
run_step() {
    local description="$1"
    local command="$2"

    echo -n "$description... "
    if eval "$command" >>"$LOGFILE" 2>&1; then
        echo "✅"
    else
        echo "❌ (voir $LOGFILE)"
    fi
}

# --- Vérification dépendances minimales
for cmd in whiptail flatpak extrepo wget curl git; do
    if ! command -v "$cmd" &>/dev/null; then
        echo "📦 Installation de la dépendance manquante : $cmd"
        sudo apt update && sudo apt install -y "$cmd"
    fi
done


# ==========================================================
#  FONCTIONS DE CHAQUE ACTION
# ==========================================================

post_install_base() {
    echo "🚀 Post-installation de base..."

    run_step "📦 Configuration des sources APT" "
    sudo cp files/debian.sources /etc/apt/sources.list.d/ &&
    if [ -f /etc/apt/sources.list ]; then
        sudo cp /etc/apt/sources.list /etc/apt/sources.list.backup
        sudo rm /etc/apt/sources.list
    fi
    "

    run_step "🆙 Mise à jour du système" "
    sudo apt update &&
    sudo apt upgrade -y
    "

    run_step "🔧 Installation des outils de base" "
    sudo apt install -y libgettextpo-dev gettext git wget curl build-essential linux-image-amd64 make ttf-mscorefonts-installer rsyslog unzip bash-completion flatpak libxdo3 extrepo
    "

    run_step "🇫🇷 Langue et clavier français" "
    sudo cp files/keyboard /etc/default/
    echo 'LANG=fr_FR.UTF-8' | sudo tee -a /etc/default/locale > /dev/null
    "

    run_step "🛒 Ajout du dépôt Flathub" "
    flatpak remote-add --if-not-exists flathub https://flathub.org/repo/flathub.flatpakrepo
    "

    run_step "🌐 DNS Cloudflare" "
    echo -e '[main]\ndns=none' | sudo tee /etc/NetworkManager/conf.d/90-dns-none.conf &&
    sudo systemctl reload NetworkManager &&
    sudo sed -i '1,4 s/^/#/' /etc/resolv.conf &&
    sudo sed -i -e '\$anameserver 1.1.1.2\nameserver 2606:4700:4700::1112' /etc/resolv.conf &&
    sudo systemctl reload NetworkManager
    "

    run_step "🎨 Icônes LibreOffice (Colibre)" "
    sudo chown $USER:$USER files/registrymodifications.xcu &&
    mkdir -p \$HOME/.config/libreoffice/4/user/ &&
    cp files/registrymodifications.xcu \$HOME/.config/libreoffice/4/user/ &&
    sudo mkdir -p /etc/skel/.config/libreoffice/4/user/ &&
    sudo cp files/registrymodifications.xcu /etc/skel/.config/libreoffice/4/user/
    "

    run_step "🦊 Installation de Firefox" "
    bash firefox.sh
    "
}


configure_gnome() {
    echo "🖥️ Configuration de GNOME..."
    run_step "Configuration de GNOME (script externe)" "
    bash gnome.sh
    "
}

# --- Fichiers annexes
source ./install_apps.sh


install_apps_menu() {
    CHOIX=$(whiptail --title "Installation de logiciels" --checklist \
    "Choisissez les logiciels à installer :" 20 78 10 \
    "freetube"   "FreeTube (YouTube sans pub)"          ON \
    "zoom"       "Zoom (visioconférence)"               OFF \
    "element"    "Element (messagerie Matrix)"          OFF \
    "chrome"     "Google Chrome (navigateur)"           OFF \
    "vscodium"   "VSCodium (éditeur de code)"           ON \
    3>&1 1>&2 2>&3)

    CHOIX=$(echo $CHOIX | tr -d '"')

    for APP in $CHOIX; do
        case $APP in
            freetube)  install_freetube ;;
            zoom)      install_zoom ;;
            element)   install_element ;;
            chrome)    install_chrome ;;
            vscodium)  install_vscodium ;;
            *)         echo "❓ Logiciel inconnu : $APP" ;;
        esac
    done
}

# ==========================================================
#  MENU PRINCIPAL (BOUCLE)
# ==========================================================
while true; do
    CHOIX=$(whiptail --title "🧰 GN-Debian 13 Toolbox" --menu \
    "Sélectionnez une action :" 20 70 10 \
    1 "Post-installation de base" \
    2 "Personnalisation de GNOME" \
    3 "Installer des applications" \
    4 "Quitter" \
    3>&1 1>&2 2>&3)

    case $CHOIX in
        1) post_install_base ;;
        2) configure_gnome ;;
        3) install_apps_menu ;;
        4) echo "👋 Au revoir !"; exit 0 ;;
        *) echo "❓ Choix invalide." ;;
    esac

    whiptail --title "Retour au menu principal" --msgbox "✅ Action terminée.\nAppuyez sur OK pour revenir au menu principal." 8 60
done
