#! /bin/sh
# postinst maintainer script for zoneminder-ui-base package

set -e

# Source the debconf stuff
. /usr/share/debconf/confmodule

apache_install() {

    mkdir -p /etc/apache2/conf-available
    ln -sf ../../zm/apache.conf /etc/apache2/conf-available/zoneminder.conf

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

    if [ -e /usr/share/apache2/apache2-maintscript-helper ] ; then
        . /usr/share/apache2/apache2-maintscript-helper
        apache2_invoke enconf zoneminder
    elif [ "$COMMON_STATE" = "installed" ] || [ "$COMMON_STATE" = "unpacked" ] ; then
        [ -d /etc/apache2/conf.d/ ] && [ ! -L /etc/apache2/conf.d/zoneminder.conf ] && ln -s ../conf-available/zoneminder.conf /etc/apache2/conf.d/zoneminder.conf
    fi

    # Enable CGI script module in apache (not enabled by default on jessie)
    a2enmod cgi >/dev/null 2>&1

    # Reload the web server
    deb-systemd-invoke reload apache2.service || true
}

# Do this when the package is installed, upgraded or reconfigured
if [ "$1" = "configure" ] || [ "$1" = "reconfigure" ]; then

    # Configure the web server
    db_get zoneminder/webserver
    webservers="$RET"

    for webserver in $webservers; do
        webserver=${webserver%,}
        # Currently we only support apache2
        if [ "$webserver" = "apache2" ] ; then
            apache_install $1
        fi
    done
fi

#DEBHELPER#

exit 0
