#!/local/bin/perl
#
# Written by: Ove Ruben R Olsen (ruben@uib.no) 
#
# Usage: totdays [userlogfile] [userlogfile] ... [userlogfile]
#
################################################################################
#
# Copyright (c) Ove Ruben R Olsen (Gnarfer from hell)
#
# Permission to use, copy, and distribute this software and its
# documentation for any purpose and without fee is hereby granted,
# provided that the above copyright notice appear in all copies and that
# both that copyright notice and this permission notice appear in
# supporting documentation.
# This also apply your own incarnations of any or all of this code.
# If you use portions of this code, you MUST include a apropriate notice
# where you "stole" it from.
#
# THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR IMPLIED
# WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF
# MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
#
# Email:  Ruben@uib.no 
#
#
# This work is free software, but if you find it usefull I would appreciate
# if you donated aprox 10 USD to your country's cancer research.
#
#
# This work is entierly dedicated to Kari L|nningen
#
################################################################################
# Alter this lines to suit your site.
#

$logfile = "/local/irc/userlog" ;		# Your userlogfil.

#
# There is no need going beyond this line unless you are very eager to
# debug and destroy this work :-)
################################################################################
################################################################################



$Date    = " Sun Jan 29 19:23:17 GMT 1995";
$Version = "1.05-006" ;
$ZCAT    = "zcat" ;
$GZIP    = "gzip -cd";
$session = 0 ;
$#full   = 10000;
$#full   = 0;
$post  = 0 ;

select STDERR ; $| = 1 ;
select STDOUT; $| = 1 ;
$junk = shift @ARGV if (@ARGV[0] =~ /^-\w+/);
$stille = 1  if ($junk =~ /^-s/);
$post = 1  if ($junk =~ /^-p/);
$STDIN  = 1  if ($junk =~ /^-i/);

if ($junk =~ /h/) {
print STDERR "\nThis is TOTDAYS version $Version - $Date. 
Written by Ove Ruben R Olsen - Copyright (C) 1994.

Usage: totdays [-s | -n | -i] [logfile] ... [logfile]
   -s   Run silently. No diagnostics nor errormessages will be shown.
   -p   Postprocessing. (No postporcessing is default).
   -i   Reads from STDIN. 

";
exit;
}

if ($#ARGV > -1) {
  push(@logfil,@ARGV) ;
} else {
  push(@logfil,$logfile);
}


print STDERR "\nThis is TOTDAYS version $Version - $Date.\n", 
"Written by Ove Ruben R Olsen - Copyright (C) 1994.\n\n"  unless $stille;


if ($STDIN) {
  print STDERR "Using STDIN as logfile(s).\n" ;
  &LOG ();
} else {
  print STDERR "Using @logfil as logfile(s).\n" ;
  foreach (@logfil) { &LOG ($_) ; }
}


printf "\nTotal served time: %3d ds + %02d:%02d:%02d (%s sessions)\n", 
$total / (24 * 3600), ($total % (24*3600)) / 3600, ($total % 3600) / 60, 
$total % 60, $session;

exit if ($post == 0);
# print "Sessions:          $session\n";

$session = 0 ;

print STDERR "POST-Processing @_[0]: " unless $stille;
$uilk = $lik = $ukjent = 0 ;
for (@full) {
    next if (/^$/);
    $session ++ ;
    print STDERR "." if ( ( $session % 250  ==  0 ) &&  (! $stille));
    ($user,$host) = split (/@/);
    $user =~ tr/~//;
    $user =~ tr/-//;
    ($dom,$ident) = ($_ =~/(\w+\.\w+)\s+\[(\w+)\]$/);

    $rs = "$user@$dom";
    $hosts{$host} ++ ;
    $users{$rs} ++ ;
    $doms{$dom} ++ ;
    
    $ukjent ++ if ($ident eq "unknown") ;
    $ulik ++ if (($ident ne $user) && ($ident ne "unknown")) ;
    $lik ++ if ($ident eq $user);

}
print STDERR "($.)\n" unless $stille;

$host = 0 ; foreach (keys %hosts) {$host ++ } ;
$user = 0 ; foreach (keys %users) {$user ++ } ;
$dom = 0 ; foreach (keys %doms) {$doms ++ } ;
printf "Number of hosts:     %5s\n", $host ;
printf "Number of domains:   %5s\n",$doms;
printf "Number of users:     %5s\n\n",$user;
printf "IDENTS:\n";
printf "   Unknown:          %5s\n",$ukjent;
printf "   Ident != userid:  %5s\n",$ulik;
printf "   Ident == userid:  %5s\n",$lik;


# exit ;


sub LOG {
  $ofil = @_[0];
  if ($STDIN == 0) {
    if (! (-e $ofil && -R $ofil ))  {
       print STDERR "Cannot open userlog file: $ofil ($!)\n" unless $stille;
       return  ;
    }
    print STDERR "Processing @_[0]: " unless $stille;
    $ofil = "$ZCAT $ofil |" if ($ofil =~ /.Z$/);
    $ofil = "$GZIP $ofil |" if ($ofil =~ /.gz$|.z$/);
    open(FILEN, $ofil) ; 
  } else {
    open (FILEN, "-");
  }

  while (<FILEN>) {
    $session ++ ;
    print STDERR "$. " if ( ( $. % 500  ==  0 ) &&  (! $stille));
    ($junk,$ti,$j,$mi,$j,$se,$j,$UatH) = unpack("a26 a3 a1 a2 a1 a2 a3 A50",$_);
    $total += ($ti * 3600) + ($mi * 60) + $se;
    push (@full,$UatH);
  }
  print STDERR "($.)\n" unless $stille;
}
