#!/usr/bin/perl
require 'ctime.pl';          

my $PATH_TO_APACHE_LOG = "/var/log/apache_access_log";
my $PATH_TO_APACHE_PID = "/var/run/apache.pid";
my $PATH_TO_BACKUP_LOGS = "/usr/local/apache/share/logs";
my $PATH_TO_NUM_HITS = "/usr/local/apache/share/support/totalhits.txt";
my $PATH_TO_STATS_FILE = "/usr/local/apache/share/htdocs/stats.inc";

my ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) =localtime;
$mon++;
my $l=sprintf("log%02d%02d%02d.txt",$mon,$mday,$year);
$l=@ARGV[0] unless @ARGV[0] eq "";
#rename("$PATH_TO_APACHE_LOG", "$PATH_TO_BACKUP_LOGS/log.$l");
#kill(-1,`cat $PATH_TO_APACHE_PID`);

#my $unique_hits =`cat $PATH_TO_BACKUP_LOGS/log.$l | awk \'{print \$1}\' | sort | uniq | wc -l`;
my $unique_hits  =`cat $PATH_TO_APACHE_LOG | awk \'{print \$1}\' | sort | uniq | wc -l`;

#read-in the number of hits so far
open (HITLIST, "$PATH_TO_NUM_HITS");
$num_hits=<HITLIST>;
chomp($num_hits);

#open LOGS,"$PATH_TO_BACKUP_LOGS/log.$l";
open LOGS, "$PATH_TO_APACHE_LOG";

$plat{linux20x}=0;
$plat{linux23x}=0;
$plat{linux22x}=0;
$plat{mac}=0;
$plat{win31}=0;
$plat{win95}=0;
$plat{winnt}=0;
$plat{sun}=0;
$plat{unix}=0;
$plat{other}=0;
$browser{ie}=0;
$browser{netscape}=0;
$browser{lynx}=0;
$browser{other}=0;

$these_hits = 0;

