#!/bin/sh -e
set -e

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

if [ "$1" = "remove" ] || [ "$1" = "deconfigure" ]; then
    # Remove background alternatives for theme packages
    while read theme filename; do
        update-alternatives --remove \
            desktop-background \
            /usr/share/desktop-base/$theme-theme/wallpaper/contents/images/$filename $priority
    done << EOF
softwaves 1280x720.svg
softwaves 1280x800.svg
softwaves 1920x1080.svg
softwaves 1920x1200.svg
softwaves 2560x1440.svg
softwaves 2560x1600.svg
lines 1280x1024.svg
lines 1600x1200.svg
lines 1920x1080.svg
lines 1920x1200.svg
lines 2560x1080.svg
EOF
    # Remove old background alternatives
    for background in \
          joy-wallpaper_1600x1200.svg \
          joy-wallpaper_1600x1200.svg \
          joy-wallpaper_1280x1024.svg \
          joy-wallpaper_1920x1080.svg \
          joy-wallpaper_1920x1200.svg \
          joy-inksplat-wallpaper_1920x1080.svg \
          spacefun-wallpaper.svg \
          spacefun-wallpaper-widescreen.svg \
          moreblue-orbit-wallpaper.svg \
          moreblue-orbit-wallpaper-widescreen.svg \
          ; do
        update-alternatives --remove desktop-background /usr/share/images/desktop-base/$background
    done
    # *Last* remove background *highest priority* alternatives for active theme
    update-alternatives --remove desktop-background /usr/share/desktop-base/active-theme/wallpaper/contents/images/1920x1080.svg

    # Remove desktop-background.xml alternatives
    # For theme packages
    while read theme; do
        update-alternatives --remove \
            desktop-background.xml \
            /usr/share/desktop-base/$theme-theme/wallpaper/gnome-background.xml $priority
    done << EOF
softwaves
lines
EOF
    # For old alternatives
    for desktopbackground in \
          joy.xml \
          ; do
        update-alternatives --remove desktop-background.xml /usr/share/images/desktop-base/$desktopbackground
    done
    # *Lastly* remove *highest priority* alternative for active theme
    update-alternatives --remove \
        desktop-background.xml \
        /usr/share/desktop-base/active-theme/wallpaper/gnome-background.xml

    # Remove desktop-lockscreen.xml alternatives
    # For theme packages
    while read theme; do
        update-alternatives --remove \
            desktop-lockscreen.xml \
            /usr/share/desktop-base/$theme-theme/lockscreen/gnome-background.xml $priority
    done << EOF
softwaves
lines
EOF
    # *Last* remove *highest priority* alternative for active theme
    update-alternatives --remove \
        desktop-lockscreen.xml \
        /usr/share/desktop-base/active-theme/lockscreen/gnome-background.xml

    # Remove Plasma 5/KDE wallpaper alternatives
    # For theme packages
    while read theme; do
        update-alternatives --remove \
            desktop-plasma5-wallpaper \
            /usr/share/desktop-base/$theme-theme/wallpaper
    done << EOF
softwaves
lines
EOF
    # *Last* remove *highest priority* alternative for active theme
    update-alternatives --remove \
        desktop-plasma5-wallpaper \
        /usr/share/desktop-base/active-theme/wallpaper

    # Remove login theme alternatives
    # For theme packages
    # Alternative for theme packages
    while read theme background sddm_preview; do
        update-alternatives --remove \
            desktop-sddm-preview \
            /usr/share/desktop-base/$theme-theme/login/$sddm_preview
        update-alternatives --remove \
            desktop-login-background \
            /usr/share/desktop-base/$theme-theme/login/$background
    done << EOF
softwaves background.svg sddm-preview.jpg
lines background.svg sddm-preview.jpg
lines background-nologo.svg sddm-preview-nologo.jpg
EOF
    # For old alternatives
    ## Joy
    ### secondary alternatives
    update-alternatives --remove desktop-sddm-preview \
        /usr/share/desktop-base/joy-theme/sddm-preview.jpg
    update-alternatives --remove desktop-login-background \
        /usr/share/desktop-base/joy-theme/login-background.svg
    # *Last* remove *highest priority* alternative for active theme
    update-alternatives --remove \
        desktop-sddm-preview \
        /usr/share/desktop-base/active-theme/login/sddm-preview.jpg
    update-alternatives --remove \
        desktop-login-background \
        /usr/share/desktop-base/active-theme/login/background.svg

    # Remove grub background alternatives
    # For theme packages
    while read theme ratio; do
        update-alternatives --remove \
            desktop-grub \
            /usr/share/desktop-base/$theme-theme/grub/grub-$ratio.png
    done << EOF
softwaves 4x3
softwaves 16x9
lines 4x3
lines 16x9
EOF
    # For old alternatives
    while read background; do
        update-alternatives --remove \
            desktop-grub \
            /usr/share/images/desktop-base/$background
    done << EOF
joy-grub.png
spacefun-grub.png
spacefun-grub-widescreen.png
EOF
    ## *Lastly* remove *highest priority* alternative
    num_grub_efi_installed=$(dpkg-query --list "grub-efi*" 2> /dev/null | grep "^i" | wc -l)
    if [ $num_grub_efi_installed -gt 0 ] ; then
        remove_first_ratio=4x3
        remove_last_ratio=16x9
    else
        remove_first_ratio=16x9
        remove_last_ratio=4x3
    fi
    update-alternatives --remove \
        desktop-grub \
        /usr/share/desktop-base/active-theme/grub/grub-$remove_first_ratio.png
    update-alternatives --remove \
        desktop-grub \
        /usr/share/desktop-base/active-theme/grub/grub-$remove_last_ratio.png

    # Remove theme package alternatives
    while read theme; do
        update-alternatives --remove \
            desktop-theme \
            /usr/share/desktop-base/$theme-theme
    done << EOF
lines
EOF

    ## *Lastly* remove *highest priority* alternative
    update-alternatives --remove \
        desktop-theme \
        /usr/share/desktop-base/softwaves-theme

fi
