#!/bin/sh -e
set -e


if dpkg-maintscript-helper supports rm_conffile 2>/dev/null; then
  dpkg-maintscript-helper rm_conffile /etc/kde3/kdeglobals 6.0.1 -- "$@"
fi


scrub_alternatives (){ 
    #tidy up any leftovers
    local alt
    for alt in $( update-alternatives --list $1 ); do
        echo "remove alternatives $alt from $1"
    	case $alt in
	    	"/usr/share/images/desktop-base/*")
		    	update-alternatives --remove $1 $alt
	    		;;
		    "/usr/share/desktop-base/*")
			    #remove all alternatives for obsolete desktop-grub.
    			update-alternatives --remove-all $alt 
	    		;;
	        *)
                if [ $1 = 'grub-background' ] || [ $1 = 'grub-theme' ] ; then
                    update-alternatives --remove-all $1 
                fi
			    ;;
    	esac
    done
}



if [ "${1}" = "upgrade" ]; then
    #tidy up any leftovers
    for alternatives in desktop-grub desktop-background desktop-background.xml desktop-splash grub-theme desktop-grub-theme; do
        if [ -h /etc/alternatives/$alternatives ]; then
            echo "Cleaning up alternatives for $alternatives"
	        scrub_alternatives $alternatives
        else
            echo "No alternatives for $alternatives to clean up"
        fi
    done

fi

case "$1" in
		   # divert stubborn xfce background
  install|upgrade) if [ -e /usr/share/backgrounds/xfce/xfce-x.svg ] ; then
			dpkg-divert --add --package desktop-base  --rename \
			--divert /usr/share/backgrounds/xfce/xfce-x.svg.distrib \
			/usr/share/backgrounds/xfce/xfce-x.svg
			ln -s /etc/alternatives/desktop-background /usr/share/backgrounds/xfce/xfce-x.svg
		   fi
  ;;
esac
