#! /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 CondorTest;
use CondorUtils;

$cmd_template = "job_core_basic_par.template";
$testdesc =  'job_core_basic-security_par - Simple parallel job with security on';
$testname = "job_core_basic-security_par";
$cmd = "job_core_basic_par.cmd";
$corename = "job_core_basic-security_par";

# where am I running
$currenthost = CondorTest::getFqdnHost();

print scalar localtime() . "\n";

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

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

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

CondorTest::debug("Master PID is $mypid\n",1);
CondorTest::debug("Master PID submit file is $mysubmitnm\n",1);

# get a remote scheduler running (side b)
my $configrem = CondorTest::StartPersonal( "job_core_basic-security_par", "x_param.basicparallel-security" ,"parallel");
my @parallel = split /\+/, $configrem;
my $parallelconfig = shift @parallel;
my $parallelport = shift @parallel;

CondorTest::debug("---Parallel config is $parallelconfig and remote port is $parallelport---\n",1);

$ENV{CONDOR_CONFIG} = $parallelconfig;

CondorTest::debug("Set config to $parallelconfig\n",1);

CondorTest::debug("Looking for schedd for Basic Parallel Test\n",1);
system("condor_status -pool $currenthost:$parallelport -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;
		CondorTest::debug("New log name: $lognm\n",1);
		print CSUB "log = $lognm\n";
	}
	elsif( $line =~ /^\s*output\s*= .*$/)
	{
		my $outputnm = $mysubmitnmout;
		CondorTest::debug("New out name: $outputnm\n",1);
		print CSUB "output = $outputnm\n";
	}
	elsif( $line =~ /^\s*error\s*= .*$/)
	{
		my $errornm = $mysubmitnmerr;
		CondorTest::debug("New error name: $errornm\n",1);
		print CSUB "error = $errornm\n";
	}
	else
	{
		CondorTest::debug("template: $line\n",1);
		print CSUB "$line\n";
	}
}
close(TPLATE);
close(CSUB);

my $result = system("./job_core_basic_par.pl $mysubmitnm");
CondorTest::debug("Result of basic paarallel  job is  $result\n",1);


system("cp *job_core_basic-security_par* $mysaveme");

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

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

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

CondorTest::KillPersonal($parallelconfig);
print scalar localtime() . "\n";

if($result != 0)
{
	# actuaal job failed but we allowed cleanup
	exit(1);
}

CondorTest::debug("job_core_basic-security_par SUCCESS\n",1);
exit(0);
