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

# logtime - perl script
# by <rjbarbal@athena>
#
# Script to determine the amount of time a user spends logged on.
# The following line should be placed in ~/.startup.X:
#     echo "login-:"`hostname`":"`second`":"`date` >>! .log
# The following line should be placed in ~/.logout:
#     echo "logout:"`hostname`":"`second`":"`date` >>! .log

# This script will ignore current login sessions and sessions
# lasting fewer than 10 minutes.

# Open record of logins and logouts for input and open standard output
$logfile = "/mit/daveg/.log";
open (FI,$logfile) || die "Couldn't open file: $!";
open (FO,">-") || die "Couldn't open file: $!";

MAIN_LOOP:
while (1)
{
# Search for next login entry, ignoring extra logout entries
$match = 0;
seek(FI,$position,0);
$_ = <FI>;
while ($_ && !$match)
{
# Specify fields of a login entry
($log,
 $machine,
 $time,
 $dayweek,
 $month,
 $day,
 $hour,
 $minute,
 $second,
 $zone,
 $year
) = split(/[: ]+/, $_);

# Save time of first login entry
if (!$first)
   {$first = $time;
    substr($year, -1) = ""; # Remove CR at end of year
    $start = "$hour:$minute:$second, $dayweek, $day $month $year";}

# Signal a match upon finding next login entry
if ($log eq "login-")
   {$match = 1;}
else
   {$_ = <FI>;}
}

# Save current login entry line for error checking
$current = $.;
# Save current position to allow for return
$position = tell(FI);

# Exit main loop upon reaching end of file
$_ = <FI>;
if (eof(FI))
   {#$. = $current;
    if (eof(FI))
       {$end = "eof";}
    else
       {next MAIN_LOOP;}}

# Search for logout entry corresponding to current login entry
$omatch = 0;
while ($_ && !$omatch)
{
# Specify fields of a logout entry
($olog,
 $omachine,
 $otime,
 $odayweek,
 $omonth,
 $oday,
 $ohour,
 $ominute,
 $osecond
) = split(/[: ]+/, $_);

# Save current logout entry line in file
$ocurrent = $.;

# Signal a match upon finding correct logout entry
if (($olog eq "logout") && ($omachine eq $machine))
   {$omatch = 1;
    #$. = $current;
    if ($ocurrent != (1+ $current))
       {$_ = <FI>;}
    # Save time of last logout entry
    $last = $otime;}

# Signal an error and break out of loop if the user logs into
# the same machine twice without logging out.
if (($olog eq "login-") && ($omachine eq $machine))
   {warn ("Error in log file $logfile:\n");
    warn ("Double login recorded at lines $current and $ocurrent.\n");
warn ("$log:$machine:$time:$date\n");
warn ("$olog:$omachine:$otime:$odate\n");
    $error = 1;
    last MAIN_LOOP;}

# Exit main loop upon reaching end of file
if (eof(FI)) 
   {#$. = $current;
    $_ = <FI>;
    if ($end eq "eof")
       {last MAIN_LOOP;}
    else
       {next MAIN_LOOP;}}

# No match, continue search
if ($omachine ne $machine)
   {$_ = <FI>;}
}

# Signal error if logout time predates login time
if (($end ne "eof") && ($otime < $time))
   {warn ("Error in log file $logfile:\n");
    warn ("Logout time at line $ocurrent predates login time at line $current.\n");
    $error = 1;
    last MAIN_LOOP;}

# Calculate time elapsed during one session
$sessiontime = $otime - $time;

# Ignore counting sessions lasting of less than 10 minutes (600 seconds)
if ($sessiontime >= 600)
   {# Count number of sessions (corresponding login/logout pairs)
    $count = $count + 1;
    # Increment total time by time elapsed during one session
    $total = $total + $otime - $time;}

# Exit main loop upon reaching end of file
if ($end eq "eof")
   {last MAIN_LOOP;}
}

# Quit on error
if ($error) 
   {exit $error;}

# Compute and display statistics
print (FO "Athena usage since $start.\n");

$history = $last - $first;
$dayson = int($history/ 86400);
$dayson = $dayson + 1;
print (FO "Approximately $dayson days.\n");

# Compute total time on-line
$day     = $total / 60 / 60 / 24;
$days    = int($day);
$hour    = ($day - $days) * 24;
$hours   = int($hour);
$minute  = ($hour - $hours) * 60;
$minutes = int($minute);
$second  = ($minute - $minutes) * 60;
$seconds = int($second);
print (FO "Total time spent on-line:  ");
print (FO "$days days, $hours hours, $minutes minutes, $seconds seconds\n");
$hour    = $total / 60 / 60;
$hours   = int(0.5 + $hour);
print (FO "Total number of login/logout sessions:  $count\n");
$average  = int($total / $count);
$ahour    = $average / 60 / 60;
$ahours   = int($ahour);
$aminute  = ($ahour - $ahours) * 60;
$aminutes = int($aminute);
$asecond  = ($aminute - $aminutes) * 60;
$aseconds = int($asecond);
print (FO "Average time per session:  ");
print (FO "$ahours:$aminutes:$aseconds\n");
print (FO "Average time per day: ");
$daytime = ($hours / $dayson);
printf (FO "%.3f hours\n",$daytime);
$percent = int(0.5 + $total / $history * 100);
print (FO "Percentage time spent on-line:  $percent%\n");

# Close files and exit
close (FI);
close (FO);
exit 0;

