#! /bin/sh

. /usr/local/etc/tas/accounting.conf

# Providing default values for config parameters:
recipient=${recipient:-root}
storage=${storage:-/usr/local/www/data/acct}
prefix=${prefix:-/var/account}
process_ip=${process_ip:-YES}
process_squid=${process_squid:-YES}
process_sendmail=${process_sendmail:-YES}
process_mailgate=${process_mailgate:-NO}
ip_daily_max=${ip_daily_max:-YES}
report_type=${report_type:-html}
if [ "X$process_squid" = "XYES" ] ; then squid_logs=${squid_logs:?} ; fi


insuffix=yesterday
monthsuffix=month

year=`date -v-1d '+%Y'`
month=`date -v-1d '+%b'`
day=`date -v-1d '+%d'`
if [ "X$report_type" = "Xhtml" ]
then
	storage_file=$storage/$year/$month/$day.html
	opt_h="-h"
else
	storage_file=$storage/$year/$month/$day.txt
	opt_h=
fi

ip_in_base=$prefix/ip.$insuffix
ip_month_base=$prefix/ip.$monthsuffix
squid_in_base=$prefix/squid.$insuffix
squid_month_base=$prefix/squid.$monthsuffix
sendmail_in_base=$prefix/sendmail.$insuffix
sendmail_month_base=$prefix/sendmail.$monthsuffix
mailgate_in_base=$prefix/mailgate.$insuffix
mailgate_month_base=$prefix/mailgate.$monthsuffix

echodate () {
	if [ "X$report_type" = "Xhtml" ]
	then
		echo "<BR>"
	fi
	echo "$@" "at" `date`
}

notify_shifted_found () {
	if [ "X$report_type" = "Xhtml" ]
	then
		echo "<P><BR><P><H3>PREVIOUSLY SHIFTED FILE $1 FOUND! PROCESSING IT...</H3>"
	else
		echo
		echo "PREVIOUSLY SHIFTED FILE $1 FOUND! PROCESSING IT..."
		echo
	fi
}

notify_shifted_done () {
	if [ "X$report_type" = "Xhtml" ]
	then
		echo "<P><H3>PROCESSING OF FILE $1 DONE.</H3>"
	else
		echo
		echo "PROCESSING OF FILE $1 DONE."
	fi
}

ip () {
	in_base=$1

	echo
	echo
	echo "	IP traffic:" >/dev/stderr
	if [ "X$report_type" = "Xhtml" ]
	then
		echo "<P><BR><P><H2>Part 1. IP.</H2>"
		echo "<HR>"
	else
		echo "Part 1. IP."
		echo "======================================================="
	fi
	if [ "X$ip_daily_max" = "XYES" ]
	then
		/usr/local/sbin/AcctMax $in_base
		echodate "Finding maximum values for ip traffic accounting data done"
	fi
	/usr/local/sbin/AcctLog $opt_h -d $in_base ip
	echodate "Done"

        /usr/local/sbin/AcctJoin $in_base $ip_month_base
        echodate "Joining of the IP traffic database done"

	rm -f $in_base.from.db $in_base.to.db
}

squid () {
	in_base=$1

	echo
	echo
	echo "	Squid traffic:" >/dev/stderr
	if [ "X$report_type" = "Xhtml" ]
	then
		echo "<P><BR><P><H2>Part 2. HTTP/FTP through the proxy server(s).</H2>"
		echo "<HR>"
	else
		echo "Part 2. HTTP/FTP through the proxy server(s)."
		echo "======================================================="
	fi
	if [ -n "$squid_logs" ]
	then
		for elem in $squid_logs
		do
			agent_host=`expr $elem : '\([^:]*\):'`
			log_file=`expr $elem : '[^:]*:\(.*\)$'`
			zcat $log_file | /usr/local/sbin/AcctSquid $agent_host
		done
	else	# for backward compatibility with TAS prior to v1.2
		zcat $squid_log | /usr/local/sbin/AcctSquid '*'
	fi
	echodate "Extracting proxy traffic accounting data done"

	/usr/local/sbin/AcctLog $opt_h -d $in_base squid
	echodate "Done"

        /usr/local/sbin/AcctJoin $in_base $squid_month_base
        echodate "Joining of the Squid traffic database done"  

	rm -f $in_base.from.db $in_base.to.db
}

