#!/usr/bin/perl
#
use CGI;
  do "acqPATHs.pl";
  $qform = CGI->new;
  $wdr = $qform->param("wdr");
  $rfnm = $qform->param("rfnm");
  $ascbin = $qform->param("ascbin");
  $handle = $qform->upload("lfnm");
  $rfnm =~ s/[^\.\w]+//g; $rfnm =~ s/^(\.)+//;
  if ($wdr ne "") { $wdr .= "/"; }
  $path = "$DATAPATH/$wdr$rfnm";
  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 upload2</title>
<script type="text/javascript">
<!--
function SelFnm() {
  parent.Ftop.document.FM.fnm.value = "__RFNM__";
  parent.Fhid.document.FM.fnm.value = "__RFNM__";
  location.replace("/AMXS/forms/blank.html");
}
function cancel() {
  location.replace("/AMXS/forms/blank.html");
}
//-->
</script>
</head>
<body bgcolor="ivory" style="margin: 4px 7px;">
<form name="FM">
<hr>
EOM
  $htmlbody =~ s/__RFNM__/$rfnm/g;
  print $htmlbody;
#
  if ($rfnm eq "") {
    print '<font color="red"><b>ERROR:</b></font>', "\n";
    print " destination filename empty. &nbsp;\n";
    print '<input type="button" value="cancel" onclick="cancel()"><br>', "\n";
  } elsif ($handle eq undef) {
    print '<font color="red"><b>ERROR:</b></font>', "\n";
    print " source file is not given. &nbsp;\n";
    print '<input type="button" value="cancel" onclick="cancel()"><br>', "\n";
  } elsif (-e $path) {
    print '<font color="red"><b>ERROR:</b></font>', "\n";
    print " file \"$rfnm\" already exists. &nbsp;\n";
    print '<input type="button" value="cancel" onclick="cancel()"><br>', "\n";
  } elsif (open(OUT, "> $path")) {
    if ($ascbin eq "bin") {
      while ($buf = <$handle>) { print OUT $buf; }
    } else {
      while ($buf = <$handle>) { $buf =~ s/\r\n/\n/; print OUT $buf; }
    }
    close(OUT);
    print "File was uploaded to \"$rfnm\" successfully. &nbsp;\n";
    print '<input type="button" value="OK" onclick="SelFnm()"><br>', "\n";
  } else {
    print '<font color="red"><b>ERROR:</b></font>', "\n";
    print " failed to create file \"$rfnm\". &nbsp;\n";
    print '<input type="button" value="cancel" onclick="cancel()"><br>', "\n";
  }
#
  print "</form>\n</body>\n</html>\n";
  exit(0);
