#!/local/bin/perl
#          
# Written by: Ove Ruben R Olsen (ruben@uib.no) 
#
# Program to check out how much the server is used every day.
# 
# NO manpage, use dycount -h.
#
################################################################################
#
# 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 Johanne Revheim
#
################################################################################
# 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:01:34 GMT 1995";
$Version = "1.00-002" ;
$unpack = "a4 a3 a1 a2"; $oupack = "a2 a2";
$start = 000000;
$end = 121299;
$ZCAT    = "zcat" ;
$GZIP    = "gzip -cd";
$max = 0 ;
$min = 90000 ;
$total = 0;

@MND = ("Jan","Feb","Mar","Apr","May","Jun",
	"Jul","Aug","Sep","Oct","Nov","Dec");

# ....+....1....+....2....+..
# Sun Jan  1 17:35:
while ($i = shift) {
  push (@logfil,$i), next if ($i !~ /^\-/) ;
  push (@logfil,shift(ARGV)), next if ("-l" eq $i)  ;
  $STDIN  = 1  if ($i eq "-i");
  if ($i =~ /^-d$/i) {
    $unpack = "a4 a3 a1 A3 a2" ;
    $oupack = "a2 a2 a2" ;
  }

  if ("-h" eq $i) {
    print (STDERR <<SLUTT);

Usage: dycount [options] [logfile] ... [logfile]
 Options:
   -d             Include hours in the count. 
   -l <logfile>   Uses logfile instead of default logfile.
   -i             Reads from STDIN.

 Logfiles:
   Logfiles is for the lazy ones who do not want to use the -l option.

SLUTT
  exit ;
  }
  
}

push (@logfil,$logfile) unless @logfil;

print STDERR "\nThis is DYCOUNT 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 @logfil as logfile(s).\n" ;
  foreach $fil (@logfil) { &LOG ($fil) ; }
}
    # $dag{$key} = $dag;
foreach (sort %teller) {
  ($mnd,$dy,$ho) = unpack ($oupack,$_);
  $antall = $teller{$_} ;
  next if ($antall eq "");
  $date = "$dag{$_}@MND[$mnd] $dy $ho"; 
  # print "m $mnd d $dy h $ho D $date ($_ -> $antall)\n";

  push(@DATA,sprintf ("$date  %5s\n",$antall));

  $total += $antall ;

  # if ($_ > $start) { $start = $_ ; }
  # if ($_ < $end) { $end = $_ ; }
  if ($antall > $max) {
    $max = $antall ;
    $maxdate = "$date";
  }
  if ($antall < $min) {
    $min = $antall ;
    $mindate = "$date";
  }

}

print "Summary of info from the daycount program\n\n",
       "Starting day:            ",unpack($unpack,@DATA[0]),"\n",
       "Ending day:              ",unpack($unpack,@DATA[$#DATA]),"\n";

  # $data = "@MND[$mnd] $dy $ho"; ($mnd,$dy,$ho) = unpack ($oupack,$_);
printf "Maximun connections:   %5s ($maxdate)\n",$max;
printf "Minimun connections:   %5s ($mindate)\n",$min;
printf "Total connections:     %5s\n\n\n",$total;
print "-" x 79,"\n";
print @DATA,"\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 )  ;
    ($dag,$mnd,$ju,$dy,$h) = unpack ($unpack,$_);

    if ($dy < 10 )  { $dy =~ tr/ //d; $dy = "0$dy"; }

    # print "PRE d $dag m $mnd r $dy -> ";
    # Use this instead of assoc arrays... I doubt that it does matter speedvise
    # since one only needs 12 lookups at max.

    if ($mnd eq "Jan") { $mnd = "00";
    } elsif ($mnd eq "Feb") { $mnd = "01";
    } elsif ($mnd eq "Mar") { $mnd = "02";
    } elsif ($mnd eq "Apr") { $mnd = "03";
    } elsif ($mnd eq "May") { $mnd = "04";
    } elsif ($mnd eq "Jun") { $mnd = "05";
    } elsif ($mnd eq "Jul") { $mnd = "06";
    } elsif ($mnd eq "Aug") { $mnd = "07";
    } elsif ($mnd eq "Sep") { $mnd = "08";
    } elsif ($mnd eq "Oct") { $mnd = "09";
    } elsif ($mnd eq "Nov") { $mnd =  10;
    } elsif ($mnd eq "Dec") { $mnd =  11; }
    $key = "$mnd$dy$h";
    # print " m $mnd r $dy ($key)\n";
    $teller{$key} ++ ;
    $dag{$key} = $dag;
  }
  print STDERR "\n";

}


################################################################################
