#! /bin/sh
set -e

THEMES="buster ceratopsian emerald homeworld softwaves"

edutheme="$(basename "$0")"
edutheme="${edutheme#update-debian-edu-artwork-}"

set_active_theme() {
    update-alternatives --install \
        /usr/share/desktop-base/active-theme \
        desktop-theme \
        /usr/share/desktop-base/debian-edu-${edutheme}-theme 60
}

restore_active_theme() {
    update-alternatives --remove \
        desktop-theme \
        /usr/share/desktop-base/debian-edu-${edutheme}-theme
}

## GRUB and PXE boot background
change_grub_background() {
    update-alternatives --install /usr/share/images/desktop-base/desktop-grub.png \
        desktop-grub \
        /usr/share/desktop-base/debian-edu-${edutheme}-theme/grub/grub-4x3.png 40 \
        --slave /usr/share/desktop-base/grub_background.sh \
        desktop-grub.sh \
        /usr/share/desktop-base/debian-edu-${edutheme}-theme/grub/grub_background.sh
    update-alternatives --install /usr/share/images/desktop-base/desktop-grub.png \
        desktop-grub \
        /usr/share/desktop-base/debian-edu-${edutheme}-theme/grub/grub-16x9.png 40 \
        --slave /usr/share/desktop-base/grub_background.sh \
        desktop-grub.sh \
        /usr/share/desktop-base/debian-edu-${edutheme}-theme/grub/grub_background.sh
    if command -v update-grub > /dev/null ; then
        # We have seen situations where grub is installed at the end of an
        # installation, but /boot/grub has not been created, yet, when running
        # this postinst's script function code.
        #
        # This e.g. can be observed when installing debian-edu-router using FAI.
        mkdir -p /boot/grub

        sync
        update-grub || true
    fi
}

restore_grub_background() {
    update-alternatives --remove desktop-grub \
        /usr/share/desktop-base/debian-edu-${edutheme}-theme/grub/grub-4x3.png
    update-alternatives --remove desktop-grub \
        /usr/share/desktop-base/debian-edu-${edutheme}-theme/grub/grub-16x9.png
    if command -v update-grub > /dev/null ; then
        update-grub || true
    fi
}

## Wallpaper
change_desktop_background() {
    update-alternatives --install \
        /usr/share/images/desktop-base/desktop-background \
        desktop-background \
        /usr/share/desktop-base/active-theme/wallpaper/contents/images/1920x1080.svg 70
    update-alternatives --install \
        /usr/share/images/desktop-base/desktop-background.xml \
        desktop-background.xml \
        /usr/share/desktop-base/active-theme/wallpaper/gnome-background.xml 50
    update-alternatives --install \
        /usr/share/images/desktop-base/desktop-background.xml \
        desktop-background.xml \
        /usr/share/desktop-base/debian-edu-${edutheme}-theme/wallpaper/gnome-background.xml 50

    for wallpaper in \
        "/usr/share/desktop-base/debian-edu-${edutheme}-theme/wallpaper/contents/images/"[0-9]*x*.svg;
        do
        update-alternatives --install \
            /usr/share/images/desktop-base/desktop-background \
            desktop-background \
            "${wallpaper}" 50
    done
}

restore_desktop_background() {
    update-alternatives --remove \
        desktop-background \
        /usr/share/desktop-base/active-theme/wallpaper/contents/images/1920x1080.svg
    update-alternatives --remove \
        desktop-background.xml \
        /usr/share/desktop-base/debian-edu-${edutheme}-theme/wallpaper/gnome-background.xml

    for wallpaper in \
        "/usr/share/desktop-base/debian-edu-${edutheme}-theme/wallpaper/contents/images/"[0-9]*x*.svg;
        do
    update-alternatives --remove \
        desktop-background \
        "${wallpaper}"
    done
}

## Plymouth
set_plymouth_theme() {
    # The check whether we are on a Debian Edu system is vital for this part of
    # the script being compliant with Debian policy. Never remove that check!
    # See: #1057815 and #856789.
    if [ -f /etc/plymouth/plymouthd.conf ] && [ -f /etc/debian-edu/config ] && command -v plymouth-set-default-theme > /dev/null ; then
        plymouth-set-default-theme -R debian-edu-${edutheme} || true
    fi
}

restore_plymouth_theme() {
    if [ -f /etc/plymouth/plymouthd.conf ] && command -v plymouth-set-default-theme > /dev/null ; then
        plymouth-set-default-theme -R -r || true
    fi
}

## Lockscreen
set_lockscreen() {
    update-alternatives --install \
        /usr/share/images/desktop-base/desktop-lockscreen.xml \
        desktop-lockscreen.xml \
        /usr/share/desktop-base/debian-edu-${edutheme}-theme/lockscreen/gnome-background.xml 50
}

restore_lockscreen() {
    update-alternatives --remove \
        desktop-lockscreen.xml \
        /usr/share/desktop-base/debian-edu-${edutheme}-theme/lockscreen/gnome-background.xml
}

is_valid=false
for theme in $THEMES; do
    if [ "${edutheme}" = "${theme}" ]; then
        is_valid=true
    fi
done
$is_valid || exit 1

case "$1" in
	configure)
		for theme in $THEMES; do
			[ "${theme}" = "${edutheme}" ] && continue
			if [ -x "/usr/sbin/update-debian-edu-artwork-${theme}" ]; then
				"/usr/sbin/update-debian-edu-artwork-${theme}" remove
			fi
		done
		set_active_theme
		change_grub_background
		change_desktop_background
		set_plymouth_theme
		set_lockscreen
	;;
	remove)
		restore_active_theme
		restore_grub_background
		restore_desktop_background
		restore_plymouth_theme
		restore_lockscreen
	;;
esac
