#!/bin/sh -e

# Uses debconf
. /usr/share/debconf/confmodule

MENUFILE="/etc/alternc/menulist.txt"
LOGAPACHE="/etc/alternc/awstats.log.alternc.conf"
APACHEROTATE="/etc/logrotate.d/apache2"

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)

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

    db_stop

    echo "Installing mysql table"
    mysql --defaults-file=/etc/alternc/my.cnf \
	</usr/share/alternc/awstats.sql || true

    # Refresh apache configuration
    # configure /etc/alternc/templates/apache/httpd.conf
    # configure /etc/alternc/templates/apache-ssl/httpd.conf

    # FIXME : ALTERNC DEPENDS ON APACHE2, SO APACHE IS NO LONGER SUPPORTED
    # REMOVE THE FOLLOWING COMMENTED LINE TO CLEAN THIS FILE

    #if [ -x /usr/sbin/apache ]; then
    #    if [ ! -h /etc/apache/conf.d/alterncawstats.conf ]; then
    #        ln -sf /etc/alternc/alterncawstats.conf \
    #            /etc/apache/conf.d/alterncawstats.conf
    #    fi
    #    # build awstats.log.alternc.conf if it does not exist
    #    if [ ! -f $LOGAPACHE ]; then
    #        cat > $LOGAPACHE <<EOF
#LogFile directive isn't important here :
#Awstats is launched with the good "LogFile" directive when called
#LogFile="/var/log/apache/access.log"
#LogFormat="%host %other %logname %time1 %methodurl %code %bytesd %refererquot %uaquot %other %virtualname"
#EOF
     #       chown alterncpanel:www-data $LOGAPACHE
     #       chmod 640 $LOGAPACHE
     #   fi
     #fi

    if [ -x /usr/sbin/apache2 ]; then
        if [ ! -h /etc/apache2/conf.d/alterncawstats.conf ]; then
            ln -sf /etc/alternc/alterncawstats.conf \
                /etc/apache2/conf.d/alterncawstats.conf
        fi
        # build awstats.log.alternc.conf if it does not exist
        if [ ! -f $LOGAPACHE ]; then
            cat > $LOGAPACHE <<EOF
#LogFile directive isn't important here :
#Awstats is launched with the good "LogFile" directive when called
LogFile="/var/log/apache2/access.log"
LogFormat="%host %other %logname %time1 %methodurl %code %bytesd %refererquot %uaquot %other %virtualname"
EOF
            chown alterncpanel:www-data $LOGAPACHE
            chmod 640 $LOGAPACHE
        fi
    fi

    if [ -x /usr/sbin/apache-ssl ]; then
      if [ ! -h /etc/apache-ssl/conf.d/alterncawstats.conf ]; then
          ln -sf /etc/alternc/alterncawstats.conf \
              /etc/apache-ssl/conf.d/alterncawstats.conf
      fi
    fi

    if [ ! -h /var/alternc/cgi-bin/awstats.pl ]; then
        ln -sf /usr/lib/cgi-bin/awstats.pl \
            /var/alternc/cgi-bin/awstats.pl
    fi
    alternc.install

    logrotate_apache

    # Add htpasswd :
    touch /etc/alternc/awstats.htpasswd
    chown alterncpanel /etc/alternc/awstats.htpasswd /etc/awstats
    chmod 640 /etc/alternc/awstats.htpasswd
    chmod 755 /etc/awstats
    mkdir -p /var/cache/awstats
    chown alterncpanel:www-data /var/cache/awstats

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

    echo "**********************************************"
    echo "*              ALTERNC-AWSTATS               *"
    echo "*  Add an autorized user called 'admin' in   *"
    echo "*   statistics section, so he will access    *"
    echo "*        every stats of every users          *"
    echo "**********************************************"
    ;;

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

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

# vim: et sw=4
