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

# Source debconf library.
. /usr/share/debconf/confmodule

# Mostly copied from exim4-config's postinst
writealiases() {
    echo '# /etc/aliases' > /etc/aliases.tmp
    echo '# Created by debathena-msmtp-mta postinst' > /etc/aliases.tmp
    echo 'mailer-daemon: postmaster' >> /etc/aliases.tmp
    for i in postmaster nobody hostmaster usenet news webmaster www ftp abuse noc security cron; do
        echo "${i}: root"
    done >> /etc/aliases.tmp
    if [ -n "$1" ]; then
	echo "root: $1" >> /etc/aliases.tmp
    fi
    mv /etc/aliases.tmp /etc/aliases
}

case "$1" in
    configure)
	if [ ! -e /etc/aliases ]; then
	    db_get debathena-msmtp-config/root_addr
	    writealiases "$RET"
	else
	    # Whine at the user if it looks like root's mail will
	    # end up somewhere local
	    if ! expand-alias root | grep -q @; then
		echo "WARNING: Check /etc/aliases to ensure root's mail goes somewhere useful." >&2
	    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
