#! /bin/sh
set -e

EDUTHEME="spacefun"

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
}

change_ldm_background() {
    update-alternatives --install /usr/share/ldm/themes/default \
        ldm-theme /usr/share/ldm/themes/debian-edu-${EDUTHEME} 85
}

restore_ldm_background() {
    update-alternatives --remove ldm-theme /usr/share/ldm/themes/debian-edu-${EDUTHEME}
}

## 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 which update-grub > /dev/null ; then
        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 which 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.png 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 1024x768.png \
                     1280x1024.png \
                     1280x800.png \
                     1366x768.png \
                     1600x1200.png \
                     1920x1080.png \
                     1920x1200.png; do
    update-alternatives --install \
        /usr/share/images/desktop-base/desktop-background \
        desktop-background \
        /usr/share/desktop-base/debian-edu-${EDUTHEME}-theme/wallpaper/contents/images/$WALLPAPER 50
    done
}

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

    for WALLPAPER in 1024x768.png \
                     1280x1024.png \
                     1280x800.png \
                     1366x768.png \
                     1600x1200.png \
                     1920x1080.png \
                     1920x1200.png; do
    update-alternatives --remove \
        desktop-background \
        /usr/share/desktop-base/debian-edu-${EDUTHEME}-theme/wallpaper/contents/images/$WALLPAPER
    done
}

## Wallpaper
compile_gschemas() {
    if [ -d /usr/share/glib-2.0/schemas/ ] && [ -x /usr/bin/glib-compile-schemas ]; then
        glib-compile-schemas /usr/share/glib-2.0/schemas/
    fi
}

## Plymouth
set_plymouth_theme() {
    if [ -f /etc/plymouth/plymouthd.conf ] && [ -f /etc/debian-edu/config ] && which plymouth-set-default-theme > /dev/null ; then
        plymouth-set-default-theme -R debian-edu-${EDUTHEME} || 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
}

case "$1" in
	configure)
		if [ -x /usr/sbin/update-debian-edu-artwork-softwaves ]; then
			update-debian-edu-artwork-softwaves remove
		fi
		if [ -x /usr/sbin/update-debian-edu-artwork-lines ]; then
			update-debian-edu-artwork-lines remove
		fi
		set_active_theme
		change_ldm_background
		change_grub_background
		change_desktop_background
		compile_gschemas
		set_plymouth_theme
		set_lockscreen
	;;
	remove)
		restore_active_theme
		restore_ldm_background
		restore_grub_background
		restore_desktop_background
		compile_gschemas
		restore_lockscreen
	;;
esac
