#!/bin/sh

set -e

rm_conffile() {
    PKGNAME="$1"
    CONFFILE="$2"

    if [ -e "$CONFFILE" ]; then
        md5sum="`md5sum \"$CONFFILE\" | sed -e \"s/ .*//\"`"
        old_md5sum="`dpkg-query -W -f='${Conffiles}' $PKGNAME | sed -n -e \"\\\\' $CONFFILE's/.* //p\"`"
        if [ "$md5sum" != "$old_md5sum" ]; then
            echo "Obsolete conffile $CONFFILE has been modified by you."
            echo "Saving as $CONFFILE.dpkg-old ..."
            mv -f "$CONFFILE" "$CONFFILE".dpkg-old
        else
            echo "Removing obsolete conffile $CONFFILE ..."
            rm -f "$CONFFILE"
        fi
    fi
}
# Prepare to move a conffile without triggering a dpkg question.  From
# http://wiki.debian.org/DpkgConffileHandling
prep_mv_conffile() {
    local PKGNAME="$1"
    local CONFFILE="$2"

    [ -e "$CONFFILE" ] || return 0

    local md5sum="$(md5sum $CONFFILE | sed -e 's/ .*//')"
    local old_md5sum="$(dpkg-query -W -f='${Conffiles}' $PKGNAME | \
            sed -n -e "\' $CONFFILE ' { s/ obsolete$//; s/.* //; p }")"
    if [ "$md5sum" = "$old_md5sum" ]; then
        rm -f "$CONFFILE"
    fi
}

case "$1" in
install)
    ;;
upgrade)
    if dpkg --compare-versions "$2" le "1.446~svn73270"; then
	update-rc.d -f update-hostname remove
	rm_conffile debian-edu-config /etc/init.d/update-hostname
    fi

    if dpkg --compare-versions "$2" le "1.446~svn73463"; then
	update-rc.d -f open-backdoor remove
	rm_conffile debian-edu-config /etc/init.d/open-backdoor
    fi

    ;;
esac

#DEBHELPER#
