#!/bin/sh
# postrm script for netsaint
#
# see: dh_installdeb(1)

set -e

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

# summary of how this script can be called:
#        * <postrm> `remove'
#        * <postrm> `purge'
#        * <old-postrm> `upgrade' <new-version>
#        * <new-postrm> `failed-upgrade' <old-version>
#        * <new-postrm> `abort-install'
#        * <new-postrm> `abort-install' <old-version>
#        * <new-postrm> `abort-upgrade' <old-version>
#        * <disappearer's-postrm> `disappear' <r>overwrit>r> <new-version>
# for details, see /usr/doc/packaging-manual/

configapache () {
    for apache in $webservers; do
	conf=httpd.conf

	if [ -f "/etc/$apache/$conf" ] ; then
	    # Remove from httpd.conf (current method)
	    perl -pe 's/Include \/etc\/amavis-stats\/apache.conf//s;' \
		< /etc/$apache/$conf > /etc/$apache/$conf-dpkg.amavis-stats
	    mv /etc/$apache/$conf-dpkg.amavis-stats /etc/$apache/$conf
	fi
    done

    if /bin/ps ax | grep -v grep | grep -q $apache; then
	if [ -x "/etc/init.d/$apache" ]; then
	    set +e ; invoke-rc.d $apache  reload ; set -e
	fi
    fi
}

case "$1" in
    remove|abort-install|abort-upgrade|failed-upgrade|disappear)
	# Remove apache config if wanted.
	if [ -f /usr/share/debconf/confmodule ]; then
		. /usr/share/debconf/confmodule
		db_get amavis-stats/config_apache
	    	webserver="$RET"
	    	case "$webserver" in
	    		Apache)        webservers="apache";;
    			Apache-SSL)    webservers="apache-ssl";;
	        	Apache2)       webservers="apache2";;
	    		Both)          webservers="apache apache-ssl";;
    			*)             webservers="";;
    		esac

		if [ ! -z "$webservers" ]; then
			configapache
		fi
	fi
	;;
    purge)
	dpkg-statoverride --remove /var/lib/amavis-stats
	dpkg-statoverride --remove /var/cache/amavis-stats

	if [ -f /usr/share/debconf/confmodule ]; then
		db_get amavis-stats/stay_on_purge
		if [ "$RET" = "true" ]; then
			rm -rf /var/lib/amavis-stats
			rm -rf /var/cache/amavis-stats
		fi
	fi

	getent passwd amavis-stats >/dev/null && deluser amavis-stats
    	;;	
    upgrade)
	;;
    *)
        echo "postrm called with unknown argument \`$1'" >&2
        exit 0
esac

#DEBHELPER#

exit 0
