#!/usr/bin/perl
#
sub htencode {
    local($htencd) = shift;
    $htencd =~ s/&/&amp;/g;
    $htencd =~ s/"/&quot;/g;
    $htencd =~ s/</&lt;/g;
    $htencd =~ s/>/&gt;/g;
    $htencd;
}
use CGI;
  do "acqPATHs.pl";
  $qform = CGI->new;
  $Prog = $qform->param("Prog");
  $Qidno = $qform->param("Qidno");
  $Action = $qform->param("Action");
  $Count = $qform->param("Count");
  $Pid = $qform->param("ProcID");
  if ($Pid eq "0") {
    $SPID = 0;
  } else {
    if (kill(0,$Pid)) { $SPID = 1; } else { $SPID = 2; }
  }
  print "Content-type: text/html; charset=iso-8859-1\n\n";
#
  if ($SPID == 2) {             # process completed
########
#------- Prog. execution completed -------
    $htmlbody =<<EOM;
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<meta http-equiv="Content-Script-Type" content="text/javascript">
<link rel="stylesheet" type="text/css" href="/AMXS/amxs.css">
<title>AMXS refresh</title>
<script type="text/javascript">
<!--
function start() {
  parent.Fbot.location.replace("/cgi/AMXS/complete.cgi?__PROG__+__QIDNO__");
}
function goback() {
  parent.Fbot.location.replace("/AMXS/forms/blank.html");
  location.replace("/AMXS/forms/axtop.html");
}
//-->
</script>
</head>
<body bgcolor="ivory" style="margin: 4px 7px;" onload="start();">
<big>Prog. <b>__PROG__</b> (QID: __QIDNO__) completed. </big> (__COUNT__s)<br>
<hr>
[ All outputs from <b>__PROG__</b> (QID: __QIDNO__) ]
EOM
#-------
    $htmlbody =~ s/__PROG__/$Prog/g;
    $htmlbody =~ s/__QIDNO__/$Qidno/g;
    $htmlbody =~ s/__COUNT__/$Count/g;
    print $htmlbody;
    if (open(IN, "< $HTMLPATH/fout/out.$Qidno")) {
      print "<pre>";
      while ($line=<IN>) { print &htencode($line); }
      close(IN);
      print "</pre>\n";
    } else {
      print "<br><font color=\"red\"><b>ERROR:</b></font>\n";
      print "out.$Qidno (IN) file open fail<br><br>\n";
    }
    print "<form name=\"FM\">\n";
    print "<input type=\"button\" value=\"OK\" onclick=\"goback()\">\n";
    print " &nbsp; ( Go back to Top Menu )\n";
    print "</form>\n</body>\n</html>\n";
########
  } else {
########
    if ($Action eq "Refresh") {
#======= Still running, Going to refresh inf. ========
      if ($SPID == 0) {
#------- Process ID not given yet
        $htmlbody =<<EOM;
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<meta http-equiv="Content-Script-Type" content="text/javascript">
<link rel="stylesheet" type="text/css" href="/AMXS/amxs.css">
<title>AMXS refresh</title>
<script type="text/javascript">
<!--
var Tid;
function start() { Tid = setTimeout('refresh()', 2000); }
function refresh() {
  if (parent.Frun.FM.ProcID)
    { document.FM.ProcID.value = parent.Frun.FM.ProcID.value; }
  document.FM.Count.value = parseInt(document.FM.Count.value) + 2;
  document.FM.submit();
}
//-->
</script>
</head>
<body bgcolor="ivory" style="margin: 4px 7px;" onload="start();">
<form name="FM" method="post" action="/cgi/AMXS/refresh1.cgi">
<big>Prog. <b>__PROG__</b> (QID: __QIDNO__) running. </big> (__COUNT__s)
 <input type="hidden" name="Prog" value="__PROG__">
 <input type="hidden" name="Qidno" value="__QIDNO__">
 <input type="hidden" name="Action" value="Refresh">
 <input type="hidden" name="Count" value="__COUNT__">
 <input type="hidden" name="ProcID" value="__PID__">
<hr>
[ Last 20 output lines from <b>__PROG__</b> (QID: __QIDNO__) ]
EOM
#-------
      } else {
#------- Process ID already given
        $htmlbody =<<EOM;
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<meta http-equiv="Content-Script-Type" content="text/javascript">
<link rel="stylesheet" type="text/css" href="/AMXS/amxs.css">
<title>AMXS refresh</title>
<script type="text/javascript">
<!--
var Tid;
function start() { Tid = setTimeout('refresh()', 2000); }
function refresh() {
  if (parent.Frun.FM.ProcID)
    { document.FM.ProcID.value = parent.Frun.FM.ProcID.value; }
  document.FM.Count.value = parseInt(document.FM.Count.value) + 2;
  document.FM.submit();
}
function abort() {
  clearTimeout(Tid);
  if (confirm("Are you sure to ABORT execution ?")) {
    var Pid = parent.Frun.FM.ProcID.value;
    var count = parseInt(document.FM.Count.value) + 1;
    parent.Fbot.location.replace("/cgi/AMXS/exabort.cgi?" + Pid +
                                 "+__PROG__+__QIDNO__+" + count);
  } else { Tid = setTimeout('refresh()', 2000); }
}
//-->
</script>
</head>
<body bgcolor="ivory" style="margin: 4px 7px;" onload="start();">
<form name="FM" method="post" action="/cgi/AMXS/refresh1.cgi">
<big>Prog. <b>__PROG__</b> (QID: __QIDNO__) running. </big> (__COUNT__s)
 <input type="button" value="ABORT execution" onclick="abort()">
 <input type="hidden" name="Prog" value="__PROG__">
 <input type="hidden" name="Qidno" value="__QIDNO__">
 <input type="hidden" name="Action" value="Refresh">
 <input type="hidden" name="Count" value="__COUNT__">
 <input type="hidden" name="ProcID" value="__PID__">
<hr>
[ Last 20 output lines from <b>__PROG__</b> (QID: __QIDNO__) ]
EOM
#-------
      }
#=======
    } else {
#======= Aborted, Going to refresh inf. ========
      $htmlbody =<<EOM;
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<meta http-equiv="Content-Script-Type" content="text/javascript">
<link rel="stylesheet" type="text/css" href="/AMXS/amxs.css">
<title>AMXS aborting</title>
<script type="text/javascript">
<!--
function start() { setTimeout('refresh()', 1000); }
function refresh() {
  if (parent.Frun.FM.ProcID)
    { document.FM.ProcID.value = parent.Frun.FM.ProcID.value; }
  document.FM.Count.value = parseInt(document.FM.Count.value) + 1;
  document.FM.submit();
}
//-->
</script>
</head>
<body bgcolor="ivory" style="margin: 4px 7px;" onload="start();">
<form name="FM" method="post" action="/cgi/AMXS/refresh1.cgi">
<big>Prog. <b>__PROG__</b> (QID: __QIDNO__) ABORT requested. </big>
 (__COUNT__s)
 <input type="hidden" name="Prog" value="__PROG__">
 <input type="hidden" name="Qidno" value="__QIDNO__">
 <input type="hidden" name="Action" value="Aborted">
 <input type="hidden" name="Count" value="__COUNT__">
 <input type="hidden" name="ProcID" value="__PID__">
<hr>
[ Last 20 output lines from <b>__PROG__</b> (QID: __QIDNO__) ]
EOM
#=======
    }
    $htmlbody =~ s/__PROG__/$Prog/g;
    $htmlbody =~ s/__QIDNO__/$Qidno/g;
    $htmlbody =~ s/__COUNT__/$Count/g;
    $htmlbody =~ s/__PID__/$Pid/g;
    print $htmlbody;
    if (open(IN, "tail -20 $HTMLPATH/fout/out.$Qidno 2>&1 |")) {
      print "<pre>";
      while ($line=<IN>) { print &htencode($line); }
      close(IN);
      print "</pre>\n";
    } else {
      print "<br><font color=\"red\"><b>ERROR:</b></font>\n";
      print "out.$Qidno (IN) file open fail\n";
    }
    print "</form>\n</body>\n</html>\n";
########
  }
  exit(0);
