#!/bin/sh
# postinst script for pnp4nagios-web-config-icinga
#
# 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

case "$1" in
	configure)
        echo "enabling Apache2 config..."

        COMMON_STATE=$(dpkg-query -f '${Status}' -W 'apache2.2-common' 2>/dev/null | awk '{print $3}' || true)

        # NEW method for Apache >= 2.4
        if [ -e /usr/share/apache2/apache2-maintscript-helper ]; then
            . /usr/share/apache2/apache2-maintscript-helper

            apache2_invoke enmod rewrite
            apache2_invoke enconf pnp4nagios

            # remove OLD Apache 2.2 link
            [ -L /etc/apache2/conf.d/pnp4nagios-web.conf ] && rm /etc/apache2/conf.d/pnp4nagios-web.conf

        # OLD methods for Apache < 2.4
        elif [ "$COMMON_STATE" = "installed" ] || [ "$COMMON_STATE" = "unpacked" ] ; then
            # enable mod rewrite
            [ -f /etc/apache2/mods-enabled/rewrite.load ] || a2enmod rewrite

            # create symlink if not existing
            [ -f /etc/apache2/conf.d/pnp4nagios.conf ] || ln -vs ../conf-available/pnp4nagios.conf /etc/apache2/conf.d/pnp4nagios.conf

            # reload webserver
            [ -x $(which invoke-rc.d) ] && invoke-rc.d apache2 reload
        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
