#!/usr/athena/bin/perl
open (FO,">/mit/mkgray/totsec") || die "Couldn't open file: $!";
open (FI,"/mit/mkgray/.log") || die "Counldn't open file: $!";

$mintot = 0;
while (<FI>) {

($1, $2, $3, $4, $5, $6, $7, $8, $9, $a, $b)
= split(/[: ]+/, $_);
$machine = $2;
$dayweek = $3;
$month   = $4;
$day     = $5;
$hour    = $6;
$minute  = $7;
$second  = $8;

($1, $2, $3, $4, $5, $6, $7, $8, $9, $a, $b)
= split(/[: ]+/, <FI>);
$num = $num +1;
$omachine = $2;
$odayweek = $3;
$omonth	  = $4;
$oday     = $5;
$ohour    = $6;
$ominute  = $7;
$osecond  = $8;



print ("$machine, $minute, $ominute \n");
print ("$day and $oday");

if ($oday == $day) {
	$seca = ($ohour - $hour)*3600;
	$secb = ($ominute - $minute)*60;
	$secc = ($osecond - $second);
	$sect = $seca + $secb + $secc;
	}
	else {
	if ($omonth == $month) {
		$secd = ($oday - $day)*24*3600;
		$seca = ($ohour - $hour)*3600;
   		$secb = ($ominute - $minute)*60;
        	$secc = ($osecond - $second);
        	$sect = $seca + $secb + $secc + $secd;
	}
		else
		{ if ($month == "Oct") { 
					$nnn = 31; 
					}
		  else {
		  if ($month == "Nov") { $nnn = (30); }
		$secd = ($oday - $day +$nnn)*24*3600;
		$seca = ($ohour - $hour)*3600;
                $secb = ($ominute - $minute)*60;
                $secc = ($osecond - $second);
                $sect = $seca + $secb + $secc + $secd;

}
}
}
if ($num > 1) {
if ($lday == $oday) {
        $seca = ($lhour - $ohour)*3600;
        $secb = ($lminute - $ominute)*60;
        $secc = ($lsecond - $osecond);
        $sectt = $seca + $secb + $secc;
        }
        else {
        if ($lmonth == $omonth) {
                $secd = ($lday - $oday)*24*3600;
                $seca = ($lhour - $ohour)*3600;
                $secb = ($lminute - $ominute)*60;
                $secc = ($lsecond - $osecond);
                $sectt = $seca + $secb + $secc + $secd;
        }
                else
                { if ($omonth == "Oct") { $nnn = (31); }
                  if ($omonth == "Nov") { $nnn = (30); }
                $secd = ($lday - $oday +$nnn)*24*3600;
                $seca = ($lhour - $ohour)*3600;
                $secb = ($lminute - $ominute)*60;
                $secc = ($lsecond - $osecond);
                $sectt = $seca + $secb + $secc + $secd;

}
}
}
$mint = int($sect/6)/10;
$mintt = int($sectt/6)/10;
$hrt  = int($mint/6)/10;
if ($sect > 0) {
print (FO "$month $day Seconds: $sect  InMinutes: $mint  InHrs: $hrt \n");
$mintot = $mintot + int($sect/6)/10;
$offtot = $offtot + $mintt;
}
$lmachine = $omachine;
$ldayweek = $odayweek;
$lmonth = $omonth;
$lday = $oday;
$lhour = $ohour;
$lminute = $ominute;
$lsecond = $osecond;
}
$hrtot = int($mintot/6)/10;
$minav = int(10*$mintot/$num)/10;
$offav = int(10*$offtot/$num)/10;
$perc = int(100*($minav/($minav+$offav)))/100;
print (FO "Total minutes: $mintot\nTotal Hours: $hrtot\nAverage logged time: $minav Minutes\n");
print (FO "Average time between sessions: $offav\n");
print (FO "Percentage of time on: $perc");
close(FO);
















