#!/usr/bin/perl

# FILE:  sample5.cgi

print "Content-type:  text/html\n\n";

print "<html> <head>\n";
print "<title>You Submitted:</title>\n";
print "</head>\n";
print "<body background=\"bg.gif\">\n";

if ($ENV{REQUEST_METHOD} eq "POST")
{
  read(STDIN,$querystring,$ENV{CONTENT_LENGTH});
} 

@my_table = split(/&/,$querystring);


foreach $entry (0..$#my_table)
{
  #Convert pluses to spaces
  $my_table[$entry] =~ s/\+/ /g;

  #Split into key and value.
  ($key, $val) = split(/=/,$my_table[$entry],2); #splits on the first =.

  #Convert %XX from hex numbers to alphanumeric
  $key =~ s/%(..)/pack("c",hex($1))/ge;
  $val =~ s/%(..)/pack("c",hex($1))/ge;

  #Associate key and value
  $my_table{$key} .= " and " if (defined($my_table{$key})); # \0 is the multiple
                                                        #  separator
  $my_table{$key} .= $val;

#  print "key:  $key     value:  $my_table{$key}<br><br>\n";

};
print "<h4>Here is the information that you submitted:<br><br>\n</h4>";

$sport = $my_table{sport};
#$sport = "Field Hockey";
$date = $my_table{date};
#$date = "092798";
$headline = $my_table{headline};
#$headline = "Transy Whips Up on UK";
$kicker = $my_table{kicker};
#$kicker = "Yet Again the Team Spirit Shines Through!";
$result = $my_table{result};
$text = $my_table{text};
$stats = $my_table{stats};

print "<table border=5 bgcolor=\"FFCCFF\">";
print "<tr><td><b>Sport</b>:</td> <td>$sport</td></tr>\n";
print "<tr><td><b>Date</b>:</td>  <td>$date</td></tr>\n";
print "<tr><td><b>Headline</b>:</td>  <td>$headline</td></tr>\n";
print "<tr><td><b>Kicker</b>:</td>  <td> $kicker</td></tr>\n";
print "<tr><td><b>Result</b>:</td>  <td>$result</td></tr>\n";
print "<tr><td><b>Text</b>:</td>  <td>$text</td></tr>\n";
print "<tr><td><b>Stats</b>:</td>  <td>$stats</td></tr>\n";
print "</table>";
print "<br>";


print "If any of this information is incorrect, please use the BACK button\n";
print "<br>";
print "and correct the form.";

if ($sport eq  "Cross Country" )
{$subdir =cross; }

if ($sport eq "Field Hockey")
{ $subdir = fieldhockey;}

print "sub directory is $subdir.";
$idnumhtm = $date . ".htm";

$output = $idnumhtm;
print "<br>output file is $output";


open (INFILE, "newstemplate.htm") || die "Could not open file newstemplate.htm \n";

print "<br> file newstemplate.htm has been opened.";

open (OUTFILE, ">$output") || die "Could not open file $output \n";
`chmod 766 $output`;
print "<br> file $output has been opened.";

@lines = <INFILE>;
foreach $line (@lines)
{
  $line =~ s/#SPORT/$sport/ ;
  $line =~ s/#HEADLINE/$headline/ ;
  $line =~ s/#TEXT/$text/ ;
  $line =~ s/#STATS/$stats/ ;
  print OUTFILE $line;
}

close(INFILE);
close(OUTFILE);

open (FILE, ">athtemplate.htm") || die "Could not open file athtemplate.htm\n";
print "<br> file athtemplate.htm has been opened.";

open (OUTFILE, ">athletics.htm") || die "Could not open file athletics.htm\n";
print "<br> file athletics.htm has been opened.";

@lines = <INFILE>;
foreach $line (@lines)
{
  $line =~ s/#HEADLINE/$headline/;
  $line =~ s/#KICKER/$kicker/;
  $line =~ s/#DATE/$date/;
  print OUTFILE $line;
}
close(INFILE);
close(OUTFILE);
`cp athletics.htm athletics.new`;


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


