#!/usr/bin/perl
# $Id: uusummary,v 1.2 1997/03/17 21:30:25 projects Rel $
# Author: D. Emilio Grimaldo T.
# Date  : June 9, 1996
# Usage : uusummary [-s system] [-u user] [ -S since || -o on ] [-t]
# Descr.:
#	Generate UUCP transfer statistics out of the Stats file
#	(HDB/Taylor configuration).
#	* system can be any UUCP system or `all'
#	* user   is any username
#	* since, on are in the format YYYY-MM-DD and only one of them
#	         can be specified. `Since' gives statistics on all 
#		 transfers of the user/system since that date whereas
#		 `on' gives it only for that particular date. Both
#	   	 require either `user' or `system' to be specified.
#
require "getopts.pl";

$SpoolDir = "/var/spool/uucp";
$StatLog  = "Stats";

sub DoStats {
    if ((defined($opt_o) && $date eq $opt_o) ||
        (defined($opt_S) && $date ge $opt_S) ||
	!defined($opt_S) && !defined($opt_o)) {
	if ($direction eq "received") {
	    $TotalReceived += $size;
	}
	elsif ($direction eq "sent") {
	    $TotalSent += $size;
	}
	print "$user $system $date $time $direction $size $port" unless defined($opt_t);
    }
}

sub UserStats {
    if ($direction eq "received") {
	$UserRx{$user} += $size;
    }
    else {
	$UserTx{$user} += $size;
    }
}

if (! -e "$SpoolDir/$StatLog") {
    print "$SpoolDir/$StatLog does not exist.\n";
    exit;
}

&Getopts('s:u:o:S:Ut');

if (defined($opt_S) && defined($opt_o)) {
    print "Cannot specify both -o (on) and -S (since)!\n";
    exit;
}
$Usr = $opt_u if (defined($opt_u));
$Sys = $opt_s if (defined($opt_s));


$TotalSent = 0;
$TotalReceived = 0;
$TotalFailures = 0;

open(STATS,"$SpoolDir/$StatLog") or die "cannot open status file\n";
while (<STATS>) {
    if (/failed/) {
        $TotalFailures++;
	next;
    } 
    ($user,$system,$date,$time,$direction,$size,$rest) = split(' ',$_,7);
    $date =~ s/^\(//;
    $time =~ s/\)$//;
    $port = substr($rest,rindex($rest,"port ") + 5);
    #
    # Now parse according to OR/AND command line spec
    #
    if (defined($opt_U)) {
	&UserStats;
    }
    elsif (defined($Usr) && defined($Sys)) {
	if ($Usr eq $user && ($Sys eq $system || $Sys eq "all")) {
	    &DoStats;
	}
    }
    elsif ((defined($Usr) && $Usr eq $user) ||
	   (defined($Sys) && $Sys eq $system) ||
	   (defined($Sys) && $Sys eq "all")) {
	&DoStats;
    }
}
close(STATS);
printf "----\nTotal sent:\t%d\nTotal received:\t%d\nTotal failures: %d\n", 
	$TotalSent, $TotalReceived, $TotalFailures  unless defined $opt_U;

if (defined($opt_U)) {
    print "Account      Sent         Received\n";
    print "----------------------------------\n";
    foreach $k (sort keys %UserTx) {
	printf "%-8s %10d %10d\n", $k, $UserTx{$k}, $UserRx{$k};
    }
}
