#!/local/bin/perl
#            ^^^^perl5 :-)
# 
# Written by: Ove Ruben R Olsen (ruben@uib.no) 
#
# Read the manual page for further info.
#
################################################################################
#
# 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.
#

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

die "Timecount needs a argument.\n" if ($#ARGV == -1);

$Date    = " Sat Jan 21 17:21:13 GMT 1995.";
$Version = "1.00-000" ;
$ZCAT    = "zcat" ;
$GZIP    = "gzip -cd";
@when    = ();



while ($i = shift) {
  $STDIN  = 1, next  if ($i eq "-i");
  if ("-h" eq $i) {
    print STDERR "\n\nUsage: timecount [options] [ircd-userlogfile]\n\n",
                 "  Options:\n     -i  Reads from STDIN.\n",
                 "     -h  Gives this brief help.\n\n";
    exit ;
  }
  push(@logfile,$i);
}

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

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



print "                             minutes\n";
print "hour"; foreach (0..11) { printf "%5s",$_*5; } print "\n";
$shift = 0 ;
printf "\n%2s ", $shift;
foreach (0..287) {
  $itime = int($_ /12);
  if ( $itime != $shift) {
    printf "\n%2s ", $itime;
    $shift = $itime;
  }
  printf ("%5s",@when[$_]);
}
print "\n\n";


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

  }
  print STDERR "Processing @_[0]: " unless $stille ;

  while (<FILEN>) {
    print STDERR "." if ( $. % 250  ==  0 )  ;
    ($junk,$sti,$junk,$smi,$junk,$ti,$junk,$mi) =
      unpack ("a11 a2 a1 a2 a10 a3 a1 a2",$_);
    # print "DB $_";
    # print "$sti -> $smi -> $ti -> $mi\n";
    $start = $sti * 12 + int($smi / 5);
    $antall = $ti * 12 + int ($mi / 5) +1 ;
    while ($antall--) {
      @when[$start] ++ ;
      $start ++ ;
      $start = 0 if ($start >287);
    }
  }
  print STDERR "\n";
}
