#!/usr/bin/perl
#
#
print "Content-type:text/html\n\n";
#
require ("cgi-lib.pl");
&ReadParse;   #get the form arguments.
#
# respond with an html file to the user.
#
$ok = "";

$ead=$in{'eaddress'};
($eid,$eho)=split("@",$ead);
if (length($eid)<1) { $ok="Invalid email address";goto cont1;}
if (length($eho)<1) { $ok="Invalid email address";goto cont1;}

# other checks here

cont1:
#
#  check that entries sum to 528
#
    $v=0;
for($i=1; $i<33; $i++)
{
    $l = $in{'l'.$i};
    if($l == ''){$l = 33;}
    if($l < 1){$l = 33;}
    if($l > 32){$l = 34;}
    $v = $v + $l;
}
if($v == 528){$ok = "";}
  else
{$ok.="<br>Ranks not entered correctly, please check.$v";}

if (length($ok) == 0) {
  ($sec,$min,$hou,$mday,$mon,$year,$wday,$yday,$isdst)=localtime(time);
  $year=$year+1900;
  $mon++;


  print <<WEB_PAGETOP;
<html>
<h4>
Rankings accepted for $ead at $year-$mon-$mday $hou:$min:$sec <p>
You may submit new rankings at any time.<BR>
The last submission received before June 12 will be the one that counts.<BR><BR>
Rankings submitted, correct if necessary by pressing BACK and resubmitting.<BR>
(Country, Rank):<BR>
WEB_PAGETOP

  # place a record in the data file
  open (DATAFILE, ">> /home/lrs/soccerpoolers.d");
  flock (DATAFILE, 2);
($sec,$min,$hou,$mday,$mon,$year,$wday,$yday,$isdst)=localtime(time);
  $year=$year+1900;
  $mon++;
for ($i=1; $i<33; $i++ )
{
    $l = $in{'l'.$i};
    if( $l eq '' ) {
    } else {
	printf DATAFILE "%04d%02d%02d%02d%02d%02d %-40s %5d %2d\n", $year, $mon, $mday, $hou, $min, $sec, $ead, $i, $l ;
	printf  "(%5d,%2d)\n", $i, $l ;
	print "<BR>";
    }
} 
  flock (DATAFILE, 8);
  close DATAFILE;

  print <<WEB_PAGEBOT;

</html>
WEB_PAGEBOT

#  open (MAIL, "| /usr/sbin/sendmail -oi -n -t" );
#print MAIL <<MAIL_MESSAGE;
#To:bszk\@uoguelph.ca
#Subject: Soccer pool submit
#From:$in{'eaddress'}
#
#$in{'eaddress'} has submitted data.
#MAIL_MESSAGE
#  close MAIL;
}

else {

print <<WEB_PAGE;
<html>
<h3>
Sorry, there is a problem with your submission:<br><br>
<font color=red>
<i>
$ok
</i>
</font>
<br><br>
Go back and try again.
</h3>
</html>
WEB_PAGE

}

exit;    # end of user script. 
