#!/usr/local/bin/perl

#
# The variables in angle brackets are updated
# by the web-generation scripts.
#

$SERVER_NAME  = "testneos";
$SERVER_HOME  = "/usr/faculty/professor/cjlin/server-3.0";

$| = 1;  # turn off file buffering
read(STDIN, $buffer, $ENV{'CONTENT_LENGTH'});
$ENV{'CONTENT_TYPE'} =~ /boundary=(.+)/;
$boundary = $1;
@pairs = split (/$boundary/, $buffer);

$config[1] = "Comments:TEXT:BEGIN.COMMENT:END.COMMENT:comments";


chdir("$SERVER_HOME/server/receiving/WEB");
chop($FILE = `$SERVER_HOME/server/receiving/WEB/bin/tempfile`);
open(handle, ">$FILE");
print handle "From: $ENV{REMOTE_HOST}\n\n";
print handle "type MISC\n";
print handle "solver HELLO\n";
$count = 0;
foreach (@pairs) {
  s/(\r)//g;
  if(/Content-Disposition:(.+)\n\n((\s|\S)*)\n--/) {
    $disp = $1;
    $rest = $2;
    if ($disp =~ /field.0/) {
      print handle "FORWARD $rest\n";
      next;
    }
    if ($disp =~ /filename/) {
      chop($rest);
    }
    @field = split (/:/, $config[$count]);
    $field[2] =~ s/\s//g;
    $field[3] =~ s/\s//g;
    if ($rest =~ /\S/) {
      if ("\U$field[3]" eq "NULL") {
          print handle "$field[2] $rest\n";
      } else {
        print handle "$field[2]\n";
        print handle "$rest\n";
        print handle "$field[3]\n";
      }
    }
  }
  $count++;
}
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;

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

if (fork) {
  print "<META HTTP-EQUIV=\"Refresh\" CONTENT=\"1; URL=http://www.csie.ntu.edu.tw/~cjlin/neos//jobs/$JOB.html\">";
  exit 0;
} else {
  close STDOUT; close STDIN; close STDERR;
}

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


while (!(-f "$SERVER_HOME/jobs/job.$JOB/job.out")) {
  sleep 1;
}
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, ">>/usr/faculty/professor/cjlin/htdocs/neos/jobs/$JOB.html");
      print WEB "$_<br>\n";
      close(WEB);
    }
  }
  sleep 1;
} until (/END-SOFTWARE-OUTPUT/);

# Job results.

open(WEB, ">/usr/faculty/professor/cjlin/htdocs/neos/jobs/$JOB.html.tmp");
print WEB "<title>testneos Job #$JOB</title>\n"; 
print WEB "<center>\n";
print WEB "[<a href = \"http://www.csie.ntu.edu.tw/~cjlin/neos/\" >testneos Server</a>]";
print WEB "</center>\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 = \"http://www.csie.ntu.edu.tw/~cjlin/neos/\" >testneos Server</a>]";
print WEB "</center>\n";
close(WEB);
system("mv /usr/faculty/professor/cjlin/htdocs/neos/jobs/$JOB.html.tmp /usr/faculty/professor/cjlin/htdocs/neos/jobs/$JOB.html");

sub clear {
  open(handle, ">/usr/faculty/professor/cjlin/htdocs/neos/jobs/$JOB.tmp");
  print handle "<META HTTP-EQUIV=\"Refresh\" CONTENT=\"5; URL=http://www.csie.ntu.edu.tw/~cjlin/neos//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 /usr/faculty/professor/cjlin/htdocs/neos/jobs/$JOB.tmp /usr/faculty/professor/cjlin/htdocs/neos/jobs/$JOB.html");
}
