#!/usr/bin/perl

$Root = $ENV{'DOCUMENT_ROOT'};
chdir("$Root/code/lineup");

$QueryString = $ENV{'QUERY_STRING'};
$Incomplete = 1;
if ($QueryString ne "") {
  $Incomplete = 0;
  open (MYFILE, '>input');
  @NameValuePairs = split (/&/, $QueryString);
  foreach $NameValue (@NameValuePairs) {
    ($Name, $Value) = split (/=/, $NameValue);
    $Value =~ tr/+/_/;
    $Value =~ s/%([\dA-Fa-f][\dA-Fa-f])/ pack ("C", hex($1))/eg;
    $Team[$index++] = $Value;
    print MYFILE "$Value\n";
  }
  print MYFILE "y\n\n"; # fastbreak
  print MYFILE "exit\n";
  close (MYFILE); 
}

print "Content-type: text/html\n\n";
print "<html>\n\n";
print "<head>\n";
print "<title>Strat-o-Matic Lineup Analyzer</title>\n";
print "</head>\n";
print "<body bgcolor=\"#EE9A49\">\n";
print "<font size=4>\n";
print "<center>\n";
print "<h3>Strat-o-Matic Lineup Analyzer</h3>\n";
print "</center>\n";
print "</font>\n";

print "<p>\n";
print "Data is for the 2006-07 Sunnyvale Stars, which went 72-10.\n";
print "Subsequent teams have been sadly too bereft of talent to display.\n";
print "Note there is no error-checking to prevent a player being used\n";
print "multiple times.  Believe it or not, there are occasions of use for\n";
print "this.  The default option at each position is the traditional\n";
print "regular season starter.  Due to minute limitations this is not\n";
print "necessarily the best player.\n";
print "</p>\n";

print "<center>\n";
print "<form action=\"offense.pl\" method=\"get\">\n";
print "<table>\n";
print "<tr>\n";
print "<td align=center>PG</td>\n";
print "<td align=center>SG</td>\n";
print "<td align=center>SF</td>\n";
print "<td align=center>PF</td>\n";
print "<td align=center>C</td>\n";
print "</tr>\n";
print "<tr>\n";

print "<td>\n";
print "<select name=\"PG\">\n";
$Last = selected($Team[0], "Knight");
print "  <option value=\"Knight\"$Last>Brevin Knight</option>\n";
$Last = selected($Team[0], "Dickau");
print "  <option value=\"Dickau\"$Last>Dan Dickau</option>\n";
$Last = selected($Team[0], "Head");
print "  <option value=\"Head\"$Last>Luther Head</option>\n";
$Last = selected($Team[0], "Hunter");
print "  <option value=\"Hunter\"$Last>Lindsey Hunter</option>\n";
print "</select>\n";
print "</td>\n";

print "<td>\n";
print "<select name=\"SG\">\n";
$Last = selected($Team[1], "Head");
print "  <option value=\"Head\"$Last>Luther Head</option>\n";
$Last = selected($Team[1], "Barry");
print "  <option value=\"Barry\"$Last>Brent Barry</option>\n";
$Last = selected($Team[1], "Dickau");
print "  <option value=\"Dickau\"$Last>Dan Dickau</option>\n";
$Last = selected($Team[1], "Hunter");
print "  <option value=\"Hunter\"$Last>Lindsey Hunter</option>\n";
$Last = selected($Team[1], "Jefferson");
print "  <option value=\"Jefferson\"$Last>Richard Jefferson</option>\n";
$Last = selected($Team[1], "Jeffries");
print "  <option value=\"Jeffries\"$Last>Jared Jeffries</option>\n";
print "</select>\n";
print "</td>\n";

print "<td>\n";
print "<select name=\"SF\">\n";
$Last = selected($Team[2], "Jefferson");
print "  <option value=\"Jefferson\"$Last>Richard Jefferson</option>\n";
$Last = selected($Team[2], "Barry");
print "  <option value=\"Barry\"$Last>Brent Barry</option>\n";
$Last = selected($Team[2], "Jeffries");
print "  <option value=\"Jeffries\"$Last>Jared Jeffries</option>\n";
$Last = selected($Team[2], "Marion");
print "  <option value=\"Marion\"$Last>Shawn Marion</option>\n";
print "</select>\n";
print "</td>\n";

print "<td>\n";
print "<select name=\"PF\">\n";
$Last = selected($Team[3], "Marion");
print "  <option value=\"Marion\"$Last>Shawn Marion</option>\n";
$Last = selected($Team[3], "Jeffries");
print "  <option value=\"Jeffries\"$Last>Jared Jeffries</option>\n";
$Last = selected($Team[3], "Milicic");
print "  <option value=\"Milicic\"$Last>Darko Milicic</option>\n";
$Last = selected($Team[3], "Miller");
print "  <option value=\"Miller\"$Last>Brad Miller</option>\n";
$Last = selected($Team[3], "Mutombo");
print "  <option value=\"Mutombo\"$Last>Dikembe Mutombo</option>\n";
$Last = selected($Team[3], "Varejao");
print "  <option value=\"Varejao\"$Last>Anderson Varejao</option>\n";
print "</select>\n";
print "</td>\n";

print "<td>\n";
print "<select name=\"C\">\n";
$Last = selected($Team[4], "Miller");
print "  <option value=\"Miller\"$Last>Brad Miller</option>\n";
$Last = selected($Team[4], "Jeffries");
print "  <option value=\"Jeffries\"$Last>Jared Jeffries</option>\n";
$Last = selected($Team[4], "Marion");
print "  <option value=\"Marion\"$Last>Shawn Marion</option>\n";
$Last = selected($Team[4], "Milicic");
print "  <option value=\"Milicic\"$Last>Darko Milicic</option>\n";
$Last = selected($Team[4], "Mutombo");
print "  <option value=\"Mutombo\"$Last>Dikembe Mutombo</option>\n";
$Last = selected($Team[4], "Varejao");
print "  <option value=\"Varejao\"$Last>Anderson Varejao</option>\n";
print "</select>\n";
print "</td>\n";

print "</tr>\n";
print "</table>\n";

print "<input type=\"submit\" value=\"Calculate Lineup Value\">\n";
print "</form>\n";
print "</center>\n";

if ($Incomplete == 0) {
  system("$Root/code/lineup/offense < input > output");
  open (MYFILE, "output");
  $FirstLine = readline (MYFILE);
  ($Ignore, $Out) = split (/Fastbreak\?/, $FirstLine);
  print "$Out<br>";
  for ($i = 0; $i < 3; $i++) {
    $Out = readline (MYFILE);
    print "$Out<br>";
  }
  close (MYFILE); 
  unlink ("input");
  unlink ("output");
}

$Domain = $ENV{'SERVER_NAME'};
print "<p>";
print "<a href = \"http://$Domain/code/lineup/explanation.html\">Explanation of results</a>";
print "</p>";

print "</body>\n";
print "</html>\n";

end;

sub selected {
    if ($_[0] eq $_[1]) {
      return " selected"
    }
    return "";
}
