#!/usr/local/bin/perl

$| = 1;  # turn off file buffering

$SERVER_HOME = "/usr/faculty/professor/cjlin/server-3.0";
$base_href = "http://www.csie.ntu.edu.tw/~cjlin/neos/"; 
$base_dir = "/usr/faculty/professor/cjlin/htdocs/neos"; 
$server = "<SERVER>";
$post = "<POST>";

read(STDIN, $buffer, $ENV{'CONTENT_LENGTH'});
@pairs = split (/&/, $buffer);

chdir("$SERVER_HOME/server/receiving/WEB");
chop($FILE = `$SERVER_HOME/server/receiving/WEB/bin/tempfile`);

$URL = $pairs[0];

$_ = $pairs[1];
if (/^field.0=(.+)/) {
  $data = $1;
  $data =~ tr/+/ /;
  $data =~ s/%([0][Dd])//eg;
  $data =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C", hex($1))/eg;
  $EMAIL = $data;
} else {
  $EMAIL = "";
}

print "Content-type: text/html\n\n";
print "<HTML>\n";
print "<BODY>\n";

#unless ($EMAIL =~ /.+/) {
#print "Please provide an e-mail address and resubmit.<P>";
#print "</BODY>\n";
#print "</HTML>\n";
#system("/bin/rm -f $FILE");
#exit 0;
#}

$USER = $ENV{REMOTE_HOST}; 

open(HANDLE, ">$FILE");
print HANDLE "From: $USER\n\n";

$_ = $URL;
/^field.(\d+)=(.+)/;
$line = $1;
unless ($line eq "0") {
  $data = $2;
  $data =~ tr/+/ /;
  $data =~ s/%([0][Dd])//eg;
  $data =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C", hex($1))/eg;
  chdir("../url_get");
  if ($DATA = `./url_get $data`) {
     print HANDLE "$DATA\n";
     if ($EMAIL =~ /\S/) {
       print HANDLE "FORWARD $EMAIL\n\n";
       open(MAIL, "|/usr/lib/sendmail -oi -t");

print MAIL <<END;
To: $EMAIL
From: testneos <$server@<HOSTNAME>>
Subject: testneos Trial Run Submission

$DATA

END
close(MAIL);

   }
 } else {
    print "ERROR: Cannot open \"$data\"<br>\n";
    print "       Please correct and resubmit.\n";
    print "</BODY>\n";
    print "</HTML>\n";
    exit 0;
    system("/bin/rm -f $FILE");
  }
  chdir("../WEB");
}

close(HANDLE);
system("chmod a+r $FILE");
system("touch MAIL");
chdir("NUMBERS");
while (!-s "$FILE") {sleep 1}
chop($JOB = `cat $FILE`);
system("/bin/rm -f $FILE");

&clear;

if (fork) {
  print "<META HTTP-EQUIV=\"Refresh\" CONTENT=\"1; URL=$base_href/jobs/$JOB.html\">";
  exit 0;
} else {
  close STDOUT; close STDIN; close STDERR;
}

# Update html file with job.out until job.results is ready.


do {sleep 1} until (-f "$SERVER_HOME/jobs/job.$JOB/job.out");
open(HANDLE, "$SERVER_HOME/jobs/job.$JOB/job.out");
do {
  $_ = <HANDLE>;
  if (length($_)>0) {
    chop;
    if ((/CLEAR_SCREEN/)||(/END-SOFTWARE-OUTPUT/)) {
      &clear;
    } else {
      open(WEB, ">>$base_dir/jobs/$JOB.html");
      print WEB "$_<br>\n";
      close(WEB);
    }
  }
  sleep 1;
} until (/END-SOFTWARE-OUTPUT/);

# Job results.

open(WEB, ">$base_dir/jobs/$JOB.html.tmp");
print WEB "<title>testneos Job #$JOB</title>\n"; 
print WEB "<center>\n";
print WEB "[<a href = \"$base_href\" >testneos Server</a>]";
print WEB "</center>\n";
print WEB "<P><hr><br>\n";
print WEB "<PRE>\n";
$RESULTS = `cat $SERVER_HOME/jobs/job.$JOB/job.results`;
print WEB "$RESULTS\n";
print WEB "</PRE>\n";
print WEB "<center>\n";
print WEB "<P><hr><br>\n";
print WEB "[<a href = \"$base_href\" >testneos Server</a>]";
print WEB "</center>\n";
close(WEB);
system("mv $base_dir/jobs/$JOB.html.tmp $base_dir/jobs/$JOB.html");

sub clear {
  open(handle, ">$base_dir/jobs/$JOB.tmp");
  print handle "<META HTTP-EQUIV=\"Refresh\" CONTENT=\"5; URL=$base_href/jobs/$JOB.html\">\n";
  print handle "<TITLE>testneos Job #$JOB</title>\n";
  print handle "<BLINK> Keep waiting for the results. </BLINK>\n";
  print handle "<hr><P>\n";
  close (handle);
  system("mv $base_dir/jobs/$JOB.tmp $base_dir/jobs/$JOB.html");
}
