#!/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
    # Vendor logo alternative
    update-alternatives --install \
            /usr/share/images/vendor-logos \
            vendor-logos \
            /usr/share/desktop-base/pardus-logos 50 \
            --slave /usr/share/icons/vendor/64x64/emblems/emblem-vendor.png \
                emblem-vendor-64 \
                /usr/share/icons/desktop-base/64x64/emblems/emblem-pardus.png \
            --slave /usr/share/icons/vendor/128x128/emblems/emblem-vendor.png \
                emblem-vendor-128 \
                /usr/share/icons/desktop-base/128x128/emblems/emblem-pardus.png \
            --slave /usr/share/icons/vendor/256x256/emblems/emblem-vendor.png \
                emblem-vendor-256 \
                /usr/share/icons/desktop-base/256x256/emblems/emblem-pardus.png \
            --slave /usr/share/icons/vendor/scalable/emblems/emblem-vendor.svg \
                emblem-vendor-scalable \
                /usr/share/icons/desktop-base/scalable/emblems/emblem-pardus.svg \
            --slave /usr/share/icons/vendor/64x64/emblems/emblem-vendor-symbolic.png \
                emblem-vendor-symbolic-64 \
                /usr/share/icons/desktop-base/64x64/emblems/emblem-pardus-symbolic.png \
            --slave /usr/share/icons/vendor/128x128/emblems/emblem-vendor-symbolic.png \
                emblem-vendor-symbolic-128 \
                /usr/share/icons/desktop-base/128x128/emblems/emblem-pardus-symbolic.png \
            --slave /usr/share/icons/vendor/256x256/emblems/emblem-vendor-symbolic.png \
                emblem-vendor-symbolic-256 \
                /usr/share/icons/desktop-base/256x256/emblems/emblem-pardus-symbolic.png \
            --slave /usr/share/icons/vendor/scalable/emblems/emblem-vendor-symbolic.svg \
                emblem-vendor-symbolic-scalable \
                /usr/share/icons/desktop-base/scalable/emblems/emblem-pardus-symbolic.svg \
            --slave /usr/share/icons/vendor/64x64/emblems/emblem-vendor-white.png \
                emblem-vendor-white-64 \
                /usr/share/icons/desktop-base/64x64/emblems/emblem-pardus-white.png \
            --slave /usr/share/icons/vendor/128x128/emblems/emblem-vendor-white.png \
                emblem-vendor-white-128 \
                /usr/share/icons/desktop-base/128x128/emblems/emblem-pardus-white.png \
            --slave /usr/share/icons/vendor/256x256/emblems/emblem-vendor-white.png \
                emblem-vendor-white-256 \
                /usr/share/icons/desktop-base/256x256/emblems/emblem-pardus-white.png \
            --slave /usr/share/icons/vendor/scalable/emblems/emblem-vendor-white.svg \
                emblem-vendor-white-scalable \
                /usr/share/icons/desktop-base/scalable/emblems/emblem-pardus-white.svg

    # 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
ayyildiz 80
dolunay 70
optimum 50
purple 60
runner 40
EOF

    # Use active theme as highest priority for background
    active_background=/usr/share/desktop-base/active-theme/wallpaper/contents/images/1920x1080.svg
    if [ -e ${active_background} ]; then
        update-alternatives --install \
            /usr/share/images/desktop-base/desktop-background \
            desktop-background ${active_background} 70
    fi
    # 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
ayyildiz 1920x1080.svg 85
dolunay 1920x1080.svg 75
dolunay 1600x1200.svg 75
dolunay 1280x1024.svg 75
dolunay 1920x1200.svg 75
dolunay 2560x1080.svg 75
dolunay 2560x1440.svg 75
dolunay 2560x1600.svg 75
dolunay 2560x2048.svg 75
dolunay 3840x2160.svg 75
optimum 1920x1080.svg 65
purple 1920x1080.svg 70
purple 1600x1200.svg 70
purple 1280x1024.svg 70
purple 1920x1200.svg 70
purple 2560x1080.svg 70
purple 2560x1440.svg 70
purple 3840x2160.svg 70
runner 1024x768.svg 50
runner 1280x720.svg 50
runner 1280x800.svg 50
runner 1280x1024.svg 50
runner 1600x1200.svg 50
runner 1920x1080.svg 50
runner 1920x1200.svg 50
runner 2560x1440.svg 50
runner 2560x1600.svg 50
runner 3840x2160.svg 50
EOF

    # Set up an alternative for the XML version of the background
    # (for GNOME)
    # Highest priority for active theme
    active_background_xml=/usr/share/desktop-base/active-theme/wallpaper/gnome-background.xml
    if [ -e ${active_background_xml} ]; then
        update-alternatives --install \
            /usr/share/images/desktop-base/desktop-background.xml \
            desktop-background.xml ${active_background_xml} 50
    fi
    # 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
