#!/usr/bin/perl
#
# Original script written by Joel Hahn, 1997, modified 1998, 1999, 2002

# Get the input
#read(STDIN, $buffer, $ENV{'CONTENT_LENGTH'});
$buffer=$ENV{'QUERY_STRING'};

 $file=$buffer;

# Check which product line was chosen
if ($file eq "jg") {
 $part=", part 1";
}
elsif ($file eq "jg2") {
 $part=", part 2";
}
else {
 $file="jg";
 $part=", part 1";
}

($dev,$ino,$mode,$nlink,$uid,$gid,$rdev,$size,$atime,$mtime,$ctime,$blksize,$blocks) = stat("$file.dat");
($sec,$min,$hour,$modmday,$modmon,$modyear,$wday,$yday,$isdst) = localtime($mtime);
$modmon=$modmon+1;
if (length($modyear)==3) {
  $modyear=substr($modyear,1,2)
}

# Open the relevant data file, dump all the data into the @LINES array
open(FILEDAT,"$file.dat") || die "Can't Open $file.dat: $!\n";
@LINES=<FILEDAT>;
close(FILEDAT);
$SIZE=@LINES;

# Start printing the output to the screen
print "Content-Type: text/html\n\n";
print "<html><head><title>Judges Guild Ratings</title></head>\n";
print "<body text=000000 bgcolor=FFFFFF>\n";
print "<h1>Judges Guild Ratings</h1>\n";
print "<i>Most recent vote cast: $modmon/$modmday/$modyear</i>\n";
print "<p>\n";
print "<hr>\n";
print "<table>\n";
print "<tr><th>Rating / Votes</th> <th align=left>Product Name</th>\n";

# Manipulate the data, crunch the numbers, output the results
for ($i=0;$i<=$SIZE-1;$i++) {
   $_=$LINES[$i];
   ($prodcode, $prodname, $votes, $rate, $ratenums)= split(/\|/, $_, 5);

   if ($rate==0) {
     $avg=0;
   }
   else {
     $rawavg=$rate/$votes;
     $exp=2;
     $avg= sprintf("\%.${exp}f", $rawavg);
   }
     $color ="<font color=0000FF>";
     if ($avg > 1) { $color = "<font color=0000CC>"; }
     if ($avg > 2) { $color = "<font color=330099>"; }
     if ($avg > 3) { $color = "<font color=660099>"; }
     if ($avg > 4) { $color = "<font color=990099>"; }
     if ($avg > 5) { $color = "<font color=BB00BB>"; }
     if ($avg > 6) { $color = "<font color=990099>"; }
     if ($avg > 7) { $color = "<font color=990066>"; }
     if ($avg > 8) { $color = "<font color=990033>"; }
     if ($avg > 9) { $color = "<font color=CC0000>"; }
     if ($avg >=10) { $color = "<font color=FF0000>"; }

   print "<tr><td align=center valign=top><b>$color$avg</font> / $votes</b></td> <td><b>$prodname</b><br>\n";
   $_=$ratenums;
   ($r0, $r1, $r2, $r3, $r4, $r5, $r6, $r7, $r8, $r9, $r10)= split(/\,/, $_, 11);
   print "<font color=0000FF>0:$r0</font> &nbsp\;\n";
   print "<font color=0000CC>1:$r1</font> &nbsp\;\n";
   print "<font color=330099>2:$r2</font> &nbsp\;\n";
   print "<font color=660099>3:$r3</font> &nbsp\;\n";
   print "<font color=990099>4:$r4</font> &nbsp\;\n";
   print "<font color=BB00BB>5:$r5</font> &nbsp\;\n";
   print "<font color=990099>6:$r6</font> &nbsp\;\n";
   print "<font color=990066>7:$r7</font> &nbsp\;\n";
   print "<font color=990033>8:$r8</font> &nbsp\;\n";
   print "<font color=CC0000>9:$r9</font> &nbsp\;\n";
   print "<font color=FF0000>10:$r10</font> \n";
   print "</td>\n";
}

# Finish up the results page
print "</table>\n";
print "<hr>\n";
print "View <a href=\"http://www.hahnlibrary.net/rpgs/rate/disratejg.cgi?jg\">part 1 of the ratings</a>.<br>\n";
print "View <a href=\"http://www.hahnlibrary.net/rpgs/rate/disratejg.cgi?jg2\">part 2 of the ratings</a>.<br>\n";
print "Return to the <a href=\"http://www.hahnlibrary.net/rpgs/rate/index.html\">main ratings page</a>.<br>\n";
print "Go <a href=\"http://www.hahnlibrary.net/rpgs/rate/getratejg.cgi?jg\">rate part 1 of the products</a>.<br>\n";
print "Go <a href=\"http://www.hahnlibrary.net/rpgs/rate/getratejg.cgi?jg2\">rate part 2 of the products</a>.\n";
print "</body></html>";
