#!/afs/athena/contrib/perl/p

require 'ctime.pl';
$week = 625388630;
while(<>){
    split(/:/, $_);
    if ($_[0] eq "LOGIN") {
	$intime = &parsetime(join(':', @_[2..$#_]));
	$in = $_;
	print if $intime < 0;
	$inwhere = $_[1];
    }
    elsif($_[0] eq "=LOGOUT"){
	if($_[1] eq $inwhere){
	    $outtime = &parsetime(join(':', @_[2..$#_]));
	    $totaltime += ($outtime-$intime);
	    $thisweek += ($outtime-$intime);
	    while($outtime > ($week+604800)){
		chop($d = &ctime($week));
#		print("Week of ", $d, ":  $thisweek (",  100*$thisweek/604800,
#		      "%)\n");
		print("$d: ");
		print "-" x (100*$thisweek/604800);
		print(int(100*$thisweek/604800),"\n");
		$week = $week+604800;
		$thisweek = 0;
	    }
	    $session++;
	    print("\n\n* Login session on $inwhere from $intime to $outtime for a total of ", $outtime-$intime, "\n* IN--> $in", "* OUT--> $_\n") 
		if ($outtime-$intime) > 28800;
	}
    }

}
print("Total time logged in: $totaltime seconds\n");
$actualtime = time() - 725388630;
$perc = $totaltime/$actualtime;
print("That is, $perc percent of the time...\n");
print("Average session of ", $totaltime/$session, " seconds ($session sessions)\n");


sub parsetime {
    local($string) = @_;
    chop($string);

    $MIN = 60;
    $HOUR = 60 * $MIN;
    $DAY = 24 * $HOUR;
    $YEAR = 365 * $DAY;
    %month = ('Jan', 0,
	     'Feb', 1,
	     'Mar', 2,
	     'Apr', 3,
	     'May', 4,
	     'Jun', 5,
	     'Jul', 6,
	     'Aug', 7,
	     'Sep', 8,
	     'Oct', 9,
	     'Nov', 10,
	     'Dec', 11);
    @mfday  = (1, 32, 61, 92, 122, 153, 183, 214, 245, 275, 306, 336);
    
    ($garb, $mon, $day, $time, @zyear) = split(/[ \t]+/, $string);
    $year = $zyear[$#zyear];
    ($hour, $min, $sec) = split(/:/, $time);

    $time = (($year-1970)*$YEAR) + ($mfday[$month{$mon}]-1)*$DAY + 
	($day-1)*$DAY + ($hour)*$HOUR + ($min)*$MIN + $sec;
}



