#!/bin/bash
#
# ::copy::
# ::maintainer::
#
CMD=$(basename $0)
VERSION="1.2"
VERSIONSTRING="$CMD v$VERSION (2018/05)"

set -e
set -u

trap 'echo -e "\n*INTR*\n"; cleanup; exit 255' 1 2 3
trap 'echo -e "\nunknown error $? at $LINENO\n"; cleanup' ERR
trap 'cleanup' EXIT


[ -f /etc/default/kusa-paths ] && . /etc/default/kusa-paths
KUSA_PATH_DATADIR=${KUSA_PATH_DATADIR:-"/w"}
KUSA_PATH_WWWDIR=${KUSA_PATH_WWWDIR:-"$KUSA_PATH_DATADIR/www"}

# (FUNCTIONS)

usage()
{
	echo -e "
$VERSIONSTRING

usage:  $CMD -V|--version
	$CMD [-i|--index-only] [month(MM) [year(YYYY)]] [awstats options ...]
" >&2
	exit 1
}

cleanup()
{
	:
}



ku_log() {
	echo "$@" >&2
}



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 for copyright infos see /usr/share/doc/kusa/COPYRIGHT"
<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-@@@
}


create_awstats_details()
{
	local domain=$1
	local file=$2
	local outputs=
	local outtype=

	tag="href=\"awstats.$domain."

	outputs=$(fgrep "$tag" $file | sed -e "s/.*$tag//" -e 's/\.html".*//')

	for outtype in $outputs
	do
		outfile="awstats.$domain.$outtype.html"

		echo "  writing $outfile"

		/usr/lib/cgi-bin/awstats.pl -config=$vhost \
			-lang=it -staticlinks -output=$outtype \
			-month=$month -year=$year \
			"$@" >$outfile

		chown $user $outfile
		chmod 644 $outfile
	done
	return 0
}




# (MAIN)

confdir=/etc/awstats
vhostsdir=$(kusa-conf apache.autovirtual_root)
year=`date '+%Y'`
month=`date '+%m'`
now=`date '+%Y-%m-%d %H:%M'`
user=$(kusa-conf apache.usergroup)

customer_website=$(kusa-conf customer.website 2>/dev/null || echo "http://www.kubiclabs.com")
customer_logo="/customer-images/logo-small.png"

vhost=
conf=
outdir=
vdir=

index_only=false

[ $# != 0 ] && {
	case $1 in
	 -h|--help)		usage ;;
	 -V|--version)		echo "$VERSIONSTRING"; exit 0 ;;
	 -i|--index-only)	index_only=true; shift ;;
	esac
}

[ $# != 0 ] && {
	case $1 in
	  [0-9][0-9])	month=$1 ; shift ;;
	esac
}
[ $# != 0 ] && {
	case $1 in
	  [1-9][0-9][0-9][0-9])	year=$1 ; shift ;;
	esac
}

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

	[ -d $vdir ] || {
		ku_log " skipping $conf ($vdir) - directory not found"
		continue
	}

	ku_log " processing $conf ($vdir)"

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

	cd $odir

	$index_only || {

		echo "  writing $out -- $@"

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

		create_awstats_details $vhost $out
	}

	# rebuild index

	echo "  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=\"$customer_website\"><img src=\"$customer_logo\"
		border=\"0\" alt=\"Logo\" title=\"Main WebSite\"></a>" >>$index
	echo '  </td>'	>>$index
	echo ' </tr>'	>>$index
	echo '</table>'	>>$index




	p_year=
	close_list=
	actual=true

	for file in `ls -r stats-*.html 2>/dev/null`
	do
		name=`echo $file | sed -e 's/^stats-//' -e 's/\.html//'`
		file_year=`echo $name | sed -e 's/-.*//'`
		if $actual
		then
			echo "<p><br>" >>$index
			echo "<p><br>" >>$index
			echo "<class=\"aws\"><a href=\"$out\"><font size=\"+1\">Attuali ($name)</font></a>" >>$index
			echo "<p><br>" >>$index
			echo "<p><h2>Storico</h2>"	>>$index
			echo "<p>Nota: nelle statistiche storiche le pagine di dettaglio non sono valide" >>$index
			echo "(si riferiscono sempre alle statistiche correnti)" >>$index
			echo "<p><br>" >>$index
			actual=false
		else
			[ $file_year != "$p_year" ] && {
				echo "$close_list"		>>$index
				echo "<p><h3>$file_year</h3>"	>>$index
				echo " <ul>"			>>$index
				p_year="$file_year"
				close_list=" </ul>"
			}
			echo -n "  <li> "			>>$index
			echo "<class=\"aws\"> <a href=\"$file\">$name</a> <br>" >>$index
		fi
	done
	echo "$close_list"	>>$index

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

exit 0
