#! /usr/bin/env perl
##**************************************************************
##
## Copyright (C) 1990-2007, Condor Team, Computer Sciences Department,
## University of Wisconsin-Madison, WI.
## 
## Licensed under the Apache License, Version 2.0 (the "License"); you
## may not use this file except in compliance with the License.  You may
## obtain a copy of the License at
## 
##    http://www.apache.org/licenses/LICENSE-2.0
## 
## Unless required by applicable law or agreed to in writing, software
## distributed under the License is distributed on an "AS IS" BASIS,
## WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
## See the License for the specific language governing permissions and
## limitations under the License.
##
##**************************************************************

use strict;
use warnings;

use CondorTest;
use CondorUtils;

my $cmd_template = "job_core_chirp_par.template";
my $testdesc =  'job_core_chirp_par - Simple parallel job';
my $testname = "job_core_chirp_par";
my $cmd = "job_core_chirp_par.cmd";
my $corename = "job_core_chirp_par";
my $debuglevel = 2;

# where am I running
my $currenthost = `condor_config_val FULL_HOSTNAME`;
chomp($currenthost);

print scalar localtime() . "\n";

my $mypid = $$;
my $mysaveme = $corename . ".saveme";

if($mypid == 0) {
	die "Saveme setup for test failed\n";
}

my $mysubmitnm = $corename . $mypid . ".cmd";
my $mysubmitnmcmdout = $corename . $mypid . ".cmd.out";
my $mysubmitnmlog = $corename . $mypid . ".log" ;
my $mysubmitnmerr = $corename . $mypid . ".err" . "\$(NODE)";
my $mysubmitnmout = $corename . $mypid . ".out" . "\$(NODE)";
my $mycorenm = $corename . ".cmd";
my $mycorenmcmdout = $corename . ".cmd.out";
my $mycorenmlog = $corename . ".log";
my $mycorenmerr = $corename . ".err";
my $mycorenmout = $corename . ".out";

#print "Master PID is $mypid\n";
#print "Master PID submit file is $mysubmitnm\n";
my $version = "jobcorechirppar";

my $firstappend_condor_config = "
	DAEMON_LIST = MASTER,SCHEDD,COLLECTOR,NEGOTIATOR,STARTD
	ALL_DEBUG = 
	# set up dedicated scheduler
	# Adding Dedicated Scheduler parrallel Universe;
	DedicatedScheduler = \"DedicatedScheduler\@schedd$mypid$version\@$currenthost\"
	STARTD_ATTRS = \$(STARTD_ATTRS), DedicatedScheduler
	SCHEDD_DEBUG = D_FULLDEBUG
	SCHEDD_NAME = schedd$mypid$version
	NUM_CPUS = 8
	use FEATURE : StaticSlots
	SUSPEND = FALSE
";
 
my $configfile = CondorTest::CreateLocalConfig($firstappend_condor_config,"$version");

my $new_condor_instance = CondorTest::StartCondorWithParams(
	condor_name => "$version",
	fresh_local => "TRUE",
	condorlocalsrc => "$configfile",
);

runcmd("condor_status -schedd -format '%s\\n' Name");

# Edit submit file to submit to scheduler at remote (side b) schedd.
my $line;
open(CSUB,">$mysubmitnm") || die "Can't open new submit file $mysubmitnm: $!\n";
open(TPLATE,"<$cmd_template") || die "Can't open submit file template $cmd_template: $!\n";
while(<TPLATE>)
{
	CondorUtils::fullchomp($_);
	$line = $_;
	if( $line =~ /^\s*log\s*= .*$/)
	{
		my $lognm = $mysubmitnmlog;
		#print "New log name: $lognm\n";
		print CSUB "log = $lognm\n";
	}
	elsif( $line =~ /^\s*output\s*= .*$/)
	{
		my $outputnm = $mysubmitnmout;
		#print "New out name: $outputnm\n";
		print CSUB "output = $outputnm\n";
	}
	elsif( $line =~ /^\s*error\s*= .*$/)
	{
		my $errornm = $mysubmitnmerr;
		#print "New error name: $errornm\n";
		print CSUB "error = $errornm\n";
	}
	elsif( $line =~ /^(.*)basic_par_socket(.*)$/)
	{
		my $newargs = $1 . " " . $mypid . " " . $2; 
		#print "New  args are < $newargs >\n";
		print CSUB "$newargs\n";
	}
	else
	{
		#print "template: $line\n";
		print CSUB "$line\n";
	}
}
close(TPLATE);
close(CSUB);


# make test data
runcmd("./x_chirpio_mkdata.pl --message='Testing message'");
# remove marker files which pace parallel job
runcmd("rm -rf ALLDONE WRITEDONE");

my $cmdfinal = $mysubmitnm;

CondorTest::debug("Submit file for this test is $cmdfinal\n",$debuglevel);

my $aborted = sub {
	my %info = @_;
	my $done;
	die "Abort event not expected!\n";
};

my $held = sub {
	my %info = @_;
	my $cluster = $info{"cluster"};

	die "Held event not expected.....\n";
};

my $executed = sub
{
	my %args = @_;
	my $cluster = $args{"cluster"};

	CondorTest::debug("Parallel job executed\n",$debuglevel);
};

my $success = sub
{
	CondorTest::debug("Success: Parallel Test ok\n",$debuglevel);
};

CondorTest::RegisterExitedSuccess( $testname, $success);
CondorTest::RegisterExecute($testname, $executed);

if( CondorTest::RunTest($testname, $cmdfinal, 0) ) {
	CondorTest::debug("$testname: SUCCESS\n",$debuglevel);
	CondorTest::RegisterResult(1,"test_name","$testname");
} else {
	CondorTest::RegisterResult(0,"test_name","$testname");
}


#fails due to directory matching pattern 
runcmd("cp *job_core_chirp_par* $mysaveme", {expect_result=>\&ANY});

if( -e "$mysubmitnm" )
{
	runcmd("cp $mysubmitnm $mycorenm");
}

if( -e "$mysubmitnmout" )
{
	runcmd("cp $mysubmitnmcmdout $mycorenmcmdout");
}

if( -e "$mysubmitnmlog" )
{
	runcmd("cp $mysubmitnmlog $mycorenmlog");
}

print scalar localtime() . "\n";

CondorTest::EndTest();

exit(0);
