#!/usr/local/bin/perl

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

require  "/usr/faculty/professor/cjlin/server-3.0/server/lib/cgi-lib.pl";
$SERVER_NAME = "testneos";
$SERVER_BIN  = "/usr/faculty/professor/cjlin/server-3.0/server/bin";
$SERVER_VAR  = "/usr/faculty/professor/cjlin/server-3.0/server/var";

#
# Read in cgi data
#

&ReadParse();

#
# Configuration file for solver
#

$config[1] = "Solver Type      ::TYPE:NULL:TYPE";
$config[2] = "Solver Identifier::SOLVER:NULL:TOKEN";
$config[3] = "Solver Name      ::TITLE:NULL:TITLE";
$config[4] = "Contact Person (e-mail)::CONTACT:NULL:CONTACT";
$config[5] = "Password::PASSWORD:NULL:PASSWORD";
$config[6] = "Workstations (one per line):TEXT:BEGIN.STATIONS:END.STATIONS:STATIONS";
$config[7] = "Token Configuration File::BEGIN.CONFIG:END.CONFIG:config";
$config[8] = "E-mail Help File::BEGIN.HELP1:END.HELP1:help";
$config[9] = "Submission Tool Help File::BEGIN.HELP2:END.HELP2:socket-help";
$config[10] = "WWW Help File::BEGIN.HELP3:END.HELP3:explain";
$config[11] = "WWW Abstract::BEGIN.ABSTRACT:END.ABSTRACT:abstract";
$config[12] = "WWW Background URL::ABOUT:NULL:about.url";
$config[13] = "WWW Trial-Run File::BEGIN.TRIAL:END.TRIAL:trial_config";


#
# cd into WEB/ and create job.received
#

chdir("$SERVER_VAR/spool/WEB");
chop($tmpFile = `$SERVER_BIN/tempfile`);
open(handle, ">$tmpFile");
$USER = "WEB_USER\@".$ENV{REMOTE_HOST};
print handle "From: $USER\n\n";
print handle "type ADMIN\n";
print handle "solver ADDSOLVER\n";
if (length($in{"field.0"})>0) {
  $email =  $in{"field.0"}; 
  print handle "forward $email\n";
}
foreach $key (keys %in) {
  $key =~ /field.(\d+)/;
  next if ($1 eq "0");
  $data = $in{$key};
  if (length($data)>0) {
    $configLine = $config[$1]; 
    $configLine =~ /(.*):(.*):(.*):(.*):(.*)/;
    $beginToken = $3;
    $endToken =$4;
    if ($endToken =~ /^null$/i) {
      print handle "$beginToken = $data\n";
    } else {
      print handle "$beginToken\n";
      print handle "$data";
      print handle "$endToken\n";
    }
  }
}
close(handle);
system("chmod a+r $tmpFile");

#
# Move WEB/$tmpFile to WEB/$jobFile
#

$jobFile = "websub.$tmpFile";
system("mv $tmpFile $jobFile");

#
# Tell receiver there are submissions
#

system("touch MAIL");

#
# Wait for job number in WEB/$numberFile 
#

$numberFile = "$jobFile.number";
while (!-s "$numberFile") {sleep 1}
chop($JOB = `cat $numberFile`);
system("/bin/rm -f $numberFile");

#
# Start outputing HTML
#

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

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

while (!(-f "$SERVER_VAR/jobs/job.$JOB/job.out")) {
  sleep 1;
}
open(HANDLE, "$SERVER_VAR/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_VAR/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. to clear the screen and keep it refreshed
#

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");
}