sendmail () {
	in_base=$1

	echo
	echo
	echo "	Sendmail traffic:" >/dev/stderr
	if [ "X$report_type" = "Xhtml" ]
	then
		echo "<P><BR><P><H2>Part 3. SMTP through mail relay server(s).</H2>"
		echo "<HR>"
	else
		echo "Part 3. SMTP through mail relay server(s)."
		echo "======================================================="
	fi
	/usr/local/sbin/AcctLog $opt_h -d $in_base sendmail
	echodate "Done"

        /usr/local/sbin/AcctJoin $in_base $sendmail_month_base
        echodate "Joining of the Sendmail traffic database done"  

	rm -f $in_base.from.db $in_base.to.db
}

mailgate () {
	in_base=$1

	echo
	echo
	echo "	MailGate traffic:" >/dev/stderr
	if [ "X$report_type" = "Xhtml" ]
	then
		echo "<P><BR><P><H2>Part 4. HTTP/FTP/NNTP through MailGate server(s).</H2>"
		echo "<HR>"
	else
		echo "Part 4. HTTP/FTP/NNTP through MailGate server(s)."
		echo "======================================================="
	fi
	/usr/local/sbin/AcctLog $opt_h -d $in_base mailgate
	echodate "Done"

        /usr/local/sbin/AcctJoin $in_base $mailgate_month_base
        echodate "Joining of the MailGate traffic database done"

	rm -f $in_base.from.db $in_base.to.db
}

mkdir $storage/$year 2>/dev/null
mkdir $storage/$year/$month 2>/dev/null

# Call AcctSendmail and AcctMailgate to shift their acct files in case there
# was no any log record since 00:00
echo | /usr/local/sbin/AcctSendmail
echo | /usr/local/sbin/AcctMailgate

echo
echo 'Gathering traffic statistics:'
echo '	(output mailed separately)'
{
	if [ "X$report_type" = "Xhtml" ]
	then
		echo "<HTML><BODY BGCOLOR=\"#50A0A0\" TEXT=YELLOW>"
	fi
	echodate "Started"
	if [ "X$process_ip" = "XYES" ] ; then ip $ip_in_base ; fi
	if [ "X$process_squid" = "XYES" ] ; then squid $squid_in_base ; fi
	if [ "X$process_sendmail" = "XYES" ] ; then sendmail $sendmail_in_base ; fi
	if [ "X$process_mailgate" = "XYES" ] ; then mailgate $mailgate_in_base ; fi

	if [ "X$process_ip" = "XYES" ]
	then
		for file in `ls $ip_in_base.*.from.db 2>/dev/null`
		do
			base=`expr $file : '\(.*\)\.from\.db$'`
			notify_shifted_found "$base.*.db"
			ip $base
			notify_shifted_done "$base.*.db"
		done
	fi
	if [ "X$process_sendmail" = "XYES" ]
	then
		for file in `ls $sendmail_in_base.*.from.db 2>/dev/null`
		do
			base=`expr $file : '\(.*\)\.from\.db$'`
			notify_shifted_found "$base.*.db"
			sendmail $base
			notify_shifted_done "$base.*.db"
		done
	fi
	if [ "X$process_mailgate" = "XYES" ]
	then
		for file in `ls $mailgate_in_base.*.from.db 2>/dev/null`
		do
			base=`expr $file : '\(.*\)\.from\.db$'`
			notify_shifted_found "$base.*.db"
			mailgate $base
			notify_shifted_done "$base.*.db"
		done
	fi
	if [ "X$report_type" = "Xhtml" ]
	then
		echo "</BODY></HTML>"
	fi
} > $storage_file

{
	echo "Subject: Daily Traffic Statistics for $month $day, $year";
	if [ "X$report_type" = "Xhtml" ]
	then	
		echo "Content-type: text/html";
	fi
	echo
	cat $storage_file
}|/usr/sbin/sendmail $recipient

