#!/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/";
$search_dir = "/projects/us/bio2/PWT/demo/src/sun/";
$pwt_perl = "/misc/www/users/damartin/PWT/perl/";
$info_file = $webdir."temp.info";
$search = $search_dir."search";
$result_file = $webdir."temp.out";
$seq_DB = $topdir."seq/gbrna.seq";
$ROC = $pwt_perl."ROC.pl";
$stats = $webdir."stats";

#-------------------------------------------------------------
# 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;  

#-------------------------------------------------------------
# Alert the user that this might take a while
#-------------------------------------------------------------

## print "<H2><B>Please Wait While your Request is Being Processed!</B><H2>\n";

#-------------------------------------------------------------
# Run Search Algorithm on genome file
#-------------------------------------------------------------

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

system ("rm $result_file");
system ("$search $seq_DB $qry_file > $result_file");
# print "search=$search<BR><P>";
# print "seq_DB=$seq_DB<BR><P>";
# print "qry_file=$qry_file<BR><P>";


#-------------------------------------------------------------
# Run ROC.pl on Search results output file 
#-------------------------------------------------------------

 system ("rm $stats ");
 system ("$ROC $result_file > $stats ");
# print "ROC=$ROC<BR><P>";
# print "result_file=$result_file<BR><P>";
# print "stat file=$stats<BR><P>";

open(FILE,$stats);
print "<PRE>";
while($input=<FILE>) {
   print "$input<BR>";
}
print "</PRE>";
close FILE;  


