#!/bin/bash
#
# __copy1__
# __copy2__
#
CMD=`basename $0`

[ -f /etc/default/kusa-paths ] && . /etc/default/kusa-paths

KUSA_PATH_WWWDIR=${KUSA_PATH_WWWDIR:-"/w/www"}
vhosts_dir="$KUSA_PATH_WWWDIR/vhosts"

[ -f /etc/apache2/envvars ] && . /etc/apache2/envvars

APACHE_RUN_USER=${APACHE_RUN_USER:-"www-data"}
APACHE_RUN_GROUP=${APACHE_RUN_GROUP:-"www-data"}
usergroup="${APACHE_RUN_USER}:${APACHE_RUN_GROUP}"

[ -f /etc/default/awstats ] && . /etc/default/awstats

CONFDIR=${CONFDIR:-"/etc/awstats"}
PROVIDER_HOME=${PROVIDE_HOME:-"http://www.kubiclabs.com"}
PROVIDER_LOGO={$PROVIDER_LOGO:-"/customer-images/logo-small.png"}

# lib functions

. /lib/ku-base/lock.sh
. /lib/ku-base/log.sh
. /lib/ku-base/libfiles.sh

LOGSYSLOG="false"
TOOLKIT="__TOOLKIT__"


# (FUNCTIONS)


put_head() {
	local host=$1

	cat <<@@@-eof-head-@@@
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html lang='it'>
<head>
<meta name="generator" content="$CMD $VERSION (KUBiC Labs, CH)">
<meta name="robots" content="noindex,nofollow">
<meta http-equiv="content-type" content="text/html; charset=iso-8859-1">
<meta http-equiv="description" content="web statistics index page for $host">
<title>Statistics Index for $host</title>
<style type="text/css">
<!--
body { font: 11px verdana, arial, helvetica, sans-serif; background-color: #FFFFFF; margin-top: 0; margin-bottom: 0; }
.aws_bodyl  { }
.aws_border { border-collapse: collapse; background-color: #CCCCDD; padding: 1px 1px 1px 1px; margin-top: 0px; margin-bottom: 0px; }
.aws_title  { font: 13px verdana, arial, helvetica, sans-serif; font-weight: bold; background-color: #CCCCDD; text-align: center; margin-top: 0; margin-bottom: 0; padding: 1px 1px 1px 1px; color: #000000; }
.aws_blank  { font: 13px verdana, arial, helvetica, sans-serif; background-color: #FFFFFF; text-align: center; margin-bottom: 0; padding: 1px 1px 1px 1px; }
.aws_data {
	background-color: #FFFFFF;
	border-top-width: 1px;   
	border-left-width: 0px;  
	border-right-width: 0px; 
	border-bottom-width: 0px;
}
.aws_formfield { font: 13px verdana, arial, helvetica; }
.aws_button {
	font-family: arial,verdana,helvetica, sans-serif;
	font-size: 12px;
	border: 1px solid #ccd7e0;
	background-image : url(/awstats-icon/other/button.gif);
}
th		{ border-color: #ECECEC; border-left-width: 0px; border-right-width: 1px; border-top-width: 0px; border-bottom-width: 1px; padding: 1px 2px 1px 1px; font: 11px verdana, arial, helvetica, sans-serif; text-align:center; color: #000000; }
th.aws	{ border-color: #ECECEC; border-left-width: 0px; border-right-width: 1px; border-top-width: 0px; border-bottom-width: 1px; padding: 1px 2px 1px 1px; font-size: 13px; font-weight: bold; }
td		{ border-color: #ECECEC; border-left-width: 0px; border-right-width: 1px; border-top-width: 0px; border-bottom-width: 1px; font: 11px verdana, arial, helvetica, sans-serif; text-align:center; color: #000000; }
td.aws	{ border-color: #ECECEC; border-left-width: 0px; border-right-width: 1px; border-top-width: 0px; border-bottom-width: 1px; font: 11px verdana, arial, helvetica, sans-serif; text-align:left; color: #000000; padding: 0px;}
td.awsm	{ border-left-width: 0px; border-right-width: 0px; border-top-width: 0px; border-bottom-width: 0px; font: 11px verdana, arial, helvetica, sans-serif; text-align:left; color: #000000; padding: 0px; }
b { font-weight: bold; }
a { font: 11px verdana, arial, helvetica, sans-serif; }
a:link    { color: #0011BB; text-decoration: none; }
a:visited { color: #0011BB; text-decoration: none; }
a:hover   { color: #605040; text-decoration: underline; }
.currentday { font-weight: bold; }
//-->
</style>
</head>

<body style="margin-top: 0px">
<a name="top">&nbsp;</a>
@@@-eof-head-@@@
}




# (MAIN)

month=`date '+%Y-%m'`
now=`date '+%Y-%m-%d %H:%M'`

vhost=
conf=
outdir=
vdir=


for conf in `cd $CONFDIR ; ls *.conf 2>/dev/null`
do
	vhost=`echo $conf | sed -e 's/^awstats\.//' -e 's/\.conf$//'`
	vdir="$vhosts_dir/$vhost"
	odir="$vdir/docs/stats"
	out="stats-$month.html"
	index="index.html"
	datadir="$vdir/awstats-data"

	[ -d $vdir ] && {
		kl_log " processing $conf ($vdir)"

		[ -d $datadir ] || {
			kl_log "  creating dir $datadir"
			mkdir $datadir || exit $?
			chown $usergroup $datadir || exit $?
			chmod 755 $datadir || exit $?
		}
		[ -d $odir ] || {
			kl_log "  creating dir $datadir"
			mkdir $odir || exit $?
			chown $usergroup $odir || exit $?
			chmod 755 $odir || exit $?
		}

		cd $odir

		/usr/lib/cgi-bin/awstats.pl -config=$vhost \
			-lang=it -update -staticlinks -output >$out

		# rebuild index

		put_head $vhost >$index

		echo '<table class="aws_border" border="0" cellpadding="4" cellspacing="0" width="100%">' >>$index
		echo ' <tr>'	>>$index
		echo '  <td class="aws" valign="middle">' >>$index
		echo "   <class=\"aws\"> <h2>&nbsp;Statistiche mensili per $vhost (aggiornate al $now) </h2>" >>$index
		echo '  </td>'	>>$index
		echo '  <td class="aws" align="right">' >>$index
		echo "   <class=\"aws\"><a href=\"$PROVIDER_HOME\"><img src=\"$PROVIDER_LOGO\"
			border=\"0\" alt=\"Provider Logo\" title=\"Provider Site\"></a>" >>$index
		echo '  </td>'	>>$index
		echo ' </tr>'	>>$index
		echo '</table>'	>>$index


		echo "<p>" >>$index

		p_year=
		close_list=

		for file in `ls stats-*.html 2>/dev/null`
		do
			name=`echo $file | sed -e 's/^stats-//' -e 's/\.html//'`
			year=`echo $name | sed -e 's/-.*//'`
			[ $year != "$p_year" ] && {
				echo "$close_list"		>>$index
				echo "<p><h3>$year</h3>"	>>$index
				echo " <ul>"			>>$index
				p_year="$year"
				close_list=" </ul>"
			}
			echo -n "  <li> "			>>$index
			echo "<class=\"aws\"> <a href=\"$file\">$name</a> <br>" >>$index
		done
		echo "$close_list"	>>$index

		echo "</body></html>"	>>$index
		chown $usergroup $out $index
		chmod 644 $out $index
	}
done

exit 0
