#!/bin/sh -e

CONFIGFILE="/etc/alternc/local.sh"
MENUFILE="/etc/alternc/menulist.txt"

# Remove awstats include to the apache configuration file given as argument
function unconfigure {
    if grep -Eqs "\*\*\*AWSTATS\*\*\*" $1; then
	cp -a -f $1 $1.alternc_awstats
	cat $1 | grep -v "\*\*\*MAILMAN\*\*\*" |
	    grep -v "	Include \"\/etc\/apache\/alternc-awstats.conf\"/" \
	    > $1.alternc_awstats
	mv -f $1.alternc_awstats $1 
    fi
}

case "$1" in
  remove)
    # Remove Awstats references from Apache configuration
    unconfigure /etc/alternc/templates/apache/httpd.conf
    unconfigure /etc/alternc/templates/apache-ssl/httpd.conf
    alternc.install

    if grep -qs "menu_aws.php" $MENUFILE; then
	rm -f $MENUFILE.alternc_awstats
	cat $MENUFILE | grep -v "menu_aws.php" >$MENUFILE.alternc_awstats
        mv -f $MENUFILE.alternc_awstats $MENUFILE
    fi
    ;;
  purge)
    /usr/lib/alternc/quota_delete aws
    if [ -e "$CONFIGFILE" -a -x "/usr/bin/mysql" ]; then
        . "$CONFIGFILE"
        mysql -f -u"$MYSQL_USER" -p"$MYSQL_PASS" -h"$MYSQL_HOST" \
              "$MYSQL_DATABASE" -e "DROP TABLE IF EXISTS aws,aws_users,aws_access;"
    fi
    rm -rf /var/cache/awstats 
    rm -f /etc/alternc/awstats.htpasswd 
    ;;
esac

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