#!/usr/local/bin/perl


print "Content-type:text/html\n\n";
print "<pre>";


$log = "/www/logs/kisrael-access-log";

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



$dayback = $ARGV[0];
if($dayback eq ""){
    $dayback = 1;
}

($sec,$min,$hour,$mday,$mon,$year) = localtime(time-(24*60*60*$dayback)); 
$year += 1900;
$month = $months[$mon];
$mon++;
if(length($min) == 1) { $min = "0".$min;}
if(length($sec) == 1) { $sec = "0".$sec;}
if(length($mday) == 1) { $mday = "0".$mday;}
if(length($mon) == 1) { $mon = "0".$mon;}     


$thedate = "$mday/$month/$year";

print "for $year.$mon.$mday: ";

open(READ,$log);
while(defined($next=<READ>)){
    if($next =~ /$thedate/ ){
	$next =~ /^(.*?) /;
	$ip = $1;
	$ips{$ip}++;
	if ($next =~ /GET \/ /){
	    $frontips{$ip}++;
	} else {
	}

	    #print $next;

	if($next =~ /\"GET (.*?)\s*HTTP/i){
	    $thing = $1;
	    
	    $totalhits{$thing}++;
	    
	    if($thing =~ /\.cgi/i){
		$listcgi{$thing}++;
	    } else {
		if($thing =~ /gif$/i || $thing =~ /jpg$/i || $thing =~ /jpeg$/i || $thing =~ /png$/i){
		    $listimg{$thing}++;
		} else {
		    if($thing =~ /\/$/ || $thing =~ /htm$/i || $thing =~ /html$/i || $thing =~ /txt$/i){
			$listhtml{$thing}++;
		    } else {
			$listother{$thing}++;
		#	print "$thing\n";
		    }
		}
	    }
	}
    }	
}
close READ;


#foreach $key (keys %frontips){
 #   $frontuniq++;
#    $fronthit += $frontips{$key};
#}
#print "frontpage: $fronthit hits ($frontuniq unique ip) ";
#
#foreach $key (keys %ips){
#    $uniq++;
#    $hit += $ips{$key};
#}
#print "overall: $hit hits ($uniq unique ip)";

print "</pre>\n";


printtotals("html",%listhtml);
printtotals("cgi",%listcgi);
printtotals("img",%listimg);
printtotals("other",%listother);




sub printtotals{
    my($title, %hash) = @_;

    print "<h2>$title</h2><table border=1>\n";


    foreach $key (sort {$hash{$b} <=> $hash{$a}} keys %hash){
	print "<tr><td>$hash{$key}</td><td>$key</td></tr>\n";

    }
print "</table>\n";
}

