#!/usr/bin/perl

###########################################
## RushHour 1.0                          ##
## Written by Janis E. Kenderdine        ##
## http://www.fifedrum.org/fifer         ##
## February 06, 2005                      ##
###########################################

use CGI ':standard';
use CGI::Carp 'fatalsToBrowser';
print header;
$thisdir = `pwd`;
chomp $thisdir;

$report = "report.cgi";
$datadir = "$thisdir/../data";
$years = `ls $datadir`; @years = split("\n", $years);

&Main;

sub Main
{
print '<table width="100%" style="border: solid #cccccc medium" cellspacing="0" cellpadding="0" rules="all">' . "\n";
print '<tr>
   <th align="center" style="padding: 5px; background-color: #cccccc; font-weight: bold">
   Reports available for the following dates:
   </th>
</tr>
</table>
<br>' . "\n";

foreach (reverse(@years))
 {
 $cur_year = $_;
 $months = `ls $datadir/$cur_year`;
 @months = split("\n", $months);
 foreach (reverse(@months))
  {
  $cur_month = $_;
  $month_name = ConvertDate($cur_month);
  $filenames = `ls $datadir/$cur_year/$cur_month`;
  @filenames = split("\n", $filenames);
  &MonthReport($month_name, $cur_year, "$datadir/$cur_year/$cur_month/");
  &DayReport("$datadir/$cur_year/$cur_month/");
  print "<p style=\"font-size:80%\">* Unique Visitors</p>\n";
  }
 }
}

# DayReport($directory)
sub DayReport
{
my $directory = $_[0];
$x=0;
foreach(@filenames)
 {
 open (HITS, "$directory/$_");
 while ($line = <HITS>)
  {
  my @splitline = split("\t", $line);
  $hits{$splitline[0]} = 1;
  }
 close HITS;
 my @hits = keys(%hits);
 $hitcount = @hits;
 my $day = $_;
 $day =~ s/^0//;
 if ($x eq "0") { print "<tr>\n"; }
 print "  <td width=\"14%\" style=\"padding: 2px 5px 2px 5px\" align=\"center\"><a href=\"$report?filename=$cur_year/$cur_month/$_&date=$month_name+$day,+$cur_year&type=daily\">$month_name $day</a><br>($hitcount visits)<span style=\"font-size:90%\">*</span></td>\n";
 $x++;
 if ($x eq "7") { print "</tr>\n"; $x=0; }
 %hits = ();
 }
while (($x ne "0") && ($x ne "7")) { print "<td width=\"14%\">&nbsp;</td>\n"; $x++; }
print "</table>\n";
}

# MonthReport (Month, Year, Directory)
sub MonthReport
{
 %hits = ();
 my $date = "$_[0], $_[1]";
 my $dirname = "$cur_year/$cur_month";
 my $directory = $_[2];
foreach(@filenames)
 {
 open (HITS, "$directory/$_");
 while ($line = <HITS>)
  {
  my @splitline = split("\t", $line);
  $hits{$splitline[0]} = 1;
  }
 close HITS;
}
 my @hits = keys(%hits);
 $hitcount = @hits;
%hits = ();
print '<table width="100%" style="border: solid #ccccff medium" 
cellspacing="0" cellpadding="0" rules="all">' . "\n";
print '<tr>
   <th colspan="7" align="center" style="padding: 5px; background-color: #ccccff; color:#990000; font-weight: bold"><a href="' . $report . '?filename=' . $dirname . '&date=' . "$date" . '&type=monthly">
   ' . "$_[0] $_[1]" . '</a> (' . $hitcount . ' visits)*
   </th>
</tr>';
}

# ConvertDate (num_month)
sub ConvertDate
{
%months = (	"01", "January",
		"02", "February",
		"03", "March",
		"04", "April",
		"05", "May",
		"06", "June",
		"07", "July",
		"08", "August",
		"09", "September",
		"10", "October",
		"11", "November",
		"12", "December");
return $months{$_[0]};
}

exit;
