

$SERVER_BASE = $ENV{SERVER_WWW_HOME}."/";
$ANALOG_HOME = $ENV{SERVER_ANALOG_HOME}."/";
$BGCOLOR     = $ENV{SERVER_WWW_BACKGROUND};
 
#autoflush output
$| = 1;
select stderr;
$| = 1;
select stdout;
$doYear=0;

if(@ARGV>0) {
    if($ARGV[0] eq "-year") {
	$doYear = 1;
 } else {
     print STDERR "Usage: stats [-year]\n";
     exit 1;
 }
}

@files = ();

if (-x "$ANALOG_HOME" and -x "${ANALOG_HOME}analog")
{
    chdir "$ANALOG_HOME";
    print STDERR "stats.pl: Calling analog...\nMonthly report...\n";
    system ("(./analog +gmonth.cfg > ${SERVER_BASE}month_stats.html)") and die "Could not execute analog with output to ${SERVER_BASE}month_stats.html\n";
    @files = (@files, "month_stats.html");
				# Run the total report only on Sundays
    @day = localtime();
    if($day[6] == 0) {	
	$doYear = 1;
    }
    if($doYear) {
	print STDERR "Total report...\n";
	system ("(./analog +gall.cfg > ${SERVER_BASE}all_stats.html)") and die "Could not execute analog with output to ${SERVER_BASE}all_stats.html\n";
	@files = (@files, "all_stats.html");
    }
}
else 
{
    print STDERR "No access to directory $ANALOG_HOME\n";
    foreach $file (@files)
    {
	open(STATS, ">$SERVER_BASE$file") or die "Could not open html files";
	print STATS <<NED;
<HTML>
<BODY BGCOLOR=\"$BGCOLOR\">
<A HREF=\"$ENV{SERVER_WWW_URL}\">
<IMG SRC=\"$ENV{SERVER_WWW_URL}/images/globe_small.gif\" border=0>
</A>
<br clear=all><p>
Web server statistics are not available at this time.
</BODY>
</HTML>
NED
     }
    exit(0);
}

foreach $file (@files)
{

    if (-r "$SERVER_BASE$file")
    {
	open(STATS, "$SERVER_BASE$file");
	@all = <STATS>;
	close(STATS);

	open(STATS, ">$SERVER_BASE$file");
	foreach $line (@all)
	{
	#    $line =~ s/<hr>/<br>/g;
	    if ($line !~ /analogo/) {
		print STATS $line;
	    }
	    if ($line =~ /<a NAME="DaySum">Daily Summary/)
	    {
		print STATS "<p>\nThe graphs for the daily summary represent the sum total of requests for that day of the week over the entire report period listed at the top of this page.  These are neither averages nor are they restricted to the past week.\n";
	    } else {
		if ($line =~ /<a NAME="Dom">Domain Report/ or $line =~ /<a NAME="Size">File Size Report/)
		{
		    print STATS "<p>\nThis report includes statistics for the entire report period listed at the top of this page.  Percentages are relative to the total Web server traffic.\n";
		}
	    }
	}
    } else {
	print STDERR "stats: No such file!","$SERVER_BASE$file","\n";
    }
}






