#! /usr/local/bin/perl
# uspco - usp companion
# Written by Michael Sullivan
# 24 Jan 2002
# sullivan@rsc.anu.edu.au
#
# A program to submit multiple jobs with specified flags
# For example can do uspco SCN test*.com -lw 500mb -lt 1h -lf 200mb
# And it will submit all the jobs named test.com test1.com,etc with the specified flags
#

$queue = shift;
 
if ($queue =~ /^-h/) {            # HELP
    &Help();
    shift;
}
unless ($queue =~ /QC9|QC2|PALON|PCN|VPPN|VPPE|SCN|SCE|SCB/) {
    print "\nNed Flanders: Calm down, Neddilly-diddily-diddily-diddily.... \n";
    print "They did their best.... Shoddilly-iddily-iddily-diddly... \n";
    print "Gotta be nice.... hostility-ility-bility-dility- \n";
    print "Aw, hell, diddly-ding-dong-crap! Can't you morons do anything right? \n\n";
    
    print "You should have used a valid Queue! You used $queue\n";
    exit;
}

$totarg=$#ARGV+1;
#print "@ARGV\n";
for ($argnum=0;$argnum<$totarg;$argnum++){
    $jobnum++;
#    print ("argnum=$argnum numARGV=$totarg\n");
#    print ("FOR loop\n");
    if ($ARGV[$argnum] =~ /^-.+/){
	@flags=splice(@ARGV,$argnum);
	$argnum=$totarg;
#	print("if argnum=$argnum\n");
    }
}
$jobnum=$jobnum-1;
#print "jobnum=$jobnum";
#print "flags=@flags\n";
#print "args=@ARGV\n";
while (@ARGV) {                 # now process additional switches
#print "@ARGV\n";
#for ($i=1,$i=$jobnum,$i++){
#    print "job=$job\n";
#    $job=$ARGV[$i];
    $job = shift;
    $job =~ /(\S+)\.(.*)?/;            # separate the file name and extension
    $injob = $1; #Get basename
    $inext = $2; #Get extension
    if ($inext =~ /com|zmat|inp|ini/ ) {
	if (-e "$injob.log" || -e "$injob.alog"||-e "$injob.out"||-e "$injob.qout"||-e "$injob.SCN"||-e "$injob.SCB"||-e "$injob.SCE"||-e "$injob.QC2"||-e "$injob.QC9"||-e "$injob.PCN") {
	    print "Sorry output file for $job already exists\n";
	} else {
#	    system("/usr/local/adm/usp/usp $queue $job $compflags{$queue}");
#	    system("/usr/local/adm/usp/usp $queue $job @flags");
	    print("usp $queue $job @flags\n");
	    $output=`/usr/local/adm/usp/usp $queue $job @flags`;
	    $output=~s/.*(Submitting job.*)/$1/s;
	    print $output;
	    if (($queue eq "SCN" || $queue eq "SCE" || $queue eq "SCB")&&($output!~/\d+\.sc?/)){
		print ("$injob not submitted. Too many jobs?\n");
	    } else {
		system("touch $injob.$queue");
	    }
	    if (($queue eq "SCN" || $queue eq "SCE" || $queue eq "SCB")&&($inext eq "ini")){
		unlink ("SC-$injob.ini","SC-$injob.ini.bak","$injob.job");
	    }
	}
    }
}
print "Done! Thanks for using this fine product!\n";
sub Help
{
    print <<EOH;
_______________________________________________________________________________
                        uspco help.
_______________________________________________________________________________

General switches:
-h              this help page
 uspco - usp companion
 A program to submit multiple jobs with specified flags
 For example can do uspco SCN test*.com -lw 500mb -lt 1h -lf 200mb
 And it will submit all the jobs named test.com test1.com,etc with the specified flags
-Mike Sullivan
sullivan at rsc.anu.edu.au
http://www.rsc.anu.edu.au/~sullivan/

EOH
    exit;
}

