#!/bin/sh
set -e


if [ "$1" = "configure" ] || [ "$1" = "abort-upgrade" ] || [ "$1" = "abort-remove" ]; then
    # Vendor logo alternative
    update-alternatives --install \
            /usr/share/images/vendor-logos \
            vendor-logos \
            /usr/share/images/kali-logos 200 \
            --slave /usr/share/icons/vendor/64x64/emblems/emblem-vendor.png \
                emblem-vendor-64 \
                /usr/share/icons/desktop-base/64x64/emblems/emblem-kali.png \
            --slave /usr/share/icons/vendor/128x128/emblems/emblem-vendor.png \
                emblem-vendor-128 \
                /usr/share/icons/desktop-base/128x128/emblems/emblem-kali.png \
            --slave /usr/share/icons/vendor/256x256/emblems/emblem-vendor.png \
                emblem-vendor-256 \
                /usr/share/icons/desktop-base/256x256/emblems/emblem-kali.png \
            --slave /usr/share/icons/vendor/scalable/emblems/emblem-vendor.svg \
                emblem-vendor-scalable \
                /usr/share/icons/desktop-base/scalable/emblems/emblem-kali.svg \

    # Theme package alternatives
    update-alternatives --install \
	/usr/share/desktop-base/active-theme \
	desktop-theme \
	/usr/share/desktop-base/kali-theme 200

    # Desktop background alternatives
    update-alternatives --install \
	/usr/share/images/desktop-base/desktop-background \
	desktop-background /usr/share/desktop-base/kali-theme/wallpaper/contents/images/3840x2160.png 210

    # Set up an alternative for the XML version of the background
    # (for GNOME)
    update-alternatives --install \
	/usr/share/images/desktop-base/desktop-background.xml \
	desktop-background.xml /usr/share/desktop-base/kali-theme/wallpaper/gnome-background.xml 210

    # Set up an alternative for the XML version of the lock screen
    # (for GNOME)
    update-alternatives --install \
	/usr/share/images/desktop-base/desktop-lockscreen.xml \
	desktop-lockscreen.xml /usr/share/desktop-base/kali-theme/lockscreen/gnome-background.xml 210

    # Set up an alternative for the wallpaper for Plasma 5/KDE
    update-alternatives --install \
	/usr/share/wallpapers/DebianTheme \
	desktop-plasma5-wallpaper /usr/share/desktop-base/kali-theme/wallpaper 210

    # 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/kali-theme/login/background.svg 210

    # Set up an alternative for the GRUB background/colors config
    # Highest priority for active theme
    # Favor widescreen / hi-res background for efi installations detected
    # by the presence of grub-efi* packages (not the grub-efi*-bin which don’t
    # necessary account for grub being the active bootloader).
    if dpkg-query --list grub-efi* | grep -v "^... grub-efi[^[:space:]]*-bin" | grep -q "^[ih][HUFWti] " ; then
        echo "grub-efi* packages found, using 16/9 as default grub background ratio"
        grub_first_ratio="16x9"
        grub_second_ratio="4x3"
    else
        echo "No grub-efi* package found, using 4/3 as default grub background ratio"
        grub_first_ratio="4x3"
        grub_second_ratio="16x9"
    fi
    active_grub_first_ratio=/usr/share/grub/themes/kali/grub-${grub_first_ratio}.png
    active_grub_second_ratio=/usr/share/grub/themes/kali/grub-${grub_second_ratio}.png
    update-alternatives --install \
	/usr/share/images/desktop-base/desktop-grub.png \
	desktop-grub ${active_grub_first_ratio} 200 \
	--slave /usr/share/desktop-base/grub_background.sh \
	desktop-grub.sh /usr/share/grub/themes/kali/grub_background.sh
    update-alternatives --install \
	/usr/share/images/desktop-base/desktop-grub.png \
	desktop-grub ${active_grub_second_ratio} 190 \
	--slave /usr/share/desktop-base/grub_background.sh \
	desktop-grub.sh /usr/share/grub/themes/kali/grub_background.sh

    # Apply GRUB background update into /boot
    # Try detecting active grub packages (so not -doc, -common, -bin) as a hint
    # that GRUB is being used as bootloader before calling update-grub.
    # Some people use the *-bin packages without using GRUB as bootloader
    # (see #851893 for more context).
    if dpkg-query --list "grub-*" | grep -v "^... grub\(-common\|-emu\|[^[:space:]]*\(-bin\|-doc\)\)" | grep -q "^[ih][HUFWti] " \
            && which update-grub > /dev/null ; then
        # Ensure the background image file has actually been written to disc
        # before updating.
        sync
        # Report success whatever grub return status. This is needed at least
        # in live systems for installation to succeed (see #850601).
        # Until we find a better solution like detecting we’re in a live image
        # and not calling update-grub at all.
        update-grub || echo "Updating grub failed, report success anyway!"
    fi

    if which update-initramfs > /dev/null; then
        update-initramfs -u
    fi

