#!/bin/sh
set -e

# Functie om te bepalen of we in een build-omgeving (chroot) zitten
is_chroot() {
    [ "$(stat -c %d/%i /)" != "$(stat -c %d/%i /proc/1/root/.)" ]
}

case "$1" in
    configure)
        echo "ALinux Voltron Branding activeren..."

        # 1. Desktop Wallpaper (Hoogste prioriteit 200)
        update-alternatives --install /usr/share/images/desktop-base/desktop-background \
            desktop-background /usr/share/alinux/wallpapers/voltron-wallpaper.svg 200

        # 2. De 'default' link die XFCE zoekt
        ln -sf /usr/share/alinux/wallpapers/voltron-wallpaper.svg /usr/share/images/desktop-base/default

        # 3. Plymouth (Boot animatie)
        if [ -d /usr/share/plymouth/themes/alinux ]; then
            plymouth-set-default-theme alinux || true
        fi

        # 4. GRUB Background
        update-alternatives --install /usr/share/images/desktop-base/desktop-grub.png \
            desktop-grub /usr/share/alinux/grub/grub-background.png 200

        # 5. GNOME / Lockscreen XML
        update-alternatives --install /usr/share/images/desktop-base/desktop-lockscreen.xml \
            desktop-lockscreen.xml /usr/share/alinux/wallpapers/voltron-lockscreen.svg 200

        # --- SYSTEEM UPDATES ---
        # Voer deze ALLEEN uit op een geinstalleerd systeem, NIET tijdens de ISO-build.
        if ! is_chroot; then
            echo "Geen chroot gedetecteerd, systeem-updates uitvoeren..."
            if which update-grub > /dev/null 2>&1; then
                update-grub || true
            fi
            if which update-initramfs > /dev/null 2>&1; then
                update-initramfs -u || true
            fi
        else
            echo "Chroot gedetecteerd (Live-Build): overgeslagen om kernel-fouten te voorkomen."
        fi
        ;;
esac

exit 0
