#!/bin/sh -e

# Debian alternc-webalizer postinst

# Uses debconf
. /usr/share/debconf/confmodule
# Uses AlternC.conf
. /etc/alternc/local.sh

APACHEROTATE=/etc/logrotate.d/apache
MENUFILE=/etc/alternc/menulist.txt

# Configure logrotate.d/apache to create log owned by group www-data :
function logrotate_apache {
        cp -a -f $APACHEROTATE $APACHEROTATE.tmp
        sed -e 's/create 640 root adm/create 640 root www-data/' < $APACHEROTATE > $APACHEROTATE.tmp
        mv -f $APACHEROTATE.tmp $APACHEROTATE
}


case "$1" in
configure)

    # 1st, configure the table if required
    mysql -h"$MYSQL_HOST" -p"$MYSQL_PASS" -u"$MYSQL_USER" "$MYSQL_DATABASE" </usr/share/alternc/webalizer.sql || true

    # Then, configure the quota for "stats"
    db_get alternc-webalizer/default_quota_value || true
    /usr/lib/alternc/quota_init stats $RET

    # stop debconf so the output of dopo doesn't screw up something
    db_stop

    # Compile the po files : 
    /usr/share/alternc/install/dopo.sh

    chown www-data /var/cache/alternc-webalizer
    
    logrotate_apache

    # ADD menu item :
    
    if ! grep -q "^menu_webalizer.php$" "$MENUFILE"; then
        rm -f $MENUFILE.alternc_webalizer
        sed -e "s/menu_brouteur.php/&\\
menu_webalizer.php/" <$MENUFILE >$MENUFILE.alternc_webalizer
        mv -f $MENUFILE.alternc_webalizer $MENUFILE
    fi

    ;;

abort-upgrade)
    exit 0
    ;;

abort-remove|abort-deconfigure)
    exit 0
    ;;

*)
    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
