#! /usr/bin/env perl
use Getopt::Long;
use Cwd;

my $chunksz = $ARGV[0];

#
#	Forestry job. This script takes information provided by the two setup
#	programs which create the result matrices and size the current image.
#	To have this run effectively on our pool, we can only process so many
#	lines of each image at one time as a Vanilla Condor job. So this script 
#	looks at the image in the current directory and sets up the DAG which
#	will correctly process the image with the passed in window and scan count.
#

if(!$chunksz)
{
	help();
	die "Scan lines to process must be passed in!";
}

GetOptions 
(
		'help' => \$help,
);

if(! -f "lowerlimrow.csv") { die "<<lowerlimrow.csv>> must exist!!!!!<setup.dag must have failed>\n"; }
if(! -f "upperlimrow.csv") { die "<<upperlimrow.csv>> must exist!!!!!<setup.dag must have failed>\n"; }
if(! -f "WINDOWSZ") { die "<<WINDOWSZ>> must exist!!!!!<setup.dag must have failed>\n"; }

if($help) { help() and exit(0); }

my $window = "";
my $low = 0;
my $high = 0;
my $imagenm = "";
open(WINDOW,"<WINDOWSZ") || die "Can't open <<WINDOWSZ>>: $!\n";
open(IMAGE,"<IMAGENM") || die "Can't open <<IMAGENM>>: $!\n";
open(LOW,"<lowerlimrow.csv") || die "Can't open <<lowerlimrow.csv>>: $!\n";
open(HIGH,"<upperlimrow.csv") || die "Can't open <<upperlimrow.csv>>: $!\n";
while(<LOW>)
{
	my $line = "";
	chomp();
	$line = $_;
	#print "Found<<$line>>\n";
	if($line =~ /(\d+)/)
	{
		$low = $1;
		print "LOW set to $low\n";
	}
}

while(<HIGH>)
{
	my $line = "";
	chomp();
	$line = $_;
	#print "Found<<$line>>\n";
	if($line =~ /(\d+)/)
	{
		$high = $1;
		print "HIGH set to $high\n";
	}
}

while(<WINDOW>)
{
	my $line = "";
	chomp();
	$line = $_;
	#print "Found<<$line>>\n";
	if($line =~ /(\d+)/)
	{
		$window = $1;
		print "WINDOW set to $window\n";
	}
}

while(<IMAGE>)
{
	my $line = "";
	chomp();
	$line = $_;
	print "Found<<$line>>\n";
	if($line =~ /^(.*)$/)
	{
		$imagenm = $1;
		print "IMAGE NAME set to $imagenm\n";
	}
}

close(WINDOW);
close(IMAGENM);
close(LOW);
close(HIGH);

if( ($high == 0) || ($low == 0))
{
	die "Neither LOW($low) nor HIGH($hign) can be zero\n";
}

my $letter = "node";
my $counter = 1;
my $currentlow = $low;
my $currenthigh = $low + $chunksz;
my $submitfilename = "";

while($currenthigh <= $high)
{
	print "Process $currentlow $currenthigh $window\n";
	
	$submitfilename = $letter . "$counter";

	my $line = "";
	my $newoutname = "secorder" . $currenthigh;
	open(TEMPLATE,"<secorder.cmd.template") || die "Can not open secorder template!<$!>\n";
	open(SUBMIT,">$submitfilename.cmd") || die "Can not open submit file <<$submitfilename.cmd>>: <$!>\n";
	while(<TEMPLATE>)
	{
		chomp();
		$line = $_;
		print "$line\n";
		if($line =~ /(.*)IMAGENM\s+START\s+STOP\s+WINDOWSZ(.*)$/)
		{
			print SUBMIT "$1$imagenm $currentlow $currenthigh $window $2\n";
		}
		elsif($line =~ /(.*)IMAGENM(.*)$/)
		{
			print SUBMIT "$1$imagenm$2\n";
		}
		elsif($line =~ /(.*)secorder(.out.*)/)
		{
			print SUBMIT "$1$newoutname$2\n";
		}
		elsif($line =~ /(.*)secorder(.err.*)/)
		{
			print SUBMIT "$1$newoutname$2\n";
		}
		elsif($line =~ /(.*)secorder(.log.*)/)
		{
			print SUBMIT "$1$newoutname$2\n";
		}
		else
		{
			print SUBMIT "$line\n";
		}
	}
	close(TEMPLATE);
	close(SUBMIT);
	$counter += 1;
	# are we doone?
	if($currenthigh == $high)
	{
		last;
	}

	$currentlow += ($chunksz + 1);
	$currenthigh += ($chunksz + 1);
	if($currenthigh > $high)
	{
		$currenthigh = $high;
	}

}

# run the main dag now.....
system("./mkdag.pl --run");

# =================================
# print help
# =================================

sub help
{
	print "Usage: calcnodecnt.pl scanlinesperprocess windowsz
		Options:
			[-h/--help]                             See this
			\n";
}

