#! /usr/local/bin/perl 

#the program will automatically find the latest access_log file in this
#location
$filename = "/usr/local/httpd_logs/texasFl.access_log";
$config = "/bwg/bin/herb_stats_config";
%month_totals = 
    ("Jan" => 0, "Feb" => 31, "Mar" => 59, "Apr" => 90,
     "May" => 120, "Jun" => 151, "Jul" => 181, "Aug" => 212,
     "Sep" => 243, "Oct" => 273, "Nov" => 304, "Dec" => 334) ;
	   

sub is_leap_year {
  my($year) = @_ ;
  if ($year % 4 == 0 && ($year % 100 != 0 || $year % 400 == 0)) {
    return 1 ;
  }
  else { return 0 } ;
}      

# given a (month,day,year) triple, determine the number of the day in the 
# year, starting from 0

sub daynum {
  my($month,$day,$year) = @_ ;

  $add = ($month ne "Jan" && $month ne "Feb" && is_leap_year($year) ?
	  1 : 0) ;

  return $month_totals{$month} + $day + $add - 1 ;
}

# given a day number and a year, return a dd/mmm/yyyy string for the date
# day number can be negative, in which case, go backwards to previous
# years.

@months = qw(Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec) ;
@days = (31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31) ;
@leapdays = (31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31) ;

sub numday {
  my($num, $year) = @_ ;

  # roll the year forward if necessary 
  while (1) {
    $max = 365 + is_leap_year($year) ;
    last if $num < $max ;
    $num -= $max ;
    $year += 1 ;
  }
  # roll the year backward if necessary
  while (1) {
    last unless $num < 0 ;
    $year -= 1 ; 
    $num += 365 + is_leap_year($year) ;
  }
  @year_days = (is_leap_year($year) ? @leapdays : @days) ;
  for ($mon_num = 0 ; $num >= $year_days[$mon_num] ; $mon_num++) {
    $num -= $year_days[$mon_num] ;
  }
  $num += 1 ;
  return "$num/$months[$mon_num]/$year" ;
}

# compare one date in dd/mmm/yyyy format to another

sub date_cmp {
  my($first, $second) = @_ ;
  %monthnum = ("Jan" => 1, "Feb" => 2, "Mar" => 3, "Apr" => 4,
	       "May" => 5, "Jun" => 6, "Jul" => 7, "Aug" => 8,
	       "Sep" => 9, "Oct" => 10, "Nov" => 11, "Dec" => 12) ;

  ($day1, $mon1, $yr1) = split(/\//, $first) ;
  ($day2, $mon2, $yr2) = split(/\//, $second) ;

  if ($yr1 < $yr2) { return -1 }
  elsif ($yr1 == $yr2) {
    if ($monthnum{$mon1} < $monthnum{$mon2}) { return -1 }
    elsif ($monthnum{$mon1} == $monthnum{$mon2}) {
      if ($day1 < $day2) { return -1 }
      elsif ($day1 == $day2) { return 0 }
      else { return 1 } ;
    } else { return 1 } ;
  }
  else { return 1 } ;
}

  
$|=1 ; # avoid flushing problems

$oneday = 0 ; $excess = "" ;
for ($i=0 ; $i <= $#ARGV ; ) {
  if ($ARGV[$i] eq "-oneday") {
    $oneday = 1 ; shift ; 
  } elsif ($ARGV[$i] eq "-excess") {
    $excess = "-x" ; shift ;
  } else {
    $i++ ;
  }
}

$_ = `date` ;
($_, $month, $day, $_, $_, $year) = split ;
$yesterday = numday(daynum($month,$day,$year)-1, $year) ;
$monthago = numday(daynum($month,$day,$year)-30, $year) ;
$counter = "/usr/local/http_hit_counter/http_hit_counter2" ;
#$logfile = $ARGV[0] ;
@files = `ls $filename*`;
chop(@files);
$maxnum = 0;
$loc = 0; $loc2 = 0; $pointer=0; $number = 0; $access_log=0;
$sndMaxNum = 0;
foreach(@files)
{
  #the highest number is what we are looking for
  #and the second highest  
  ($string,$access_log,$number)=split /\./;
  if($number > $maxnum)
  {
    $sndMaxNum = maxnum;
    $maxnum = $number;
    $loc2 = $pointer;
    $pointer = $loc;
  }
  $loc++;
}
$logfile1 = $files[$pointer];
$logfile2 = $files[$loc2];

#create a consolidated big file for parsing
#open(LOGFILE,">output");
#  open(INPUT2,"<$logfile2");  
#  @data2 = <INPUT2>;
#  close(INPUT2);
#  print LOGFILE @data2;
#  open(INPUT,"<$logfile1");
#  @data = <INPUT>;
#  close(INPUT);
#  print LOGFILE @data;
#close(LOGFILE);

$logfile = $logfile1;
#$logfile = "/space/local/apache/logs/access_log.1052352000";
#$logfile = "/bwg/bin/output";
#$config = $ARGV[1] ;

$_ = `head -1 $logfile` ;
/\[(.+?):/ ;
$first_log_date = $1 ;
print "<html>";
print "<head>";
print "<title>Texas A&amp;M BWG WWW access statistics</title>";
print "</head>";
print "<body bgcolor=\"#FFFFFF\" text=\"#000000\">";
print "<center><img src=\"/FLORA/bwg/bwgstat.gif\" alt=\"Texas A&amp;M University Bioinformatics Working Group Product Usage\"></center><br>";
print "<h2>Accesses yesterday:</h2>\n";

system("$counter -l $logfile -c $config $excess -s $yesterday -e $yesterday");
unless ($oneday == 1) {
  if (date_cmp($monthago, $first_log_date) > 0) {
    print "<h2>Accesses for the past month:</h2>\n" ;
    system("$counter -l $logfile -c $config $excess -s $monthago -e $yesterday") ;
  }

  print "<h2>All accesses in current log file</h2>\n" ;
  system("$counter -l $logfile -c $config $excess -e $yesterday") ;
}
#`rm output`;
