#!/bin/bash
# ============================================================
#  FWS — Sélecteur d'environnement de bureau (LIVE uniquement)
#
#  Le paquet calamares (AUR) ne compile PAS le module packagechooser :
#  on choisit donc le bureau ICI, dans le live, AVANT de lancer Calamares.
#  Le choix est écrit dans /run/fws-desktop, lu ensuite par :
#    - le module Calamares fws-desktop-choice (validation),
#    - shellprocess.conf → /usr/local/bin/fws-desktop-init-<bureau>.
# ============================================================
set -u

STATE=/run/fws-desktop
DEFAULT=xfce
VALID_RE='^(xfce|kde|gnome|hyprland)$'

choice=""

# Dialogue graphique si zenity + X sont disponibles ; sinon repli silencieux.
if command -v zenity >/dev/null 2>&1 && [ -n "${DISPLAY:-}" ]; then
    choice="$(zenity --list --radiolist \
        --title="FWS — Environnement de bureau" \
        --text="Choisissez le bureau à installer :" \
        --width=560 --height=360 \
        --column="Choix" --column="id" --column="Bureau" \
        --hide-column=2 --print-column=2 \
        TRUE  xfce     "Xfce — léger, stable et rapide (recommandé)" \
        FALSE kde      "KDE Plasma — moderne et très personnalisable" \
        FALSE gnome    "GNOME — épuré et intuitif" \
        FALSE hyprland "Hyprland — compositeur Wayland dynamique" \
        2>/dev/null)"
fi

# Normalisation : toute valeur vide / annulée / invalide → bureau par défaut.
choice="$(printf '%s' "$choice" | tr -d '[:space:]')"
if ! printf '%s' "$choice" | grep -Eq "$VALID_RE"; then
    choice="$DEFAULT"
fi

printf '%s' "$choice" > "$STATE"
exit 0
