#!/usr/local/bin/perl

#<META TITLE="Clint's BUsage Stats">

require "../lib/constants.pl";
require "../lib/header.pl";
require "../lib/Parse_Data.pl";
require "../lib/counter.pl";
require "../lib/string.pl";
&Print_Header;
&Parse_Data;

print qq[<table width=100% border=0><Tr valign=top><td align=center>
<script type="text/javascript"><!--
google_ad_client = "pub-1303586900976050";
/* Footer, 728x90, created 6/13/08 */
google_ad_slot = "9449894613";
google_ad_width = 728;
google_ad_height = 90;
//-->
</script>
<script type="text/javascript"
src="http://pagead2.googlesyndication.com/pagead/show_ads.js">
</script>
</td></tr></table>
];

print "<TITLE>Clint's Usage Stats</TITLE>";

my $accessreport=&counter({begintext => "You are the ",
			   usesuffix => 1,
			   nolinks   => 1,
			   endtext   => " person to check these stats."});

my $URL_TO_THIS_PROGRAM = $ENV{REQUEST_URL};
my $counterlog="$COUNTER_LOG";


open(FILE,"$counterlog")||die("couldn't open $counterlog, please alert <a href=\"MAILTO:$MAILTO\">$MAILTO</a>!");
my %DATA=();
my $totalcount=0;
while (<FILE>) {
	next if /^02.000$/;
	$_ =~ s/\s[0-9]{4}$//;

	my ($entity,$count)=split(/\s/);

	$entity =~ s/^\'\/?//;
	$entity =~ s/\'$//;
	$entity =~ s/www\.acm\.vt\.edu\/\///;
	$entity =~ s/clint\.sheer\.us\/\///;

	$count =~ s/^0+//;

	#DEBUG: print "entity=$entity,count=$count<BR>\n";

	$DATA{$entity}=$count;

	$totalcount += $count;
}#endwhile

my $sorttype = $FORM{sort}    || "count";
my $reverse  = $FORM{reverse} || 0;

if ($sorttype eq "url") {
	@sorted_keys = reverse sort { $a cmp $b } (keys %DATA);
} elsif (($sorttype eq "count") || ($sorttype eq "") || 1) {
	@sorted_keys = reverse sort { $DATA{$a} <=> $DATA{$b} } (keys %DATA);
}#endif

my $reversent=1;
if ($reverse) {$reversent=0;}
if ($reverse) { @sorted_keys=reverse @sorted_keys; }


print "<h1 align=center>Clint's Usage Stats</h1>\n";

print "<B>$accessreport</B>";
#print "<BR>";
print "<font size=2>";
#print "(Yes, I decided to keep stats on how often stats are checked.)";
print "<br>(The URL to these stats shows up in the list.)</font>";

print "<h2 align=center>Total hits: ".&commaize($totalcount)."</h2>\n";


my @colwidth=("","55\%","30\%","15\%");

my $FONTB="<font size=5><B>";
my $FONTE="</b></font>";
print qq[
<table border=0 cellspacing=0 cellpadding=2 align=center width=100\%>
<tr valign=middle bgcolor=DDDDDD>
<td align=center width=$colwidth[1]>$FONTB<!--<a href="$URL_TO_THIS_PROGRAM?sort=title&reverse=$reversent">-->TITLE<!--</a>-->$FONTE</td>
<td align=center width=$colwidth[2]>$FONTB<a href="$URL_TO_THIS_PROGRAM?sort=url&reverse=$reversent">URL</a>$FONTE</td>
<td align=right  width=$colwidth[3]>$FONTB<a href="$URL_TO_THIS_PROGRAM?sort=count&reverse=$reversent">Hits</a>$FONTE</td>
</tr>
];


