#!/bin/sh -e
# Post-installation script for greylistd.
# see: dh_installdeb(1)
#
# 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>
#        * <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

. /usr/share/debconf/confmodule                                         
db_version 2.0

owner=greylistd
initscript=/etc/init.d/greylist


running_exim4()
{
    test -x /usr/sbin/exim4 -a -d /etc/exim4
}



copyfile ()
{
    if [ -f "$1" -a '!' -f "$2" ]
    then
	echo -n "Copying $1 to ${2%/*}: "
	cp -f "$1" "$2" && echo " Done."
    fi
}


movefile ()
{
    if [ -f "$1" -a '!' -f "$2" ]
    then
	echo -n "Moving $1 to $2: "
	mv -f "$1" "$2" && echo " Done."
    fi
}


removefile ()
{
    if [ -f "$1" ]
    then
	echo -n "Removing $1:"
	rm -f "$1" && echo " Done."
    fi
}


set_config_value()
{
    file=$1
    option=$2
    value=$3

    text=$(sed "s/\($option[[:space:]]*=[[:space:]]*\).*/\1$value/" "$file") &&
        echo "$text" > "$file"
}


add_user ()
{
    user=$1
    group=$2

    for membership in $(id -Gn "$user" 2>/dev/null)
    do
        [ "$membership" = "$group" ] && return 1
    done

    adduser "$user" "$group"
    return 0
}


do_configure()
{
    oldversion=$1

    username=greylist
    groupname=greylist
    datadir=/var/lib/greylistd
    rundir=/var/run/greylistd
    docdir=/usr/share/doc/greylistd
    confdir=/etc/greylistd

    ### The following keeps "make" silent during adduser/deluser operations
    ### on YP/NIS systems.
    export MAKEFLAGS
    MAKEFLAGS=-s

    ### If the user does not already exist, create it.
    id -u "$username" >/dev/null 2>&1 ||
	adduser --system --group --disabled-password \
	    --home "$datadir" --no-create-home "$username"


    ### If we are using Exim 4, and the Debian-exim user is not in the
    ### group, add it and then restart Exim.
    if running_exim4 && add_user Debian-exim "$groupname"
    then
        db_get "$owner/restartexim"
	$RET && invoke-rc.d exim4 restart

    ### Otherwise, if we are updating from 0.7 or prior versions, we add
    ### the greylist user from the "ugid" DebConf setting
    elif dpkg --compare-versions "$oldversion" le "0.7" && db_get "$owner/ugid"
    then
	uid="${RET%:*}"
	add_user "$uid" "$groupname" || true

    fi

    ### Create working directories
    for dir in "$datadir" "$rundir"
    do
        if [ ! -d "$dir" ]
	then
	    echo -n "Creating directory $dir:"
	    mkdir "$dir" &&
	    echo " Done."
	fi
	chown -R "$username:$groupname" "$dir"
    done

    removefile /etc/default/greylistd
    removefile /etc/default/greylist
    movefile   "$datadir/data" "$datadir/states"
    copyfile   "$docdir/examples/whitelist-hosts" "$confdir/whitelist-hosts"

}


case "$1" in 
    configure|reconfigure)
	do_configure $2
	;;

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

    *)
        echo "greylistd.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
