#!/bin/bash

set -e

# summary of how this script can be called:
#        * <postinst> `configure' <most-recently-configured-version>
#        * <old-postinst> `abort-upgrade' <new version>
#        * <conflictor's-postinst> `abort-remove' `in-favour' <package>
#          <new-version>
#        * <deconfigured's-postinst> `abort-deconfigure' `in-favour'
#          <failed-install-package> <version> `removing'
#          <conflicting-package> <version>
# for details, see http://www.debian.org/doc/debian-policy/ or
# the debian-policy package
#

# Function to remove all diversions of a package
function remove_divert {
    if [ ! -z "$1" ]; then
        for CMD in $(LANG=C dpkg-divert --list "$1" | awk '{print $3}'); do
            DIVERT=$(LANG=C dpkg-divert --list "$CMD" | awk '{print $5}')
            if [ -e "$DIVERT" ]; then
                if [ -e "$CMD" ]; then
                    mv "$CMD" "$CMD.bak"
                fi
                cp "$DIVERT" "$DIVERT.bak"
                dpkg-divert --remove --rename "$CMD"
                if [ ! -e "$CMD" ]; then
                    mv "$DIVERT.bak" "$CMD"
                else
                    if [ -e "$DIVERT.bak" ]; then
                        rm "$DIVERT.bak"
                    fi
                     if [ -e "$CMD.bak" ]; then
                        rm "$CMD.bak"
                    fi
                fi
            else
                dpkg-divert --remove "$CMD"
            fi
        done
    fi
}

case "$1" in
    remove|purge)
    # Remove diversions
    remove_divert 'chromium-solydxk-adjustments'
    ;;
    *)
    ;;
esac

#DEBHELPER#

exit 0
