#! /bin/sh
set -e

isDigit() {
	test -n "$1" || return 1
	string="$1"
	while [ "${string#[[:digit:]]}" != "$string" ]; do
		string="${string#[[:digit:]]}"
	done
	[ -z "$string" ] || return 1
}

case "$1" in
    configure)

    	. /usr/share/debconf/confmodule

	db_get apticron/notification
	EMAIL="$RET"

	if [ ! -d /etc/apticron/ ] ; then
		mkdir /etc/apticron
	fi

	tmpfile="$( mktemp -t apticron.conf.XXXXXXXXXX )"
	chmod 0644 "$tmpfile"

	cat <<EOF > "$tmpfile"
# apticron.conf
#
# set EMAIL to a space separated list of addresses which will be notified of
# impending updates
#
EMAIL="$EMAIL"

#
# Set DIFF_ONLY to "1" to only output the difference of the current run
# compared to the last run (ie. only new upgrades since the last run). If there
# are no differences, no output/email will be generated. By default, apticron
# will output everything that needs to be upgraded.
#
# DIFF_ONLY="1"

#
# Set LISTCHANGES_PROFILE if you would like apticron to invoke apt-listchanges
# with the --profile option. You should add a corresponding profile to
# /etc/apt/listchanges.conf
#
# LISTCHANGES_PROFILE="apticron"

#
# Set SYSTEM if you would like apticron to use something other than the output
# of "hostname -f" for the system name in the mails it generates
#
# SYSTEM="foobar.example.com"

#
# Set IPADDRESSNUM if you would like to configure the maximal number of IP
# addresses apticron displays. The default is to display 1 address of each
# family type (inet, inet6), if available.
#
# IPADDRESSNUM="1"

#
# Set IPADDRESSES to a whitespace separated list of reachable addresses for
# this system. By default, apticron will try to work these out using the
# "ip" command
#
# IPADDRESSES="192.0.2.1 2001:db8:1:2:3::1"

#
# Set NOTIFY_HOLDS="0" if you don't want to be notified about new versions of
# packages on hold in your system. The default behavior is downloading and
# listing them as any other package.
#
# NOTIFY_HOLDS="0"

#
# Set NOTIFY_NEW="0" if you don't want to be notified about packages which
# are not installed in your system. Yes, it's possible! There are some issues
# related to systems which have mixed stable/unstable sources. In these cases
# apt-get will consider for example that packages with "Priority:
# required"/"Essential: yes" in unstable but not in stable should be installed,
# so they will be listed in dist-upgrade output. Please take a look at
# http://bugs.debian.org/cgi-bin/bugreport.cgi?bug=531002#44
#
# NOTIFY_NEW="0"


#
# Set CUSTOM_SUBJECT if you want to replace the default subject used in
# the notification e-mails. This may help filtering/sorting client-side e-mail.
#
# CUSTOM_SUBJECT=""
EOF

	ucf --debconf-ok --three-way "$tmpfile" /etc/apticron/apticron.conf
	rm -f "$tmpfile"

	# read time from cron.d job, if any
	if [ -f /etc/cron.d/apticron ] ; then

		time=$( grep -v '^[[:space:]]*\(\#\|$\)' /etc/cron.d/apticron \
			2>/dev/null | {
			read min hour null ||true
			if isDigit "$hour" && isDigit "$min" ; then
				echo "$hour:$min"
			fi
			} )

		if [ "$time" ] ; then
			hour="${time%%:*}"
			min="${time##*:}"
		fi
	fi

	# get random time if cron.d snippet doesn't exist or is malformed
	if ! [ "$hour" -a "$min" ] ; then
		hour=$(( $( od -vAn -N2 -tu4 < /dev/urandom ) % 24 ))
		min=$(( $( od -vAn -N2 -tu4 < /dev/urandom ) % 60 ))
	fi

	tmpfile="$( mktemp -t apticron.crond.XXXXXXXXXX )"
	chmod 0644 "$tmpfile"

	cat <<EOF >"$tmpfile"
# cron entry for apticron

$min $hour * * * root test -x /usr/sbin/apticron && /usr/sbin/apticron --cron
EOF
	ucf --debconf-ok --three-way "$tmpfile" /etc/cron.d/apticron
	rm -f "$tmpfile"

    ;;

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

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

#DEBHELPER#

exit 0
