#!/usr/bin/perl -w 
use strict ;
use FileHandle ;
use Getopt::Long;
use Cwd ;
use MyUtils;
use ConfigPDB;
use MyGeom;
  use Time::HiRes qw( usleep ualarm gettimeofday tv_interval
   clock_gettime clock_getres  clock
   );

use POSIX qw(floor);
my $commandline = util_get_cmdline("",\@ARGV) ;
my ($infile,$outfile,$which_tech,$listfile);
my (@expressions,$protein);
my $howmany = 100000 ;
GetOptions(
            "which_tech=s"=>\$which_tech ,
            "infile=s"=>\$infile ,
            "protein=s"=>\$protein ,
            "listfile=s"=>\$listfile ,
            "outfile=s"=>\$outfile ,
            "expr=s"=>\@expressions,
            "howmany=i"=>\$howmany ,
           );
die "Dont recognize command line arg @ARGV " if(@ARGV);
usage( "Need to give a output file name => option -outfile ") if(!defined $outfile);
my $ofh = util_write($outfile);
usage( "Need to give a input file name => option -protein ") if(!defined $protein);
usage( "Need to give a listfile of outconfs -option -listfile  ") if(!defined $listfile);
my $ofh = util_write($outfile);

my $CNT = 0 ; 
my ($RESULTDIR,$PDBDIR,$FASTADIR,$APBSDIR,$FPOCKET,$SRC) = util_SetEnvVars();
my $PWD = cwd;


my @list= util_read_list_sentences($listfile);
my $list = {};
map { $list->{$_} = 1 ; } @list ;

my  ($seconds, $microseconds) = gettimeofday;

my $dir = "$PWD/$protein.Results/";
`mkdir -p $dir ` ;

foreach my $i (@list){
   util_printAndDo(" cd $dir ; $SRC/3DMatch -find -outf $dir/$protein.$i.out -pd $PDBDIR/$protein.pdb -inconf $PWD/$i");
}


my  $t0 = [gettimeofday];
my $elapsed = tv_interval ( $t0, [$seconds, $microseconds]);
print  "Elapsed = $elapsed \n";
print $ofh  "Elapsed = $elapsed \n";



chmod 0777, $outfile ;
sub usage{
    my ($msg) = @_ ;
    print $msg , "\n" ; 
print << "ENDOFUSAGE" ; 
ENDOFUSAGE
    die ;
}
