#!/bin/bash
set -euo pipefail

. /usr/local/bin/fws-desktop-init-common

first_user="$(fws_first_user)"

# Config Hyprland minimale + autostart (agent polkit, barre, applet réseau,
# notifications). Sans config ni agent polkit, la session est inutilisable
# pour un non-technicien (pas de raccourcis, pas d'élévation de privilèges GUI).
fws_write_hyprland_config() {
    local user_name="$1"
    [ -n "$user_name" ] && [ -d "/home/$user_name" ] || return 0
    local cfg_dir="/home/$user_name/.config/hypr"
    install -d -m 755 "$cfg_dir"
    if [ -f /usr/share/hypr/hyprland.conf ]; then
        cp -f /usr/share/hypr/hyprland.conf "$cfg_dir/hyprland.conf"
    else
        cat > "$cfg_dir/hyprland.conf" <<'EOF'
$terminal = kitty
$menu = wofi --show drun
bind = SUPER, Q, exec, $terminal
bind = SUPER, R, exec, $menu
bind = SUPER, C, killactive
bind = SUPER, M, exit
EOF
    fi
    cat >> "$cfg_dir/hyprland.conf" <<'EOF'

# ── FWS : démarrage automatique de la session ──
exec-once = systemctl --user start hyprpolkitagent
exec-once = waybar
exec-once = nm-applet --indicator
exec-once = mako
EOF
    chown -R "$user_name:$user_name" "/home/$user_name/.config"
}

# PipeWire/applet NM/polices viennent de FWS_BASE_PACKAGES ; hyprpolkitagent
# fournit l'agent d'authentification polkit pour Wayland/Hyprland.
fws_install_packages "${FWS_BASE_PACKAGES[@]}" \
    hyprland xdg-desktop-portal-hyprland xdg-desktop-portal-gtk \
    waybar wofi kitty mako swaybg swayidle swaylock \
    xorg-xwayland wl-clipboard grim slurp \
    qt5-wayland qt6-wayland hyprpolkitagent sddm
fws_disable_services lightdm.service gdm.service
fws_enable_services sddm.service
fws_set_graphical_default
fws_write_sddm_config "$first_user" hyprland
fws_write_hyprland_config "$first_user"