#! /bin/sh
set -e

THEMES="buster ceratopsian emerald homeworld softwaves spacefun"

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

if [ "${edutheme}" = "${basename}" ]; then
    echo "${basename} script called directly, please call ${basename}-<theme> scripts (symlinks) instead."
    exit 1
fi

check_alternative_installed() {
    alt_name="$1"
    alt_path="$2"
    if update-alternatives --list "${alt_name}" | grep -q "${alt_path}"; then
        # alternative already installed
        return 0
    fi
    # alternative not installed, yet
    return 1
}

set_active_theme() {
    if ! check_alternative_installed desktop-theme /usr/share/desktop-base/debian-edu-${edutheme}-theme; then
        update-alternatives --install \
            /usr/share/desktop-base/active-theme \
            desktop-theme \
            /usr/share/desktop-base/debian-edu-${edutheme}-theme 60
    fi
    update-alternatives --set desktop-theme /usr/share/desktop-base/debian-edu-${edutheme}-theme
}

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

## GRUB and PXE boot background
change_grub_background() {
    # Tie desktop-grub to the active-theme.
    if check_alternative_installed desktop-grub /usr/share/desktop-base/active-theme/grub/grub-16x9.png; then
        # prefer 16:9 grub theme (e.g. FullHD and friends)
        update-alternatives --set desktop-grub /usr/share/desktop-base/active-theme/grub/grub-16x9.png
    fi

    # Install all our desktop-grub variants as alternative options, if needed by the system's admin.
    if ! check_alternative_installed desktop-grub /usr/share/desktop-base/debian-edu-${edutheme}-theme/grub/grub-4x3.png; then
        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
    fi
    if ! check_alternative_installed desktop-grub /usr/share/desktop-base/debian-edu-${edutheme}-theme/grub/grub-16x9.png; then
        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
    fi

    # update the grub configuration
    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

    # update the grub configuration
    if command -v update-grub > /dev/null ; then
        update-grub || true
    fi
}

## Wallpaper
change_desktop_background() {
    # Tie desktop-background to the active-theme.
    if check_alternative_installed desktop-background /usr/share/desktop-base/active-theme/wallpaper/contents/images/1920x1080.svg; then
        update-alternatives --set desktop-background /usr/share/desktop-base/active-theme/wallpaper/contents/images/1920x1080.svg
    fi
    # Install all our background wallpapers as alternative options, if needed by the system's admin.
    for wallpaper in \
        "/usr/share/desktop-base/debian-edu-${edutheme}-theme/wallpaper/contents/images/"[0-9]*x*.svg;
    do
        if ! check_alternative_installed desktop-background "${wallpaper}"; then
            update-alternatives --install \
                /usr/share/images/desktop-base/desktop-background \
                desktop-background \
                "${wallpaper}" 50
        fi
    done

    # Tie desktop-background.xml to the active-theme.
    if check_alternative_installed desktop-background.xml /usr/share/desktop-base/active-theme/wallpaper/gnome-background.xml; then
        update-alternatives --set desktop-background.xml /usr/share/desktop-base/active-theme/wallpaper/gnome-background.xml
    fi

    # Install all our desktop-background.xml as alternative option, if needed by the system's admin.
    if ! check_alternative_installed desktop-background.xml /usr/share/desktop-base/debian-edu-${edutheme}-theme/wallpaper/gnome-background.xml; then
        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
    fi
}

restore_desktop_background() {
    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() {
    # In the old times, we violated Debian policy in Debian Edu and modified
    # /etc/plymouth/plymouthd.conf via this function (see #1057815 and #856789).
    #
    # The policy violation has been mitigated by only modifying plymouthd.conf if
    # the Debian system is a Debian Edu system.
    #
    # For some time now, Plymouth theming is half-supported desktop-base
    # via update-alternatives. If proposed changes for desktop-base (#1148027)
    # and plymouth (#1148087) land before the forky release, modifying
    # /etc/plymouth/plymouthd.conf will not be necessary anymore to enable
    # debian-edu-artwork*'s Plymouth theme, it will be co-activated via
    # update-alternatives --config desktop-theme.
    #
    # However, until forky let's make sure Debian Edu systems start using
    # the virtual 'debian-theme' Plymouth theme, so we enforce it by tweaking
    # /etc/plymouth/plymouthd.conf via the plymouth-set-default-theme command
    # (on Debian Edu systems only, of course).
    #
    # Once above desktop-base and plymouth bugs have been resolved, the
    # set_plymouth_theme() and restore_plymouth_theme() functions can be dropped
    # from this script.
    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-theme || 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() {
    # Tie desktop-lockscreen.xml to the active-theme.
    if check_alternative_installed desktop-lockscreen.xml /usr/share/desktop-base/active-theme/lockscreen/gnome-background.xml; then
        update-alternatives --set desktop-lockscreen.xml /usr/share/desktop-base/active-theme/lockscreen/gnome-background.xml
    fi

    # Install all our desktop-lockscreen.xml as alternative option, if needed by the system's admin.
    if ! check_alternative_installed desktop-lockscreen.xml /usr/share/desktop-base/debian-edu-${edutheme}-theme/lockscreen/gnome-background.xml; then
        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
    fi
}

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)
		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