ayyildiz 70
dolunay 60
optimum 40
purple 50
runner 30
EOF

    # Set up an alternative for the XML version of the lock screen
    # (for GNOME)
    # Highest priority for active theme
    active_lockscreen=/usr/share/desktop-base/active-theme/lockscreen/gnome-background.xml
    if [ -e ${active_lockscreen} ]; then
        update-alternatives --install \
            /usr/share/images/desktop-base/desktop-lockscreen.xml \
            desktop-lockscreen.xml ${active_lockscreen} 50
    fi
    # 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
ayyildiz 70
dolunay 60
optimum 40
purple 50
runner 30
EOF

    # Set up an alternative for the wallpaper for Plasma 5/KDE
    # Highest priority for active theme
    active_plasma_wallpaper=/usr/share/desktop-base/active-theme/wallpaper
    if [ -e ${active_plasma_wallpaper} ]; then
        update-alternatives --install \
            /usr/share/wallpapers/PardusTheme \
            desktop-plasma5-wallpaper ${active_plasma_wallpaper} 50
    fi
    # Alternatives for theme packages
    while read theme priority; do
        update-alternatives --install \
            /usr/share/wallpapers/PardusTheme \
            desktop-plasma5-wallpaper \
            /usr/share/desktop-base/$theme-theme/wallpaper $priority
    done << EOF
ayyildiz 70
dolunay 60
optimum 40
purple 50
runner 30
EOF

    # Login theme
    # Highest priority for active theme
    active_login_background=/usr/share/desktop-base/active-theme/login/background.svg
    if [ -e ${active_login_background} ]; then
        update-alternatives --install /usr/share/images/desktop-base/login-background.svg \
            desktop-login-background ${active_login_background} 50
    fi
    # 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
ayyildiz background.svg 70
dolunay background.svg 60
optimum background.svg 40
purple background.svg 50
runner background.svg 30
EOF

    # 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_background=/usr/share/desktop-base/active-theme/grub/grub_background.sh
    active_grub_first_ratio=/usr/share/desktop-base/active-theme/grub/grub-${grub_first_ratio}.png
    if [ -e ${active_grub_first_ratio} ] && [ -e ${active_grub_background} ]; then
        update-alternatives --install \
            /usr/share/images/desktop-base/desktop-grub.png \
            desktop-grub ${active_grub_first_ratio} 50 \
            --slave /usr/share/desktop-base/grub_background.sh \
            desktop-grub.sh ${active_grub_background}
    fi
    active_grub_second_ratio=/usr/share/desktop-base/active-theme/grub/grub-${grub_second_ratio}.png
    if [ -e ${active_grub_second_ratio} ] && [ -e ${active_grub_background} ]; then
        update-alternatives --install \
            /usr/share/images/desktop-base/desktop-grub.png \
            desktop-grub ${active_grub_second_ratio} 45 \
            --slave /usr/share/desktop-base/grub_background.sh \
            desktop-grub.sh ${active_grub_background}
    fi
    # Alternatives for theme packages
    while read theme ratio priority; do
        update-alternatives --install /usr/share/images/desktop-base/desktop-grub.png \
            desktop-grub \
            /usr/share/desktop-base/$theme-theme/grub/grub-$ratio.png $priority \
            --slave /usr/share/desktop-base/grub_background.sh \
            desktop-grub.sh \
            /usr/share/desktop-base/$theme-theme/grub/grub_background.sh
    done << EOF
ayyildiz 4x3 70
ayyildiz 16x9 70
dolunay 4x3 60
dolunay 16x9 60
optimum 4x3 40
optimum 16x9 40
purple 4x3 50
purple 16x9 50
runner 4x3 30
runner 16x9 30
EOF

    # 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 fon 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

# Set purple as default ayyildiz theme
if [ $(plymouth-set-default-theme) != "ayyildiz"  ]; then
    plymouth-set-default-theme -R ayyildiz
fi

plymouth-set-default-theme -R bgrt