fi

desktop_base_version=$(dpkg-query -f'${Version}' -W desktop-base 2>/dev/null || true)
if [ "$1" = "configure" ]; then
    if dpkg --compare-versions "$desktop_base_version" lt-nl 10.0.3+kali3 || \
       dpkg --compare-versions "$2" lt-nl "2019.4.6"; then
	# Remove alternatives for the old theme

	# Background images
	while read theme filename; do
	    update-alternatives --remove \
		desktop-background \
		/usr/share/desktop-base/$theme-theme/wallpaper/contents/images/$filename $priority
	done << EOF
kali 1024x768.svg
kali 1280x720.svg
kali 1280x800.svg
kali 1280x1024.svg
kali 1600x1200.svg
kali 1920x1080.svg
kali 1920x1200.svg
kali 2560x1440.svg
kali 2560x1600.svg
kali 3840x2160.svg
EOF
	# Remove gnome-background.xml alternatives
	update-alternatives --remove \
	    desktop-background.xml \
	    /usr/share/desktop-base/kali-theme/wallpaper/gnome-background.xml

	# Remove desktop-lockscreen.xml alternatives
	update-alternatives --remove \
	    desktop-lockscreen.xml \
	    /usr/share/desktop-base/kali-theme/lockscreen/gnome-background.xml

	# Remove Plasma 5/KDE wallpaper alternatives
	update-alternatives --remove \
	    desktop-plasma5-wallpaper \
	    /usr/share/desktop-base/kali-theme/wallpaper

	# Remove login theme alternatives
	update-alternatives --remove \
	    desktop-login-background \
	    /usr/share/desktop-base/kali-theme/login/background.svg

	# Remove grub alternatives
	update-alternatives --remove \
	    desktop-grub \
	    /usr/share/desktop-base/kali-theme/grub/grub-4x3.png
	update-alternatives --remove \
	    desktop-grub \
	    /usr/share/desktop-base/kali-theme/grub/grub-16x9.png

    elif dpkg --compare-versions "$2" lt-nl "2020.2.3"; then
	# Remove invalid alternatives that moved to /usr/share/desktop-base/active-theme/*
        update-alternatives --remove \
            desktop-background \
            /usr/share/backgrounds/kali/kali-logo-16x9.png

        update-alternatives --remove \
            desktop-background.xml \
            /usr/share/backgrounds/kali/kali-logo.xml

        update-alternatives --remove \
            desktop-lockscreen.xml \
            /usr/share/backgrounds/kali/kali-light.xml

        update-alternatives --remove \
            desktop-plasma5-wallpaper \
            /usr/share/wallpapers/KaliLogo

        update-alternatives --remove \
            desktop-login-background \
            /usr/share/backgrounds/kali/kali-light.svg

    fi
fi

#DEBHELPER#
