#!/bin/sh
# postinst script for debathena-auto-update
#
# 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

desync2cron() {
    hour=$1
    interval=$2
    shift 2
    desync=`desync -n $interval`
    mins=`echo $desync % 60 | bc`
    hours=`echo "$hour + ($desync / 60)" | bc`
    if [ $hours -ge 24 ]; then
	hours=`echo $hours - 24 | bc`
    fi
    echo "$mins $hours * * * $*"
}

UPD_CRONFILE=/etc/cron.d/athena-auto-update
UPG_CRONFILE=/etc/cron.d/athena-auto-upgrade

# We stopped shipping our own cron.d file in 1.21, so remove it
# dpkg-maintscript-helper exists in maverick+
if dpkg-maintscript-helper supports rm_conffile 2> /dev/null; then
    dpkg-maintscript-helper rm_conffile \
	/etc/cron.d/debathena-auto-update 1.22.2 -- "$@"
else
    # From http://wiki.debian.org/DpkgConffileHandling
    # Which is full of lies, like the fact that postinsts get 
    # called with "install" or "upgrade" when they totally don't
    rm_conffile() {
	local PKGNAME="$1" # Unused
	local CONFFILE="$2"
	if [ -f "$CONFFILE".dpkg-del ]; then
            rm -f "$CONFFILE".dpkg-del
	fi
    }
    case "$1" in
	configure)
	    if dpkg --compare-versions "$2" le-nl 1.22.2; then
		rm_conffile debathena-auto-update "/etc/cron.d/debathena-auto-update"
	    fi
    esac
fi

case "$1" in
    configure)
	# Nuke /var/lib/athena-update-status on 1.18, 
	# since we changed its format. It will be created 
	# at the next auto update
	if dpkg --compare-versions "$2" lt 1.18~ &&
	    [ -f /var/lib/athena-update-status ]; then
	    rm /var/lib/athena-update-status
	fi
	
        if [ ! -f /var/lib/athena-update-status ]; then
	    echo "$(date +"%s")|$(date +"%s")|ok|Package configured" > /var/lib/athena-update-status
        fi

	rm -f $UPD_CRONFILE
	rm -f $UPG_CRONFILE

	echo "# Automatically generated by debathena-auto-update postinst" > $UPD_CRONFILE
	echo "SHELL=/bin/sh" >> $UPD_CRONFILE
	echo "PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin" >> $UPD_CRONFILE

	for h in 2 4 6; do
	    desync2cron $h 120 root /usr/sbin/athena-auto-update cron >> $UPD_CRONFILE
	done
	for h in 8 14 20; do
	    desync2cron $h 360 root /usr/sbin/athena-auto-update cron >> $UPD_CRONFILE
	done
	echo "# Automatically generated by debathena-auto-update postinst" > $UPG_CRONFILE
	echo "SHELL=/bin/sh" >> $UPG_CRONFILE
	echo "PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin" >> $UPG_CRONFILE

	upginterval=360
	hostname=`hostname | tr 'A-Z' 'a-z'`
	case $hostname in
	    m38-370*)
		upginterval=480
		;;
	    w20-575*)
		upginterval=420
		;;
	esac
	for h in 1 2 3; do
	    desync2cron $h $upginterval root /usr/sbin/athena-auto-upgrade >> $UPG_CRONFILE
	done
	
    ;;

    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
