#!/usr/bin/perl
# Created by Ben Okopnik on Fri Oct 31 18:39:15 EST 2003
use warnings;
$|++;

$file = $ARGV[0] || "/var/www/linuxgazette.net-config/logs/access_log.1";
-f $file or die "File does not exist.\n";
-r $file or die "File is not readable by you.\n";
-s $file or die "File is empty.\n";

open Log, "$file" or die "Couldn't open $file: $!\n";

( $month, $year ) = ( split / /, localtime )[1, 5];
# $year += 1900;

while ( <Log> ){ 
	@values = split /[\/\[\]: ]/;
	if ( (defined $month && defined $year ) && ( $values[5] eq $month ) && ( $values[6] eq $year ) ){
		$hits{ $values[0] }++;
		$hpd{ $values[4] }++;
		$count++;
	}
}

close Log;

open Out, ">$ENV{HOME}/running.log" or die "~/running.log: $!\n";
my $old_fh = select Out;
print   "*********************\n";
printf "%-18s%d\n", "$_:", $hits{ $_ } for sort { $hits{ $b } <=> $hits{ $a } } keys %hits;
print   "*********************\n\n";
if ( keys %hpd > 1 ){
	printf "$month %s: %d\n", $_, $hpd{ $_ } for sort { $a <=> $b } keys %hpd;
	print   "*********************\n";
}

$hits = scalar keys %hits;
print "$hits unique host${[s=>]}[$hits==1]\n";
print "$count total hit${[s=>]}[$count==1]\n";

select $old_fh;
close Out;
print "Data written to ~/running.log\n";