#!/usr/bin/perl

# Calculate dsl costs
# Version 0.1
# R. Krienke, krienke@uni-koblenz.de

require "newgetopt.pl";

$costpermin=0.005;   # 0.5cent/min
$freeHours=20;

if( ! &NGetOpt( "a", "t","f=s","h", "w", "help") ){
        &usage;
}

#
# Print usage
#
sub usage {
	die "\n$0\n   [-f <datafile>] read data from <datafile>\n",
	    "   [-t] display data from \"today\" only\n",
	    "   [-a] display all data in detail\n",
	    "   [-w] output in HTML\n",
	    "\n";
}


#
# Print one formatted output line
#
sub printEntry{
	my($d, $h, $m, $rec, $xmit, $costs )=@_;

	printf "%-11s", $d .":";
	printf "%4d:%02d h", $h, $m;
	
	printf "%8.2f MB in", $rec/1024/1024;
	printf "%8.2f MB out",$xmit/1024/1024;
	printf "%6.2f (%.2f) Eur\n", 
		$costs-$freeHours*60*$costpermin >0? 
				$costs-$freeHours*60*$costpermin:0,
		$costs ;
}

#
# --------------------------------------------------------------
#
usage if( $opt_h || $opt_help );

if( length($opt_f) ){
	$dataFile=$opt_f;
}else{
	$dataFile="/var/log/ppp-usage";
}	

if( length($opt_t) ){
	$today=`date "+%d.%m.%Y"`;
	chomp($today);
}

#
# Read raw data from logfile. Data are inserted into hash where
# "month.year" serves as akey to the hash. For each entry in the log
# file we sum up the onlinetime, received and xmitted data for this
# month. So after all we have a hash with one entry per month holding
# the totals for this month. If option opt_a is given the date consists
# of day.month.year, so we build up totals for each day.
#
open(LOGS, $dataFile) || die "Cannot open /var/log/ppp-usage. Abort.\n";
while( <LOGS> ){
	chomp;
	@line=split;
	$date=$line[0];
	next if( length($opt_t) && ($today ne $date) );
	$date=~s/[0-9]+\.// if( ! length($opt_a));
	#warn $date, "\n";
	
	$costs{"$date"}->{"onlinetime"}+=$line[2];
	$costs{"$date"}->{"minonlinetime"}+=int(($line[2] +59)/60)*60;
	
	$costs{"$date"}->{"received"}+=$line[4];
	$costs{"$date"}->{"xmitted"}+=$line[3];
}
close(LOGS);


if( $opt_w ){
	print "Content-Type: text/html\n\n";
	print <<'EOF';
<HTML>
<HEAD>
<TITLE>T-DSL Kosten</TITLE>
</HEAD>
<BODY bgcolor="#FFFFFF">
<PRE>
EOF
}
print "DSL-Kosten:\n";
print "-----------\n";
print "\n";

#
# Total sums over all months
#
$tot_minonline=0;
$tot_online=0;
$tot_in=0;
$tot_out=0;

#
# print all data ordered by month
# sum up totals for onlinetime etc.
#
foreach $i (sort(keys(%costs))){
	$tot_minonline+=$costs{$i}->{"minonlinetime"};
	$tot_online+=$costs{$i}->{"onlinetime"};
	$tot_in+=$costs{$i}->{"received"};
	$tot_out+=$costs{$i}->{"xmitted"};

	# minonlinetime ist die onlinetime in Minuten, wobei jede
	# angebrochene Minute als 1 gewertet wird. Wert ist in Sekunden
	# In onlinetime steht die genau Zeit in sekunden
	$h=int($costs{$i}->{"minonlinetime"}/60)/60;
	$m=$h-int($h);
	$m=int(($m*60.0/100.0)*100+0.5);
	

	# Providers like to tease their customers by using started minutes
	# Try to calculate this right
	$costs=int($costs{$i}->{"minonlinetime"}/60 +0.99);
	$costs*=$costpermin;

	printEntry( $opt_t?$today:$i, $h, $m, 
			$costs{$i}->{"received"},
			$costs{$i}->{"xmitted"}, $costs );
}


# Print totals of data
#
print "------------------------------------------------------------------\n";
$h=int($tot_minonline/60)/60;
$m=$h-int($h);
$m=int(($m*60.0/100.0)*100+0.5);

$costs=int($tot_minonline/60 +0.99);
$costs*=$costpermin;

printEntry( "Total", $h, $m, 
		$tot_in,
		$tot_out, $costs );

#
# Prologue
if( $opt_w ){
	print <<'EOF';
</PRE>
</BODY>
</HTML>
EOF

}
