#!/usr/local/bin/perl 

require "cgi-lib.pl";
## require "cgi-utils.pl";
binmode STDIN;
&ReadParse(*input);

$remote_host=$ENV{'REMOTE_HOST'};
if ($remote_host =~/sdsc\.edu/) {
   print "Content-type: text/html\n\n";
##   print "<B>Status: 200 OK, You are from SDSC's domain.</B><BR><P>\n";
}
else {
   print "Content-type: text/html\n\n";
   print "<B>Status 400 Bad Request, You must be part of SDSC's domain.</B>\n";
   print "remote_host is $remote_host\n";
   exit(0);
}

########################### GLOBALS #################################

$webdir = "/misc/www/users/damartin/cgi-bin/temp/";
$topdir = "/projects/us/bio2/PWT/demo/";
$pwt_perl = "/misc/www/users/damartin/PWT/perl/";
$info_file = $webdir."temp.info";
$UPDATE = $pwt_perl."update.pl";


#-------------------------------------------------------------
# convert hex characters passed in directory value
#-------------------------------------------------------------

for $i (0..$#input) {
# convert hex versions of :,/,\ to ascii
   $input[$i] =~s/%(..)/pack("c",hex($1))/ge;
}

#-------------------------------------------------------------
# Print out input to see what we got
#-------------------------------------------------------------

@input1 = split(/=/, $input[0]);
## print "<PRE>";
## print "$input1[1]<BR>\n";
## print "</PRE>";


#-------------------------------------------------------------
# Read info file for which files to process
#-------------------------------------------------------------

# for error checking
if ( !(-e $info_file) ) {
   print "sequence database file $info_file doesn't exist!\n";
   print "Bailing out..\n";
   exit(1);
}

open(INFILE1, "<$info_file") || die "Unable to open filename \"$!\".\n";
while($line=<INFILE1>) {
    @fields = split(' ',$line);
    if ($fields[0] eq "directory") { $direct=$fields[1]; }
    elsif ($fields[0] eq "file") { $filena=$fields[1]; }
}
close INFILE1;  

#-------------------------------------------------------------
# Write out modified contents to a temporary query file temp.qry
#-------------------------------------------------------------
$tempqry=$webdir."temp.qry";
$outfilename = $tempqry;
## system ("rm $tempqry");

#-------------------------------------------------------------
# start creating output file
#-------------------------------------------------------------
if ( !open(OUTFILE,"> $outfilename") ) {
    print "Error opening an output file $outfilename: $!\n";
    print "Bailing out..\n";
    exit(1);
}

print OUTFILE "$input1[1]\n";
close OUTFILE;

#-------------------------------------------------------------
# Run update.pl to recompute node values and save results
# to original query file
#-------------------------------------------------------------

$qry_file="$direct"."$filena";

## system ("$update $tempqry $qry_file");
## system (" $UPDATE > $qry_file");
$var=`$UPDATE $tempqry $qry_file`;


#-------------------------------------------------------------
# save processed info to qry_file
#-------------------------------------------------------------
$outfilename=$qry_file;

if ( !open(OUTFILE,"> $outfilename") ) {
    print "Error opening an output file $outfilename: $!\n";
    print "Bailing out..\n";
    exit(1);
}

print OUTFILE "$var";
close OUTFILE;


open(FILE,$qry_file);
print "<PRE>";
while($line=<FILE>) {
   print "$line<BR>";
}
#print $var;
print "</PRE>";
close FILE;  


## system ("rm $tempqry");

