#!/bin/bash -e

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

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

    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
	fi
        chown alterncpanel:root $LOGAPACHE
        chmod 644 $LOGAPACHE
    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

    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
    chmod 770 /var/cache/awstats

    # Update rights on previous user's awstats configuration files
    chown alterncpanel:root /etc/awstats/awstats.*.conf > /dev/null 2>&1 || true

    echo -e "\033[31m**********************************************"
    echo "*  ALTERNC-AWSTATS   ACTION REQUESTED        *"
    echo "* If you add an autorized user called 'admin'*"
    echo "* in statistics section, he will access      *"
    echo "*        every stats of every users          *"
    echo "*                                            *"
    echo "* Please run alternc.install to fully deploy *"
    echo "**********************************************"
    echo -e "\033[0m"
    ;;

  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
