#!perllocation
# mathopt - receives POST data from math-form, checks it and 
#           passes it along to mathmethod
#
# NB: this file belongs in the directory configured as ScriptAlias /jg/
#     on this HTTPD server.  (typically htmlbin or optbin)
#     Generated from a template by a makefile in the source tree
#
# December 8, 1998, clh: correction to parsing of path_info to form
#     OBJ and EXT - was depending on an extension present - may not be
#  
require "cgi-lib.pl";
use LWP::Simple;
#
# i do not know how to get perl to do the equivalent of 
#   a csh's source command, so a workaround is build-opt-env.pl
#
do 'build-opt-env.pl';

$thisdir = "$topdir"."/"."$rundir";

&ReadParse(*input);

if ($input{'lhs1'} && $input{'rhs1'}) {
  $formula="$input{'lhs1'}".":="."$input{'rhs1'}";
}
else {
  if ($ENV{'QUERY_STRING'} && $ENV{'QUERY_STRING'} ne "") {
     system("$thisdir/math-form-error?$ENV{'QUERY_STRING'}");
     exit;
  } else {
     system("$thisdir/math-form-error");
     exit;
  }
}
print "Content-type: text/html\n\n";

foreach $pair (2,3,4) {
  $left="lhs";
  $right="rhs";
  $left.=$pair;
  $right.=$pair;
  if ($input{$left} && $input{$right}) {
     $formula2="$input{$left}".":="."$input{$right}";
     $formula.=","."$formula2";
  }
}
$tmp=$ENV{'PATH_INFO'};
# 
# if there is an extension on the object name, remove it
#
if (rindex($tmp,".") > rindex($tmp,"/")) {
  $OBJ=substr($tmp,0,rindex($tmp,"."));
  $EXT=substr($tmp,rindex($tmp,".")+1);
} else {
  $OBJ=$tmp;
  $EXT="html0";
}
$FULLOBJ=$OBJ;
$QS="";
delete $ENV{'PATH_INFO'};

if ($ENV{'QUERY_STRING'}) {
  $QS=`$ENV{'OPTHOME'}/bin/htmlesc "$ENV{QUERY_STRING}"`;
  $FULLOBJ="$OBJ"."("."$QS".")";
}

#
# below replaces transfer program - use 'get' from LWP package to fetch URL
#
$myaddr="$ENV{'MYADDR'}";
print (get "http://$myaddr/jg/serv/mathmethod.$EXT?$FULLOBJ,$formula");
