#!/usr/bin/perl
$USER = an4m;
$WEBLOG = "/users/$USER/public_html/cgi-bin/.weblog";
#$WEBLOG = "/users/webman/server/httpd_current/logs/access_log";
########## DO NOT CROSS - MAYHEM BELOW ####################################
$PROG = $0; $PROG =~ s#.*/##;
$DATE = $EXCEPT = $ONLY = ""; $PAGES = $SITES = 1; $OUTPUT = "html";
for ($i = 0; $i <= $#ARGV; $i++) {
	if (@ARGV[$i] =~ /-*help/) {
		die "Repeatable optionally-hyphenated command switches for $PROG\n"
			. "\t-date <arg>   : statistics for day <arg>\n"
			. "\t-hour         : shortcut for -date " . `date "+%d/%b/%Y:%H"`
			. "\t-today        : shortcut for -date " . `date "+%d/%b/%Y"`
			. "\t-month        : shortcut for -date " . `date "+../%b/%Y"`
			. "\t-year         : shortcut for -date " . `date "+../.../%Y"`
			. "\t-only <arg>   : only pages matching <arg> included\n"
			. "\t-except <arg> : pages/sites matching <arg> excluded\n"
			. "\t-pages        : only page statistics\n"
			. "\t-sites        : only site statistics\n"
			. "\t-output <arg> : <arg> = text/html/raw\n"
			. "\t-help         : this help page\n"
			. "Example: $PROG "
			. "-date 30/Aug/1971 only html -only gif -except oops.html "
			. "except cs.virginia.edu sites\n"; }
	elsif (@ARGV[$i] =~ /-*hour/) { $DATE = `date "+%d/%b/%Y:%H:"`; chop($DATE); }
	elsif (@ARGV[$i] =~ /-*today/) { $DATE = `date "+%d/%b/%Y:"`; chop($DATE); }
	elsif (@ARGV[$i] =~ /-*month/) { $DATE = `date "+../%b/%Y:"`; chop($DATE); }
	elsif (@ARGV[$i] =~ /-*year/) { $DATE = `date "+../.../%Y:"`; chop($DATE); }
	elsif (@ARGV[$i] =~ /-*date/) { $DATE = @ARGV[++$i]; }
	elsif (@ARGV[$i] =~ /-*only/) { $ONLY .= "|@ARGV[++$i]"; }
	elsif (@ARGV[$i] =~ /-*except/) { $EXCEPT .= "|@ARGV[++$i]"; }
	elsif (@ARGV[$i] =~ /-*page/) { $PAGES = 1; $SITES = 0; }
	elsif (@ARGV[$i] =~ /-*site/) { $PAGES = 0; $SITES = 1; }
	elsif (@ARGV[$i] =~ /-*output/) { $OUTPUT = @ARGV[++$i]; }
	else { print "Unknown option: @ARGV[$i]\n"; @ARGV[$i+1] = "help"; } }
$EXCEPT =~ s/^\|//; $ONLY =~ s/^\|//;
@weblog = ($EXCEPT eq "")
		? `egrep \"$DATE.*$USER.*$ONLY\" $WEBLOG`
		: `egrep \"$DATE.*$USER.*$ONLY\" $WEBLOG | egrep -v \"$EXCEPT\"`;
$totalhits = 0; %pages = %visitors = ();
foreach (@weblog) {
	($site, $tmp, $tmp, $tmp, $tmp, $tmp, $page, $tmp, $result, $tmp) = split;
	next if ($result ne "200");
	$page =~ s/\?.*//; $page =~ s/%7[eE]/~/; $page =~ s/index.html//;
	$page =~ s/\/~$USER\///; if ($page eq "") { $page = "index.html"; }
	$totalhits++; $visitors{$site}++; $pages{$page}++; }
if ($OUTPUT eq "text") {
	print "TOTAL HITS: $totalhits\n";
	if ($PAGES == 1 && print "\nPOPULAR PAGES\n") {
		foreach (sort { $pages{$a} < $pages{$b} } keys(%pages))
			{ printf("%-50s  %d\n", $_, $pages{$_}); } }
	if ($SITES == 1 && print "\nFREQUENT VISITORS\n") {
		foreach (sort { $visitors{$a} < $visitors{$b} } keys(%visitors))
			{ printf("%-50s  %d\n", $_, $visitors{$_}); } } }
elsif ($OUTPUT eq "html") {
	print "<html><head><title>Web Visitors Page</title>"
		. "<meta http-equiv=\"pragma\" content=\"nocache\"></head>"
		. "<body bgcolor=#efefef><center><h1>Web Visitors Page, <i>$DATE</i>"
		. "</h1><hr>TOTAL HITS: $totalhits<hr><table width=100%><tr>";
	if ($PAGES == 1 &&
		print "<td valign=top><table width=100% border=3><tr><td>"
			. "<table cellspacing=0 cellpadding=0 width=100%><tr>"
			. "<td align=center width=50%>P<font size=-1>OPULAR</font> "
			. "P<font size=-1>AGES</font></td><td width=50%>"
			. "<hr align=left size=5 width=100%></td></tr>") {
		foreach (sort { $pages{$a} < $pages{$b} } keys(%pages)) {
			print "<tr><td width=50%><a href=\"$_\">$_</a></td>"
				. "<td width=50%><hr noshade size=5 align=left "
				. "width=" . ($pages{$_}*100/$totalhits) . "%></td></tr>"; }
		print "</table></td></tr></table></td>"; }
	if ($SITES == 1 &&
		print "<td valign=top><table width=100% border=3><tr><td>"
			. "<table cellspacing=0 cellpadding=0 width=100%><tr>"
			. "<td align=center width=50%>F<font size=-1>REQUENT</font> "
			. "V<font size=-1>ISITORS</font></td><td width=50%>"
			. "<hr size=5 align=left width=100%></td></tr>") {
		foreach (sort { $visitors{$a} < $visitors{$b} } keys(%visitors)) {
			print "<tr><td width=50%>$_</td><td width=50%><hr noshade "
				. "size=5 align=left width=" . ($visitors{$_}*100/$totalhits)
				. "%></td></tr>"; }
		print "</table></td></tr></table></td>"; }
	print "</tr></table><hr></center></body></html>\n"; }
elsif ($OUTPUT eq "raw") {
	foreach (@weblog)
		{ print; } }
else
	{ die "Unimplemented\n"; }
