#!/bin/sh
# postinst script for debathena-moira-update-server
#
# see: dh_installdeb(1)

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>
#        * <postinst> `abort-remove'
#        * <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

# Move a conffile without triggering a dpkg question
mv_conffile() {
    OLDCONFFILE="$1"
    NEWCONFFILE="$2"

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

    echo "Preserving user changes to $NEWCONFFILE ..."
    mv -f "$NEWCONFFILE" "$NEWCONFFILE".dpkg-new
    mv -f "$OLDCONFFILE" "$NEWCONFFILE"
}

case "$1" in
    configure)
	if dpkg --compare-versions "$2" lt 4.0.0+svn20100104-0debathena1~; then
	    mv_conffile /etc/athena/moira.conf /etc/moira.conf

	    # If that empties out /etc/athena, then delete it
	    rmdir --ignore-fail-on-non-empty /etc/athena
	fi
	if dpkg --compare-versions "$2" lt 4.0.0+svn20100405-0debathena2~; then
	    # If they're upgrading from a version that pre-dates
	    # the defaults file, then they probably had it installed for
	    # a reason, so we should turn it on (if it's not a cluster machine)
	    if ! dpkg-query --showformat '${Status}\n' -W "debathena-cluster" 2>/dev/null | grep -q ' installed$'; then
		sed -i -e 's/^ENABLED=.*$/ENABLED=true/' /etc/default/debathena-moira-update-server
	    fi
	fi
    ;;

    abort-upgrade|abort-remove|abort-deconfigure)
    ;;

    *)
        echo "postinst called with unknown argument \`$1'" >&2
        exit 1
    ;;
esac

# dh_installdeb will replace this with shell code automatically
# generated by other debhelper scripts.

#DEBHELPER#

exit 0


