#!/bin/sh
set -e

THEME_PATH="/usr/share/plymouth/themes/alinux/alinux.plymouth"

if [ "$1" = "configure" ]; then
    echo "ALinux Boot Theme installeren..."

    # 1. Registreer het thema bij het systeem
    if [ -f "$THEME_PATH" ]; then
        update-alternatives --install /usr/share/plymouth/themes/default.plymouth \
            default.plymouth "$THEME_PATH" 100
        
        # Zet hem direct als actief
        update-alternatives --set default.plymouth "$THEME_PATH"
    fi

    # 2. Forceer de config file (Plymouth negeert soms alternatives)
    if [ -f /etc/plymouth/plymouthd.conf ]; then
        # Vervang Theme=... door Theme=alinux
        sed -i 's/^Theme=.*/Theme=alinux/' /etc/plymouth/plymouthd.conf || true
    else
        # Als de file niet bestaat, maak hem aan
        echo "[Daemon]" > /etc/plymouth/plymouthd.conf
        echo "Theme=alinux" >> /etc/plymouth/plymouthd.conf
    fi

    # 3. Fix voor het Debian Logo (voorkomt foutmeldingen in logs)
    # Alleen doen als de map bestaat
    if [ -d /usr/share/plymouth ]; then
        # Backup origineel als het geen symlink is
        if [ -f /usr/share/plymouth/debian-logo.png ] && [ ! -L /usr/share/plymouth/debian-logo.png ]; then
            mv /usr/share/plymouth/debian-logo.png /usr/share/plymouth/debian-logo.png.bak
        fi
        ln -sf themes/alinux/logo.png /usr/share/plymouth/debian-logo.png
    fi

    # 4. Initramfs update
    # We checken op 'update-initramfs' command, dat is veiliger dan /proc checks
    if command -v update-initramfs >/dev/null; then
        echo "Updating initramfs..."
        update-initramfs -u
    fi
fi

exit 0
