#!/bin/sh
# postinst script for z-push-config-apache
#
# see: dh_installdeb(1)

set -e

if type a2enconf >/dev/null 2>/dev/null; then
  # FIXME: configure case below pulls symlinks from conf-available manually.
  a2enconf z-push || true
  service apache2 reload || true
fi

reload_apache()
{
    if apache2ctl configtest 2>/dev/null; then
    if [ -x /usr/sbin/invoke-rc.d ]; then
            invoke-rc.d apache2 $1 3>/dev/null || true
    else
            /etc/init.d/apache2 $1 3>/dev/null || true
    fi
    else
        echo "Your Apache 2 configuration is broken, so we're not restarting it for you."
    fi
}

set_perms() {
        USER=$1
        GROUP=$2
        MODE=$3
        FILE=$4
        if ! dpkg-statoverride --list $FILE > /dev/null 2>&1; then
                chown -R $USER:$GROUP $FILE
                chmod -R $MODE $FILE
        fi
}


case "$1" in
    configure)

    #Set permissions
    set_perms www-data www-data 700 /var/lib/z-push
    set_perms www-data www-data 700 /var/log/z-push

    CONF="z-push"
    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
        if [ -e /etc/apache2/conf-available/$CONF.conf ] ; then
            apache2_invoke enconf $CONF || exit $?
        fi
    elif [ "$COMMON_STATE" = "installed" ] || [ "$COMMON_STATE" = "unpacked" ] ; then
        if [ -d /etc/apache2/conf.d/ ] && [ ! -L /etc/apache2/conf.d/$CONF.conf ] ; then
            ln -s ../conf-available/$CONF.conf /etc/apache2/conf.d/$CONF.conf
        fi
        # Test whether a2enmod is available (and thus also apache2ctl).
        if [ -x /usr/sbin/a2enmod ]; then
            # Restart Apache to really enable the module and load
            # /etc/apache2/conf.d/z-push.conf.
            reload_apache reload
        fi
    fi
    ;;

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

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

#DEBHELPER#

exit 0
