#!/bin/sh

set -e

FILES_TO_DIVERT="
/etc/lightdm/lightdm-gtk-greeter.conf
/etc/plymouth/plymouthd.conf
/etc/xdg/qterminal.org/qterminal.ini
/etc/xdg/xfce4/panel/default.xml
/etc/xdg/xfce4/terminal/terminalrc
/etc/xdg/xfce4/whiskermenu/defaults.rc
/etc/xdg/xfce4/xfconf/xfce-perchannel-xml/xfce4-desktop.xml
/etc/xdg/xfce4/xfconf/xfce-perchannel-xml/xfce4-keyboard-shortcuts.xml
/etc/xdg/xfce4/xfconf/xfce-perchannel-xml/xfce4-power-manager.xml
/etc/xdg/xfce4/xfconf/xfce-perchannel-xml/xfwm4.xml
/etc/xdg/xfce4/xfconf/xfce-perchannel-xml/xsettings.xml
" # END FILES_TO_DIVERT

setup_diversion_on_upgrade() {
    local prev_version="$1"
    local ref_version="$2"
    local file="$3"
    if dpkg --compare-versions "$prev_version" lt "$ref_version"; then
	setup_diversion "$file"
    fi
}

setup_diversion() {
    local file="$1"
    dpkg-divert --rename --package kali-themes \
		--divert "$file.original" \
		--add "$file"
}

case "$1" in
    install)
	# Handle upgrade from kali-defaults
	DIVERT_FILE="/etc/xdg/xfce4/xfconf/xfce-perchannel-xml/xsettings.xml"
	if [ "$(dpkg-divert --listpackage $DIVERT_FILE)" = "kali-defaults" ]; then
	    rm -f $DIVERT_FILE
	    dpkg-divert --rename --package kali-defaults \
		--divert $DIVERT_FILE.original \
                --remove $DIVERT_FILE
	fi
	# Setup all the diversions
	for file in $FILES_TO_DIVERT; do
	    setup_diversion "$file"
	done
    ;;
    upgrade)
	# Dynamically add new diversions when required on upgrade
	# Example to divert /etc/xdg/xfce4/panel/default.xml when you
	# upgrade from a version older than 2019.4.6:
	# setup_diversion_on_upgrade "$2" 2019.4.6 /etc/xdg/xfce4/panel/default.xml
	setup_diversion_on_upgrade "$2" 2019.4.9 /etc/plymouth/plymouthd.conf
    ;;
esac

#DEBHELPER#
