#! /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}
compress_rotated=${compress_rotated:-NO}
keep=${keep:-12}
keep=$(($keep+1))
process_ip=${process_ip:-YES}
process_squid=${process_squid:-YES}
process_sendmail=${process_sendmail:-YES}
process_mailgate=${process_mailgate:-NO}
report_type=${report_type:-html}


outsuffix=${1:-`date -v-1m '+%Y%m'`}
oldestsuffix=`date -v-${keep}m '+%Y%m'`
insuffix=month

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

ip_in_base=$prefix/ip.$insuffix
ip_out_base=$prefix/ip.$outsuffix
ip_oldest_base=$prefix/ip.$oldestsuffix

squid_in_base=$prefix/squid.$insuffix
squid_out_base=$prefix/squid.$outsuffix
squid_oldest_base=$prefix/squid.$oldestsuffix

sendmail_in_base=$prefix/sendmail.$insuffix
sendmail_out_base=$prefix/sendmail.$outsuffix
sendmail_oldest_base=$prefix/sendmail.$oldestsuffix

mailgate_in_base=$prefix/mailgate.$insuffix
mailgate_out_base=$prefix/mailgate.$outsuffix
mailgate_oldest_base=$prefix/mailgate.$oldestsuffix

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

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

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
		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
		/usr/local/sbin/AcctLog $opt_h -d $ip_in_base ip
		echodate "Done"
		mv $ip_in_base.from.db $ip_out_base.from.db
		mv $ip_in_base.to.db $ip_out_base.to.db
		rm -f $ip_oldest_base.* 2>/dev/null
		if [ "X$compress_rotated" = "XYES" ] ; then gzip $ip_out_base.* ; fi
	fi

	if [ "X$process_squid" = "XYES" ]
	then
		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
		/usr/local/sbin/AcctLog $opt_h -d $squid_in_base squid
		echodate "Done"
		mv $squid_in_base.from.db $squid_out_base.from.db
		mv $squid_in_base.to.db $squid_out_base.to.db
		rm -f $squid_oldest_base.* 2>/dev/null
		if [ "X$compress_rotated" = "XYES" ] ; then gzip $squid_out_base.* ; fi
	fi

	if [ "X$process_sendmail" = "XYES" ]
	then
		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 $sendmail_in_base sendmail
		echodate "Done"
		mv $sendmail_in_base.from.db $sendmail_out_base.from.db
		mv $sendmail_in_base.to.db $sendmail_out_base.to.db
		rm -f $sendmail_oldest_base.* 2>/dev/null
		if [ "X$compress_rotated" = "XYES" ] ; then gzip $sendmail_out_base.* ; fi
	fi

	if [ "X$process_mailgate" = "XYES" ]
	then
		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 $mailgate_in_base mailgate
		echodate "Done"
		mv $mailgate_in_base.from.db $mailgate_out_base.from.db
		mv $mailgate_in_base.to.db $mailgate_out_base.to.db
		rm -f $mailgate_oldest_base.* 2>/dev/null
		if [ "X$compress_rotated" = "XYES" ] ; then gzip $mailgate_out_base.* ; fi
	fi

	if [ "X$report_type" = "Xhtml" ]
	then
		echo "</BODY></HTML>"
	fi

} > $storage_file

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

