#! /bin/sh
# postinst script for amavis-stats
#
# see: dh_installdeb(1)

set -e

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


# Restart apache/apache-ssl
restartapache () {
    if /bin/ps ax | grep -v grep | grep -q apache; then
	    for apache in $webservers; do
		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
	    done
    fi
}

# Automatic Apache Configuration:
configapache () {
    for apache in $webservers; do
	conf=httpd.conf

	if [ -f "/etc/$apache/$conf" ]; then
	    if ! cat "/etc/$apache/$conf" | egrep -q '^Include.*/amavis-stats/'; then
		# No amavis-stats config/include
		echo 'Include /etc/amavis-stats/apache.conf' >> /etc/$apache/$conf
	    fi
	fi
    done

    restartapache
}

case "$1" in
    configure)
	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

        if ! id -u amavis-stats >/dev/null 2>&1; then
           adduser --quiet --system --ingroup adm \
                   --home /var/lib/amavis-stats amavis-stats
        fi

	if ! dpkg-statoverride --list /var/cache/amavis-stats 1>/dev/null 2>&1; then
            dpkg-statoverride --quiet --update --add www-data adm 0755 \
                /var/cache/amavis-stats || true
	fi

	if ! dpkg-statoverride --list /var/lib/amavis-stats 1>/dev/null 2>&1; then
            dpkg-statoverride --quiet --update --add amavis-stats adm 0755 \
                /var/lib/amavis-stats || true
	fi
#        dpkg-statoverride --quiet --update --add amavis-stats adm 0755 \
#            /var/lib/amavis-stats/img || true
    ;;

    upgrade|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


