#!/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 Fr|ydis Bakken.
#
################################################################################
# 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    = " Tue Feb  1 10:27:46 GMT 1994";
$Version = "1.03-001" ;
$ZCAT    = "zcat" ;
$GZIP    = "gzip -cd";
$session = 0 ;
$#full   = 10000;
$#full   = 0;
$nopost  = 1 ;

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

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) {
  &LOG ();
}  else {
  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 ($nopost);
# print "Sessions:          $session\n";

$session = 0 ;

print STDERR "POST-Processing @_[0]: " unless $stille;
for (@full) {
    $session ++ ;
    print STDERR "$session " if ( ( $session % 500  ==  0 ) &&  (! $stille));
    ($user,$host) = split (/@/);
    ($dom) = ($_ =~/(\w+\.\w+)$/);
    $user = "$user@$dom";

    push (@hosts,$host) if (grep (/^$host$/,@hosts) == 0)  ;
    push (@users,$user) if (grep (/^$user$/,@users) == 0);
    push (@doms,$dom) if (grep (/^$dom$/,@doms) == 0 );
}
print STDERR "($.)\n" unless $stille;

printf "Number of hosts:   %3s\n",$#hosts;
printf "Number of domains: %3s\n",$#doms;
printf "Number of users:   %3s\n",$#users;


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 ++ ;
    # $0 = "RUNNING $." ;
    print STDERR "$. " if ( ( $. % 500  ==  0 ) &&  (! $stille));
    # print STDERR "|" if ( ( $. % 500 ==  0 ) &&  (! $stille));
    # print STDERR "/" if ( ( $. % 1000 ==  0 ) &&  (! $stille));
    # print STDERR "-" if ( ( $. % 500  ==  0 ) &&  (! $stille));
    # print STDERR "\\" if ( ( $. % 1000 ==  0 ) &&  (! $stille));
    # print STDERR "-" if ( ( $. % 500 ==  0 ) &&  (! $stille));
    # print STDERR "/" if ( ( $. % 1000 ==  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;
}