my $url="";
my $title="";
my $linkit="";
my $tmpfile="";
my $url4print="";
foreach my $url (@sorted_keys) {
	$title="";

	next if ($url eq "");

	$count = $DATA{$url};

	##### Determine if it actually exists:
	$linkit=1;
	$file=$url;
	$file =~ s/~$USERNAME/$WWWDIR/;
	$file =~ s/^.*\/pub-cgi-bin\/$USERNAME/$BASECGIDIR/;
	$title .= " <nobr>[]</nobr>";
	if (!-e $file) { 
		$linkit=0; 
		$title="<B>(dead)</b>";
		if ($file !~ /\.html?$/) { next; }
	} else {
		##### Get title:
		if ($file =~ /\.html?$/i) {
			if (open(FILE,"$file")) {
				my $second2lastlinetmp;
				my $lastlinetmp;
				my $islast=0;
				while (<FILE>) {
					if ($_ =~ /<\/TITLE>/i) {
						#DEBUG: print "Found title in line $_<br>";
						$title = $_;
						$title =~ s/^(.*)<TITLE>(.*)<\/TITLE>(.*)$/$2/ig;
						$title =~ s/<TITLE>//ig;
						$title =~ s/<\/TITLE>//ig;
						
						$islast=1;
					}#endif
					$second2lastlinetmp = $lastlinetmp;
					$lastlinetmp = $_;
					if ($islast) { last; }
				}#endif
				close(FILE);
				if ($title eq "") { $title="<B>N/A</B>"; }
			} else {
				$title="&nbsp;";
			}#endif
		} elsif ($file =~ /\.pl$/i) {
			if (open(FILE,"$file")) {
				while (<FILE>) {
					if ($_ =~ /<META TITLE=/i) {
						#DEBUG: print "Found title in line $_<br>";
						$title = $_;
						$title =~ s/^.*<META TITLE=\"//;
						$title =~ s/\".*$//;;
						last;
					}#endif
				}#endif
				close(FILE);
			} else {
				$title="";
			}#endif

			if ($title eq "") { $title="<B>(perl script)</b>"; }

		} elsif ($file =~ /\.pl$/i) {
			$title="<B>(perl script)</b>";#OH
		} elsif ($file =~ /\.cgi$/i) {
			$title="<B>(cgi script)</b>";
		} else {
			$title="<B>N/A</B>";
		}#endif
	}#endif

	##### KLUDGE:
	#print "title is now $title";
	if ($title =~ /<nobr>\[\]<\/nobr>/i) { $title="<B>N/A</B>"; }

	##### Prepare URL for printing:
	$url =~ s/$WWWMACHINENAME\///;
	$url4print = $url;
	$url4print =~ s/^~$USERNAME\///;
	$url4print =~ s/pub-cgi-bin\/$USERNAME\///;
	$url4print =~ s/pub-cgi-bin\/$USERNAME\///;

	##### Draw the row with the link, linking it if ($linkit)
	print "<tr valign=middle bgcolor=\"\#". (($j++%2)?"FFFFFF":"EEEEEE") ."\">\n";

	##### Print title:
	print qq[<td align=center><font face="arial, helvetica, sans-serif" size=2>$title</font></td>];

	##### Print URL:
	print "<td align=center>";
	if ($url =~ /counterstats\.pl/) { print "<font size=4>"; }
	if ($linkit) {
		print "<a target=\"_backfromcounterstats\" ";
		print "href=\"$MACHINEURL" . "/" . "$url\">";
	} else {
		print "<font size=2><B>";
	}#endif
	print "$url4print";
	if ($linkit) { 
		print "</a>"; 
	} else { 
		print " (dead)</B></font>"; 
	}#endif
	#DEBUG: print "<BR>file=$file";#
	if ($url =~ /counterstats\.pl/) { print "</font> <font color=ff0000><nobr><B>(This Page!)</B></nobr></font>"; }
	print "</td>";

	##### Print count:
	print "<td align=right><B>";
	print &commaize($count) || "0";
	print "</B></td>";
	print "</tr>";
}#endif

print qq[</table>];




print qq[
<BR>
<BR>
<font size=2>
<a href="$BASEURL">Clint's Page</a><br>
<a href="$VCRURL">Clint's Media Catalogs</a><br>
<a href="MAILTO:$MAILTO">mail Clint</a><br>
</font>
];