while(<LOGS>) {
	#this should really be generalized...
	my ($host, $referrer, $code, $page, $blank, $info)=split(/"/);

	$these_hits++;

	$_=$info;
	if(/linux 2.0/i) {
		$plat{linux20x}++;
	} elsif(/linux 2.3/i) {          
                $plat{linux23x}++;
        } elsif(/linux 2.2/i) {          
                $plat{linux22x}++;
	} elsif(/mac/i) {
		$plat{mac}++;
	} elsif(/winnt/i) {
		$plat{winnt}++;
	} elsif(/win95/i or /Windows 98/i) {
		$plat{win95}++;
	} elsif(/windows 3.1/i) {
		$plat{win31}++;
	} elsif(/sun/i) {
		$plat{sun}++;
	} elsif (/irix/i or /bsd/i or /open/i or /dec/i or /osf/i or /sco/i) {
		$plat{unix}++;
	} else {
		$plat{other}++;
	}

	if(/mozilla/i) {
		if(/compat/i) {
			$browser{ie}++;
		} 
		else {
			$browser{netscape}++;
		}
	} elsif(/lynx/i) {
		$browser{lynx}++;
	} else {
		$browser{other}++;
	}
}

$totalhits=$these_hits+$num_hits;

#precompute the image widths and percents...

$width{linux20x} =   350 * ($plat{linux20x}/$totalhits);
$percent{linux20x} = 100 * ($plat{linux20x}/$totalHits);

$width{linux23x} =   350 * ($plat{linux23x}/$totalhits);
$percent{linux23x} = 100 * ($plat{linux23x}/$totalHits);

$width{linux22x} =   350 * ($plat{linux22x}/$totalhits);
$percent{linux22x} = 100 * ($plat{linux22x}/$totalHits);

$width{mac}      = 350 * ($plat{mac}/$totalhits);
$percent{mac}    = 100 * ($plat{mac}/$totalHits);

$width{win31}    = 350 * ($plat{win31}/$totalhits);
$percent{win31}  = 100 * ($plat{win31}/$totalHits);

$width{win95}    = 350 * ($plat{win95}/$totalhits);
$percent{win95}  = 100 * ($plat{win95}/$totalHits);

$width{winnt}    = 350 * ($plat{winnt}/$totalhits);
$percent{winnt}  = 100 * ($plat{winnt}/$totalHits);

$width{sun}      = 350 * ($plat{sun}/$totalhits);
$percent{sun}    = 100 * ($plat{sun}/$totalHits);

$width{unix}     = 350 * ($plat{unix}/$totalhits);
$percent{unix}   = 100 * ($plat{unix}/$totalHits);

$width{other}    = 350 * ($plat{other}/$totalhits);
$percent{other}  = 100 * ($plat{other}/$totalHits);

$width{ie}       = 350 * ($browser{ie}/$totalhits);
$percent{ie}     = 100 * ($browser{ie}/$totalHits);

$width{netscape}   = 350 * ($browser{netscape}/$totalhits);
$percent{netscape} = 100 * ($browser{netscape}/$totalHits);

$width{lynx}     = 350 * ($browser{lynx}/$totalhits);
$percent{lynx}   = 100 * ($browser{lynx}/$totalHits);

$width{other}    = 350 * ($browser{other}/$totalhits);
$percent{other}  = 100 * ($browser{other}/$totalHits);

open STATSFILE, ">$PATH_TO_STATS_FILE";
print STATSFILE "

<H2>Yesterday's Statistics</H2>
<P>Yesterday we had $totalHits hits ($unique unique hosts),
bringing the
total number of hits to $totalhits.

<H2>Platforms</H2>
<TABLE><TR>
  
<TD align=right>Linux 2.0.x</TD>
  <TD>$plat{linux20x}</TD>
  <td><img src='images/dot.gif' height=20 width=$width{linux20x}>";

printf("%.2f", $percent{linux20x});
print " percent</td></TR>
<TR>

<TD align=right>Linux 2.3.x</TD>
  <TD>$plat{linux23x}</TD>
  <td><img src='images/dot.gif' height=20 width=$width{linux23x}>";
 
printf("%.2f", $percent{linux23x});
print " percent</td></TR>
<TR>

<TD align=right>Linux 2.2.x</TD>
  <TD>$plat{linux22x}</TD>
  <td><img src='images/dot.gif' height=20 width=$width{linux22x}>";
 
printf("%.2f", $percent{linux23x});
print " percent</td></TR>
<TR>
 
<TD align=right>Windows 3.1 </TD>
  <TD>$plat{win31} </TD>
<td><img src='images/dot.gif' height=20 width=$width{win31}>";

printf("%.2f", $percent{win31});
print " percent</td></TR>
<TR>

"<TD align=right>Windows 95/98</TD>
  <TD>$plat{win95} </TD>
<td><img src='images/dot.gif' height=20 width=$width{win95}>
<td>";

printf("%.2f", $percent{win95});
print " percent</td></TR>
<TR>

<TD align=right>Windows NT</TD>
  <TD>$plat{winnt} </TD>
<td><img src='images/dot.gif' height=20 width=$width{winnt}><td>";

printf("%.2f", $percent{winnt});
print " percent</td></TR>
<TR>

<TD align=right>Macintosh </TD>
<TD>$plat{mac} </TD>
<td><img src='images/dot.gif' height=20 width=$width{mac}><td>";
printf("%.2f", $percent{mac});
print " percent</td></TR>
<TR>

<TD align=right>Sun/Solaris</TD>
  <TD>$plat{sun} </TD>
<td><img src='images/dot.gif' height=20 width=$width{sun}><td> ";
printf("%.2f", $percent{sun});
 
print " percent</td></TR>
<TR>

<TD align=right>Other Unix </TD>
  <TD>$plat{unix} </TD>
<td><img src='images/dot.gif' height=20 width=$width{unix}><td> ";
printf("%.2f", $percent{unix});

print " percent</td></TR>
<TR>

<TD align=right>Other </TD>
  <TD>$plat{other} </TD>
<td><img src='images/dot.gif' height=20 width=$width{other}><td> ";
printf("%.2f", $percent{other});
print " percent</td></TR></TABLE>


<h2>Browsers</H2>
<TABLE>
<TR>

  <TD align=right>Netscape </TD>
  <TD>$browser{netscape} </TD>
<td><img src='images/dot.gif' height=20 width=$width{netscape}><td> ";
printf("%.2f", $percent{netscape});

print " percent</td>
</TR>
<TR>

  <TD align=right>Internet Exploiter </TD>
  <TD>$browser{ie} </TD>
<td><img src='images/dot.gif' height=20 width=$width{ie}><td> ";
printf("%.2f", $percent{ie});
print " percent</td></TR>
<TR>

  <TD align=right>Lynx </TD>
  <TD>$browser{lynx} </TD>
<td><img src='images/dot.gif' height=20 width=$width{lynx}><td> ";
printf("%.2f", $percent{lynx});
print " percent</td>
</TR>
<TR>

  <TD align=right>Other </TD>
  <TD>$browser{other} </TD>
<td><img src='images/dot.gif' height=20 width=$width{other}><td> ";
printf("%.2f", $percent{other});

print " percent</td>
</TR>
</TABLE>";

open HITLIST, ">$PATH_TO_NUM_HITS";
print HITLIST "$sdTotalHits";
