#!/bin/env perl

# This script will generate a set of job scripts suitable for
# submission to the JLab batch farm. Each job will process data
# for a specific momentum/angle. The values of these are determined
# by specifying blocks of points where the points are laid out
# on a grid. This allows the points to be targeted more to physical
# regions of phase space.


# Global job definition parameters
$TOP_DIR = "/work/halld/home/davidl/2013.01.28.cpp_acceptance";

$PROJECT = "gluex";
$TRACK = "simulation";
$COMMAND = "$TOP_DIR/farm/run_one.csh";
$OS = "centos62";
$JOBNAME = "cpp_vertex_z.1"; # will also be used for directory used for output files
$MULTI_JOBS = "1";
$CPU = "1";
$MEMORY = "2 GB";
$DISK_SPACE = "10 GB";

# Make output directory for job files (and eventually data files)
$outputdir = "$TOP_DIR/$JOBNAME";
print "Job files will be written to $outputdir\n";
mkdir($outputdir);

# Make the job files
$Njobfiles_created = 0;
&MakeBlock(1,80);


# Print final informational message to user
print "\n";
print "Generated $Njobfiles_created job files in:\n";
print "$outputdir\n";
print "\n";


#======================= Subroutines ============================

#---------------
# MakeBlock
#---------------
sub MakeBlock
{
	$Njobs		= $_[0];
	$Zstart = $_[0];
	$Zend   = $_[1];

	for($i=$Zstart; $i<=$Zend; $i+=2){
			
		$basename = sprintf("z%d", $i);

		$fname = $basename.".job";
		print "Making: $fname\n";

		open(FILE, ">$fname");

		print FILE "PROJECT: $PROJECT\n";
		print FILE "TRACK: $TRACK\n";
		print FILE "COMMAND: $COMMAND\n";
		print FILE "OS: $OS\n";
		print FILE "JOBNAME: $JOBNAME\n";
		print FILE "OPTIONS: $i\n";
		print FILE "INPUT_FILES:\n";
		print FILE "MULTI_JOBS: $MULTI_JOBS\n";
		print FILE "CPU: $CPU\n";
		print FILE "MEMORY: $MEMORY\n";
		print FILE "DISK_SPACE: $DISK_SPACE\n";

		close(FILE);
		$Njobfiles_created++;
	}
}		
