#!perl/

use File::Copy;
use File::Path;
use Getopt::Long;

my $tag;
my $runfile;
GetOptions ( 'tag=s' => \$tag, 'protocol=s' => \$runfile );


print "ClusterRun 1.0: Run a tgz collection of VECT files through rr.\n";

unless (defined $tag) {

    die("Needs option --tag=\"tag\" to identify runs\n");

}

print "Removing old ~/Jason/unfinished\n\t".`rm -fr /home/us/Jason/unfinished*`."\n";

print "./unfinished.tar.gz -> ~/Jason/unfinished.tgz\n";

copy("./data/unfinished.tar.gz","/home/us/Jason/unfinished.tgz") or die("Couldn't copy");

print "Expanding unfinished...\n";

chdir("/home/us/Jason/");

`tar -xzf unfinished.tgz`;

unless (-d "/home/us/Jason/unfinished/") { die("Couldn't untar /home/us/Jason/unfinished.\n"); }

chdir("/home/us/Jason/unfinished") or die("Couldn't chdir to unfinished.\n");

print "Loading perl scripts into unfinished.\n";

`cp /home/us/Jason/prl/* /home/us/Jason/unfinished/`;

unless (-f "/home/us/Jason/unfinished/setupMangleRun.pl") { die("Couldn't move pl scripts."); }

print "Setting up regular run with tag $tag...\n";

`sed s/keywest/$tag/ harvestkw.pl > harvest.pl`;
`sed s/keywest/$tag/ gleankw.pl > glean.pl`;

if (defined $runfile) {

    `sed s/quickrunv9JustR8.prl/$runfile/ generateCondorFiles.prl > newGenerateCondorFiles.pl`;
    print "Generating condor files which call $runfile ...\n";
    `perl newGenerateCondorFiles.pl`;

} else {

    print "Generating condor files which call quickrunv9JustR8.prl...\n";
    `perl generateCondorFiles.prl`;

}

print "Submitting condor files...\n";

`perl submitCondorFiles.prl`;

print "Done!\n";
    

