#!/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;
}
  $str = $ENV{"QUERY_STRING"};
  ($dir, $fnm) = split(/\+/, $str);
  if ($dir ne "") { $dir .= "/"; }
  if (open(IN, "./bin/drange $dir$fnm |")) {
    $_ = <IN>;
    if (/^OK/) { $err = 0; }
    else { $err = 1; $msg = $_; }
  } else {
    $err = 1; $msg = "drange.cgi: drange start fail.";
  }
  if ($err == 1) {
    print "Content-type: text/html; charset=iso-8859-1\n\n";
####
    $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 drange</title>
<script type="text/javascript">
<!--
function cancel() {
  location.replace("/AMXS/forms/blank.html");
}
//-->
</script>
</head>
<body bgcolor="ivory" style="margin: 4px 7px;">
<form><font color="red"><b>ERROR:</b></font> &nbsp; &nbsp; &nbsp; &nbsp;
<input type="button" value="OK" onclick="cancel()"><br>
__MESG__
</form>
</body>
</html>
EOM
####
    $htmlbody =~ s/__MESG__/$msg/;
    print $htmlbody;
  } else {
    print "Content-type: text/html; charset=iso-8859-1\n\n";
####
    $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 drange</title>
<script type="text/javascript">
<!--
function cancel() {
  location.replace("/AMXS/forms/blank.html");
}
//-->
</script>
</head>
<body bgcolor="ivory" style="margin: 4px 7px;">
<form name="FM">
<b>[ Data-Range &nbsp; __DFN__ ]</b> &nbsp; &nbsp; &nbsp; &nbsp;
 <input type="button" value="OK" onclick="cancel()">
<pre>
EOM
####
    $htmlbody =~ s/__DFN__/$dir$fnm/;
    print $htmlbody;
    while (<IN>) { print &htencode($_); }
    print "</pre>\n</form>\n</body>\n</html>\n";
  }
  close(IN);
  exit(0);
