#!/bin/sh
set -e

BASE=/usr/share/kazamos-desktop-base/kazamos

if [ "$1" = "configure" ] || [ "$1" = "abort-upgrade" ] || [ "$1" = "abort-remove" ]; then

    # Create required directories for update-alternatives
    mkdir -p /usr/share/images/desktop-base
    mkdir -p /usr/share/desktop-base

    # GRUB background
    # Prefer 16x9 if grub-efi is present, otherwise 4x3
    if dpkg-query --list grub-efi* | grep -v "^... grub-efi[^[:space:]]*-bin" | grep -q "^[ih][HUFWti] "; then
        grub_first="16x9"
        grub_second="4x3"
    else
        grub_first="4x3"
        grub_second="16x9"
    fi

    update-alternatives --install \
        /usr/share/images/desktop-base/desktop-grub.png \
        desktop-grub \
        ${BASE}/grub/grub-${grub_first}.png 60 \
        --slave /usr/share/desktop-base/grub_background.sh \
            desktop-grub.sh \
            ${BASE}/grub/grub_background.sh

    update-alternatives --install \
        /usr/share/images/desktop-base/desktop-grub.png \
        desktop-grub \
        ${BASE}/grub/grub-${grub_second}.png 55 \
        --slave /usr/share/desktop-base/grub_background.sh \
            desktop-grub.sh \
            ${BASE}/grub/grub_background.sh

    # Login background
    update-alternatives --install \
        /usr/share/images/desktop-base/login-background.svg \
        desktop-login-background \
        ${BASE}/login/background.svg 60

    update-alternatives --install \
        /usr/share/images/desktop-base/login-background.svg \
        desktop-login-background \
        ${BASE}/login/background-nologo.svg 55

    # Wallpaper
    for res in 1080x2160 1280x1024 1280x800 1920x1200 2520x1080; do
        update-alternatives --install \
            /usr/share/images/desktop-base/desktop-background \
            desktop-background \
            ${BASE}/wallpaper/contents/images/${res}.svg 75
    done

    update-alternatives --install \
        /usr/share/images/desktop-base/desktop-background \
        desktop-background \
        ${BASE}/wallpaper/contents/images/1920x1080.svg 80

    if dpkg-query --list "grub-*" \
            | grep -v "^... grub\(-common\|-emu\|[^[:space:]]*\(-bin\|-doc\)\)" \
            | grep -q "^[ih][HUFWti] " \
            && which update-grub > /dev/null; then
        sync
        update-grub || echo "update-grub failed, continuing anyway"
    fi

    cp /usr/share/pixmaps/kOS.png /etc/skel/.face
    (cd /etc/skel;ln -s .face .face.icon)

fi
