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


$topdir = getcwd();

$cmd_template = "job_core_wantcores.template";
$testdesc =  'job_core_wantcores - Simple parallel job';
$testname = "job_core_wantcores-kill";
$cmd = "job_core_wantcores.cmd";
$corename = "job_core_wantcores";

# 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";
}

$mysubmitnmlog = $corename . $mypid . ".log" ;
$mysubmitnmerr = $corename . $mypid . ".err" ;
$mysubmitnmout = $corename . $mypid . ".out" ;

$mycorenmlog = $corename . ".log";
$mycorenmerr = $corename . ".err";
$mycorenmout = $corename . ".out";

print "Master PID is $mypid\n";
#print "Master PID submit file is $mysubmitnm\n";

# Edit template config info for signal and location
my $figsrc = "x_wantcore_configpostsrc";
my $figsrctemp = $figsrc . ".template";
my $signal = "KILL";
my $line;
open(CSUB,">$figsrc") || die "Can't open new config info file<$figsrc>: $!\n";
open(TPLATE,"<$figsrctemp") || die "Can't config info template<$figsrctemp>: $!\n";
while(<TPLATE>)
{
	CondorUtils::fullchomp($_);
	$line = $_;
	if( $line =~ /^\s*KILLER\s*=\s*(.*)\s*$/)
	{
		my $lognm = $mysubmitnmlog;
		print "killer name is $1\n";
		print CSUB "KILLER = $topdir/$1\n";
	}
	elsif( $line =~ /^\s*KILLER_ARGS\s*= .*$/)
	{
		print "Set signal to $signal\n";
		print CSUB "KILLER_ARGS = $signal\n";
	}
	else
	{
		print "template: $line\n";
		print CSUB "$line\n";
	}
}
close(TPLATE);
close(CSUB);

# get a remote scheduler running (side b)
my $configrem = CondorTest::StartPersonal( "job_core_wantcores-kill", "x_param.wantcore" ,"wantcore");
my @wantcore = split /\+/, $configrem;
my $wantcoreconfig = shift @wantcore;
my $wantcoreport = shift @wantcore;
my $logdir = "";

if($wantcoreconfig =~ /^(.*)\/condor_config.*$/) {
	$logdir = $1 . "/log";
	print "LOGDIR is $logdir\n";
	runcmd("ls -l $logdir");
}

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

$ENV{CONDOR_CONFIG} = $wantcoreconfig;

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

runcmd("ls -l $logdir");
my $result = system("./job_core_wantcores-look.pl $logdir");
CondorTest::debug("Result of core file test  job is  $result\n",1);

#fails due to directory matching pattern
#system("cp *job_core_wantcores* $mysaveme");
runcmd("cp *job_core_wantcores* $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");
}

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

if( $result != 0 )
{
	exit(1);
}

CondorTest::debug("job_core_wantcores-kill SUCCESS\n",1);
exit(0);
