#!/usr/bin/perl

open(LOG, "/var/www/data/purity_log") || die "can't open log\n";

my %total = ();
my %count = ();
my %high = ();
my %low = ();

$images{"Rice"} = "http://www.rice.edu/rice_stone_logo_4color.jpg";
$images{"Texas"} = "http://www.utexas.edu/graphics/side_nav_divider.jpg";
$images{"A&M"} = "http://tarpon.tamug.edu/~cjhall/AM.jpg";
$images{"Cornell"} = "http://cornelledu.cit.cornell.edu/img/layout/cu_logo.gif";

while(my $line = <LOG>)
{ # aca207d0.ipt.aol.com 59 Sun Aug 31 06:46:08 2003
  if($line =~ /(\S+) (\d+)/)
  {
    my $host = $1;
    my $score = $2;

    if(($score > 0) && ($score < 100))
    {
      update("Overall", $score);
  
      if($host =~ /rice.edu$/)           { update("Rice", $score); }
      elsif($host =~ /utexas.edu$/)      { update("Texas", $score); }
      elsif($host =~ /tamu.edu$/)        { update("A&M", $score); }
      #elsif($host =~ /cornell.edu$/)     { update("Cornell", $score); }
      else                               { update("Other", $score); }
    }
  }
}

print <<EOT;
Content-type:text/html


<html>
<head>
<title>Rice University 100 Question Purity Test Results</title></head>
<body BGCOLOR="#000000" TEXT="#F0F0F0" LINK="#FFFF00" VLINK="#22AA22" ALINK="#0077FF">

<center>
<h1>
<img align=middle src=http://www.amphibious.org/pic/sex.gif WIDTH=48 HEIGHT=48>
The <a href=http://www.rice.edu/>Rice University</a> Results
<img align=middle src=http://www.amphibious.org/pic/sex.gif WIDTH=48 HEIGHT=48>
</h1>
<img src=http://www.amphibious.org/pic/all2.gif WIDTH=904 HEIGHT=24> <p>
<TABLE BORDER=3 CELLSPACING=2 CELLPADDING=2 WIDTH="30%">
<CAPTION>Results Table</CAPTION>
<THEAD><TR><TD>Group<TD>Average<TD>High<TD>Low<TD>Samples</THEAD>
EOT

foreach my $group (keys %total)
{
  if(
     ($group ne "Rice") && 
     ($group ne "Overall")
    )
  {
    summary($group);
  }
}

#summary("Texas");
summary("Rice");
summary("Overall");

print <<EOT;
</TABLE>
<br>
<br>
<br>
<script type="text/javascript"><!--
google_ad_client = "pub-3135495551593611";
/* Rice Purity Top */
google_ad_slot = "6785081082";
google_ad_width = 728;
google_ad_height = 90;
//-->
</script>
<script type="text/javascript"
src="http://pagead2.googlesyndication.com/pagead/show_ads.js">
</script>
<br>
<br>
<br>
<a href=http://www.amphibious.org/plc/rice100.html>Take the test again!</a>
<br>
<a href=http://www.amphibious.org/plc/>Visit the PLC</a>
<br><p>
</center>
<script type="text/javascript">
var gaJsHost = (("https:" == document.location.protocol) ? "https://ssl." : "http://www.");
document.write(unescape("%3Cscript src='" + gaJsHost + "google-analytics.com/ga.js' type='text/javascript'%3E%3C/script%3E"));
</script>
<script type="text/javascript">
var pageTracker = _gat._getTracker("UA-2209762-1");
pageTracker._initData();
pageTracker._trackPageview();
</script>
</body></html>
EOT

sub summary()
{
  my $group = shift;

  my $avg = sprintf("%.2f", $total{$group} / $count{$group});
  my $hi = $high{$group};
  my $lo = $low{$group};
  my $cnt = $count{$group};

  if(defined($images{$group}))
  {
    print "<TR><TD><img src=$images{$group} WIDTH=75 HEIGHT=75 ALT=\"$group\"><TD>$avg<TD>$hi<TD>$lo<TD>$cnt\n";
  }
  else 
  {
    print "<TR><TD>$group<TD>$avg<TD>$hi<TD>$lo<TD>$cnt\n";
  }
}


sub update()
{
  my $group = shift;
  my $score = shift;

  if(!defined($total{$group})) 
  {
    $total{$group} = 0;
    $count{$group} = 0;
    $high{$group} = 0;
    $low{$group} = 100;
  }

  if(($score > 0) && ($score < 100))
  {
    $count{$group}++;
    $total{$group} += $score;
    if ($score > $high{$group}) { $high{$group} = $score; }
    if ($score < $low{$group})  { $low{$group} = $score; }
  }
}

