#!/bin/sh
# postinst script
#
# 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

# . /usr/share/apache2/apache2-maintscript-helper

# dh_installdeb will replace this with shell code automatically
# generated by other debhelper scripts.

#DEBHELPER#

case "$1" in
    configure)
	qwdctl download
	chown _qemu-web-desktop: /var/lib/qemu-web-desktop/snapshots
	# cgi include mpm_itk proxy proxy_http proxy_wstunnel rewrite
	# Enable modules only if not already enabled
	for mod in cgi include proxy proxy_http proxy_wstunnel rewrite; do
	    if ! apache2ctl -M | grep -q "[[:space:]]${mod}_module"; then
	        apache2_invoke enmod $mod
	    fi
	done

	# Switch MPM only if needed
	if ! apache2ctl -M | grep -q "[[:space:]]mpm_itk"; then
	    apache2_switch_mpm itk
	fi

	# Test config before restarting
	if apache2ctl configtest; then
	    sleep 1
	    apache2_reload restart
	else
	    echo "Apache configuration test failed. Not restarting."
	    exit 1
	fi
    ;;

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

    *)
        echo "postinst called with unknown argument \`$1'" >&2
        exit 1
    ;;
esac

exit 0
