#!/usr/common/usg/languages/perl/5.16.0/bin/perl -w
#
# clusterSubmitJobs.pl by Jarrod Chapman <jchapman@lbl.gov> Tue Jun  9 08:33:52 PDT 2009
# Copyright 2009 Jarrod Chapman. All rights reserved.
#


use Getopt::Std;
my %opts = ();
my $validLine = getopts('j:q:s:P:Mm:t:EN:', \%opts);
my @required = ("j",);
my $nRequired = 0;
map {$nRequired += exists($opts{$_})} @required;
$validLine &= ($nRequired == @required);
if ($validLine != 1) {
    print "Usage: ./clusterSubmitJobs.pl <<-q queue>> <<-m memoryRequirement(GB)>> <<-t timeRequirement(h:m:s)>> <<-s startingJobID>> <-j jobFile> <<-P pe-slots>> <<-M(emtime?)>> <<-E(xclusive.c?)>> <<-N nJobsPerTask>>\n";
    exit;
}

my $startID = 1;
if (exists($opts{"s"})) {
   $startID = $opts{"s"};
}

my $peSlots = 0;
if (exists($opts{"P"})) {
   $peSlots = $opts{"P"};
}

my $memtime = 0;
if (exists($opts{"M"})) {
#OLDMEMTIME    $memtime = "/jgi/tools/bin/memtime ";
    $memtime = "/usr/common/jgi/jgibio/prod/DEFAULT/bin/memtime "; 
}

my $exclusive = 0;
if (exists($opts{"E"})) {
    $exclusive = 1;
}

my $nJobsPerTask = 1;
if (exists($opts{"N"})) {
    $nJobsPerTask = $opts{"N"};
}

my $queue = "normal.c";

my %queues = ();
$queues{"normal.c"} = 1;
$queues{"debug.c"} = 1;
$queues{"short.c"} = 1;
$queues{"background.c"} = 1;

my @validQueues = keys(%queues);

if (exists($opts{"q"})) {
   $queue = $opts{"q"};
   unless (exists($queues{$queue})) {
       die "Invalid queue.  Must be one of { @validQueues }.\n";
   }
}

my $memRequired = 6;
if (exists($opts{"m"})) {
   $memRequired = $opts{"m"};
}
$memRequired .= "G";

my $timeRequired = "4:0:0";
if (exists($opts{"t"})) {
   $timeRequired = $opts{"t"};
}

my $jobFile = $opts{"j"};

open (J, $jobFile) || die "Couldn't open $jobFile.\n";

my $jobID = $startID;
my $finishID = $startID;
while (my $line = <J>) {
    chomp $line;
    my $cmd = "";
    if ($memtime) {
	$cmd = $memtime;
    }
    $cmd .= $line; 

    if ($nJobsPerTask > 1) {
	for (my $j = 1; $j < $nJobsPerTask; $j++) {
	    my $nextLine = <J>;
	    if ($nextLine) {
		chomp $nextLine;
		$cmd .= " &\n";
		if ($memtime) {
		    $cmd .= $memtime;
		}
		$cmd .= $nextLine;
	    } else {
		last;
	    }
	}
    }

    my $cwd = `pwd`;
    chomp $cwd;
    my $jobDir = "$cwd/JOB_$jobID";
    my $script = "sgeJob$jobID.sh";

    doIt("mkdir $jobDir");
    open (S,">$jobDir/$script") || die "Couldn't open $jobDir/$script\n";
    print S "#!/bin/bash\n";
    print S "#\$ -S /bin/bash\n";
    print S "$cmd\n";
    close S;

    $finishID = $jobID;
    $jobID++;
    
}
close J;

my $cwd = `pwd`;
chomp $cwd;
my $arrayScriptFile = "arrayScript.sh";
open (A,">$arrayScriptFile") || die "Couldn't open $arrayScriptFile\n";
print A "#!/bin/bash\n";
print A "#\$ -S /bin/bash\n";
print A "cd $cwd/JOB_\$SGE_TASK_ID\n";
print A "/bin/bash ./sgeJob\$SGE_TASK_ID.sh\n";
close A;

my $arrayRange = "$startID-$finishID";

#my $sgeOpts = "-w e -P plant-assembly.p -l $queue,ram.c=$memRequired,h_rt=$timeRequired,h_vmem=$memRequired";
my $sgeOpts = "-w e -P plant-assembly.p -l ";
if ($exclusive) {
    $sgeOpts .= "exclusive.c,";
}
$sgeOpts .= "ram.c=$memRequired,h_rt=$timeRequired,h_vmem=$memRequired";
if ($peSlots) {
    $sgeOpts .= " -pe pe_slots $peSlots";
}

doIt("qsub $sgeOpts -cwd -t $arrayRange ./$arrayScriptFile > qsub.OUT");

sub doIt {
    my ($cmd) = @_; 
    print STDERR "$cmd\n";
    system $cmd;
}
   



       
