#!/bin/sh -e

# debconf
. /usr/share/debconf/confmodule

# source dbconfig-common stuff
dbc_dbtypes="mysql, pgsql"
. /usr/share/dbconfig-common/dpkg/frontend.postinst
dbc_generate_include="php:/etc/acidbase/database.php"
dbc_generate_include_owner="root:www-data"
dbc_generate_include_perms="0640"
dbc_generate_include_args="--dbname=alert_dbname --dbpass=alert_password --dbuser=alert_user --dbserver=alert_host --dbport=alert_port --dbtype=DBtype"
dbc_go acidbase $@

CONF=/etc/acidbase/base_conf.php
[ -f $CONF ] || exit

case "$1" in
    configure)

    # Automatic apache configuration
    db_get acidbase/webserver || true
    WEBSERVER="$RET"
    case "$WEBSERVER" in
        Apache)        webservers="apache" ;;
        Apache2)       webservers="apache2" ;;
        Apache-SSL)    webservers="apache-ssl" ;;
        All)           webservers="apache apache-ssl apache2" ;;
        *)             webservers="" ;;
    esac

    for apache in $webservers; do
        if [ -d "/etc/$apache/conf.d" ] && \
           [ -x "/etc/init.d/$apache" ] && \
           [ ! -e "/etc/$apache/conf.d/acidbase.conf" ]
        then
            ln -sf /etc/acidbase/apache.conf \
                /etc/$apache/conf.d/acidbase.conf
            if [ -f "/var/run/$apache.pid" ]; then
                invoke-rc.d $apache reload
            fi
        fi
    done

    ;;
    upgrade)
    ;;
    abort-upgrade|abort-remove|abort-deconfigure)
    ;;
    *)
        echo "postinst called with unknown argument \`$1'" >&2
        exit 0
    ;;
esac

# Ensure the config file is readable by root.www-data and mode 640
# since it stores the database password
if [ ! -x /usr/sbin/dpkg-statoverride ] || \
   ! dpkg-statoverride --list $CONF > /dev/null
then
    chown root:www-data $CONF
    chmod 640 $CONF
fi


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

#DEBHELPER#

exit 0

