#!/bin/sh
set -e

# Tag to allow some debhelper commands to inject relevant code
#DEBHELPER#

if [ "${1}" = "configure" ] && [ ! -z "${2}" ]; then
    if dpkg --compare-versions ${2} le "9.0.0~exp1"; then
        # Try remove folder if empty, left after 9.0.0~exp1 conf suppression
        # by debhelper conffile handling
        OLD_KDM_DIR=/etc/default/kdm.d
        if [ -d ${OLD_KDM_DIR} ] ; then
            echo "Removing old KDM configuration directory \"${OLD_KDM_DIR}\""
            rmdir ${OLD_KDM_DIR} || true
        fi
    fi
fi

if [ "$1" = "configure" ] || [ "$1" = "abort-upgrade" ] || [ "$1" = "abort-remove" ]; then
    # Theme package alternatives
    while read theme priority; do
        update-alternatives --install \
            /usr/share/desktop-base/active-theme \
            desktop-theme \
            /usr/share/desktop-base/$theme-theme $priority
    done << EOF
yellowmountain 60
blue 50
anger 40
gray 30
EOF

    # Use active theme as highest priority for background
    update-alternatives --install \
        /usr/share/images/desktop-base/desktop-background \
        desktop-background \
        /usr/share/desktop-base/active-theme/wallpaper/contents/images/1920x1080.svg 75
    # Alternatives for the background in theme packages
    while read theme filename priority; do
        update-alternatives --install \
            /usr/share/images/desktop-base/desktop-background \
            desktop-background \
            /usr/share/desktop-base/$theme-theme/wallpaper/contents/images/$filename $priority
    done << EOF
anger 1280x1024.svg 60
anger 1600x1200.svg 60
anger 1920x1080.svg 60
anger 1920x1200.svg 60
anger 2560x1080.svg 60
blue 1280x1024.svg 65
blue 1600x1200.svg 65
blue 1920x1080.svg 65
blue 1920x1200.svg 65
blue 2560x1080.svg 65
gray 1600x1200.svg 55
gray 1280x1024.svg 55
gray 1920x1080.svg 55
gray 1920x1200.svg 55
yellowmountain 1280x1024.svg 70
yellowmountain 1600x1200.svg 70
yellowmountain 1920x1080.svg 75
yellowmountain 1920x1200.svg 70
yellowmountain 2560x1080.svg 70
EOF

    # Set up an alternative for the XML version of the background
    # (for GNOME)
    # Highest priority for active theme
    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
    # Alternatives for theme packages
    while read theme priority; do
        update-alternatives --install \
            /usr/share/images/desktop-base/desktop-background.xml \
            desktop-background.xml \
            /usr/share/desktop-base/$theme-theme/wallpaper/gnome-background.xml $priority
    done << EOF
yellowmountain 50
blue 40
anger 30
gray 20
EOF

    # Set up an alternative for the XML version of the lock screen
    # (for GNOME)
    # Highest priority for active theme
    update-alternatives --install \
        /usr/share/images/desktop-base/desktop-lockscreen.xml \
        desktop-lockscreen.xml \
        /usr/share/desktop-base/active-theme/lockscreen/gnome-background.xml 50
    # Alternatives for theme packages
    while read theme priority; do
        update-alternatives --install \
            /usr/share/images/desktop-base/desktop-lockscreen.xml \
            desktop-lockscreen.xml \
            /usr/share/desktop-base/$theme-theme/lockscreen/gnome-background.xml $priority
    done << EOF
yellowmountain 50
blue 40
EOF

    # Set up an alternative for the wallpaper for Plasma 5/KDE
    # Highest priority for active theme
    update-alternatives --install \
        /usr/share/wallpapers/DebianTheme \
        desktop-plasma5-wallpaper \
        /usr/share/desktop-base/active-theme/wallpaper 50
    # Alternatives for theme packages
    while read theme priority; do
        update-alternatives --install \
            /usr/share/wallpapers/DebianTheme \
            desktop-plasma5-wallpaper \
            /usr/share/desktop-base/$theme-theme/wallpaper $priority
    done << EOF
yellowmountain 50
blue 40
anger 30
gray 20
EOF

    # Login theme
    # Highest priority for active theme
    update-alternatives --install /usr/share/images/desktop-base/login-background.svg \
        desktop-login-background \
        /usr/share/desktop-base/active-theme/login/background.svg 50
    # Alternatives for theme packages
    while read theme background priority; do
        update-alternatives --install /usr/share/images/desktop-base/login-background.svg \
            desktop-login-background \
            /usr/share/desktop-base/$theme-theme/login/$background $priority
    done << EOF
yellowmountain background.svg 50
blue background.svg 40
anger background.svg 30
gray background.svg 20
EOF

    # Alternatives for grub
    ## Favor widescreen / hi-res background for efi installations
    num_grub_efi_installed=$(dpkg-query --list "grub-efi*" | grep "^i" | wc -l)
    if [ $num_grub_efi_installed -gt 0 ] ; then
        pardus_grub_prio=15
        pardus_grub_1920_prio=20
    else
        pardus_grub_prio=20
        pardus_grub_1920_prio=15
    fi
    update-alternatives --install \
        /usr/share/images/desktop-base/desktop-grub.png \
        desktop-grub \
        /usr/share/images/desktop-base/pardus-grub.png $pardus_grub_prio
    update-alternatives --install \
        /usr/share/images/desktop-base/desktop-grub.png \
        desktop-grub \
        /usr/share/images/desktop-base/pardus-grub-1920x1080.png $pardus_grub_1920_prio

    while read background priority; do
        update-alternatives --install \
            /usr/share/images/desktop-base/desktop-grub.png \
            desktop-grub \
            /usr/share/images/desktop-base/$background $priority
    done << EOF
EOF

    # GRUB background
    if which update-grub2 > /dev/null ; then
        sync
        update-grub2 || true
    fi

    if [ -x /usr/sbin/update-initramfs ]; then
        update-initramfs -u
    fi

fi