#!/bin/bash

source ./install_apps.sh

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

run_step() {
    local description="$1"
    local command="$2"

    echo -n "$description... "
    eval "$command" > /dev/null 2>>"$LOGFILE"
    if [ $? -eq 0 ]; then
        echo "✅"
    else
        echo "❌ (voir $LOGFILE)"
    fi
}

#################################
# 🎯 POST-INSTALL DEBIAN 13     #
#################################

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
"

# 🧰 BASE TOOLS
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 "🛒 Ajout du dépôt Flathub" "
flatpak remote-add --if-not-exists flathub https://flathub.org/repo/flathub.flatpakrepo
"

# 🌐 DNS
run_step "🌐 Configuration 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
"

# 🎨 LIBREOFFICE
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/
"

# 🦊 FIREFOX
run_step "🦊 Installation de Firefox (script)" "
bash firefox.sh
"

# 🖥️ GNOME
if whiptail --title "Configuration GNOME" --yesno "Souhaitez-vous configurer GNOME maintenant ?" 10 60; then
    run_step "🖥️ Configuration de GNOME (script)" "
    bash gnome.sh
    "
else
    echo "⏭️ Configuration GNOME ignorée."
fi


##############################
## SELECTION DES LOGICIELS ##
##############################

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)

# Nettoyage des guillemets doubles de la sortie de whiptail
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



echo -e "\n🎉 Post-installation terminée."
echo "📄 Rapport des erreurs (s'il y en a) : $LOGFILE"
