#!/bin/bash
set -euo pipefail

SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
PACKAGES_FILE="$SCRIPT_DIR/packages.txt"

# ---------------------------------------------------------------------------
# Enable multilib in /etc/pacman.conf (with a safety backup)
# ---------------------------------------------------------------------------
enable_multilib() {
    sudo sed -i.bak '/^#\[multilib\]/,+1 s/^#//' /etc/pacman.conf
}

# ---------------------------------------------------------------------------
# Ensure yay (AUR helper) is installed.
# ---------------------------------------------------------------------------
install_yay() {
    if pacman -Q yay &>/dev/null; then
        echo "yay bereits installiert; fahre fort."
        return
    fi
    if pacman -Si yay &>/dev/null; then
        echo "Installiere yay mit pacman ..."
        sudo pacman -S --needed yay
        return
    fi
    echo "installiere yay via git ..."
    sudo pacman -S --needed base-devel git
    local tmp
    tmp="$(mktemp -d)"
    git clone https://aur.archlinux.org/yay.git "$tmp/yay"
    (
        cd "$tmp/yay"
        makepkg -si
    )
    rm -rf "$tmp"
}

# ---------------------------------------------------------------------------
# Read packages.txt, split into pacman / aur / npm sections.
# ---------------------------------------------------------------------------
read_packages() {
    local section="pacman"
    local current
    current=""
    while IFS= read -r line || [[ -n "$line" ]]; do
        current="${line%%#*}"
        current="${current##*( )}"
        current="${current%%*( )}"
        [[ -z "$current" ]] && continue
        case "$current" in
            @pacman) section="pacman" ;;
            @aur)    section="aur" ;;
            @npm)    section="npm" ;;
            *)
                case "$section" in
                    pacman) pacman_pkgs+=("$current") ;;
                    aur)    aur_pkgs+=("$current") ;;
                    npm)    npm_pkgs+=("$current") ;;
                esac
                ;;
        esac
    done < "$PACKAGES_FILE"
}

declare -a pacman_pkgs=() aur_pkgs=() npm_pkgs=()

# ---------------------------------------------------------------------------
# Install sections.
# ---------------------------------------------------------------------------
install_pacman() {
    if ((${#pacman_pkgs[@]})); then
        echo "==> Installiere Pacman-Pakete: ${pacman_pkgs[*]}"
        yay -S --needed "${pacman_pkgs[@]}"
    fi
}

install_aur() {
    if ((${#aur_pkgs[@]})); then
        echo "==> Installiere AUR-Pakete: ${aur_pkgs[*]}"
        yay -S --needed "${aur_pkgs[@]}"
    fi
}

install_npm() {
    if ((${#npm_pkgs[@]})); then
        echo "==> Installiere globale npm-Pakete: ${npm_pkgs[*]}"
        npm install -g "${npm_pkgs[@]}"
    fi
}

# ---------------------------------------------------------------------------
# Write SDDM configuration and switch display manager from gdm to sddm.
# ---------------------------------------------------------------------------
configure_sddm() {
    echo "==> Schreibe /etc/sddm.conf ..."
    echo "
[General]
Numlock=on

[Theme]
ThemeDir=/usr/share/sddm/themes            # Standardverzeichnis für Designvorlagen
Current=catppuccin-mocha-blue              # Standardthema
CursorTheme=breeze_cursors                 # Mausthema
FacesDir=/usr/share/sddm/faces             # Verzeichnis für Avatarbilder

[Users]
MaximumUID=1100
HideUsers=mathieu
" | sudo tee /etc/sddm.conf >/dev/null

    echo "==> Deaktiviere gdm, aktiviere sddm ..."
    sudo systemctl disable gdm
    sudo systemctl enable sddm
}

# ---------------------------------------------------------------------------
# Main
# ---------------------------------------------------------------------------
main() {
    echo "==> Aktiviere multilib ..."
    enable_multilib

    echo "==> System aktualisieren ..."
    sudo pacman -Syu

    echo "==> Sicherstelle yay ..."
    install_yay

    echo "==> Lese Paketliste: $PACKAGES_FILE"
    read_packages

    install_pacman
    install_aur
    install_npm

    configure_sddm

    echo "Fertig. Die Basis-Systemumgebung ist eingerichtet."
}

main
