#! /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_startd_multimatch.template";
$testdesc =  'job_startd_multimatch - Startd Multimatch job';
$testname = "job_startd_multimatch";
$cmd = "job_startd_multimatch.cmd";
$corename = "job_startd_multimatch";
$localpostsrc = "x_startd_multimatch_postsrc";
$interval = 120;
$jobcount = 5;

# 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" . "\$(NODE)";
$mysubmitnmout = $corename . $mypid . ".out" . "\$(NODE)";
$mycorenm = $corename . ".cmd";
$mycorenmcmdout = $corename . ".cmd.out";
$mycorenmlog = $corename . ".log";
$mycorenmerr = $corename . ".err";
$mycorenmout = $corename . ".out";

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

#create a pid specific cmd file
#system("cp $mycorenm $mysubmitnm");
$myline = "";
open(NEWSUB,">$mysubmitnm") || die "Could not create test submit file $mysubmitnm: $!\n";
open(TEMPLATE,"<$mycorenm") || die "Could not open submit template: $mycorenm:$!\n";
while(<TEMPLATE>) {
	$myline = "$_";
	if($myline =~ /^.*queue.*$/) {
		print NEWSUB "queue  $jobcount\n";
	} elsif($myline =~ /^.*log\s*=.*$/) {
		print NEWSUB "log =  $mysubmitnmlog\n";
	} else {
		print NEWSUB "$myline";
	}
}
close(NEWSUB);
close(TEMPLATE);
open(LOCAL,">$localpostsrc") || die "Could not open local config addition:$!\n";
print LOCAL "NEGOTIATOR_INTERVAL = $interval\n";
close(LOCAL);

# get a remote scheduler running (side b)
my $configrem = CondorTest::StartPersonal( "job_startd_multimatch", "x_param.startdmultimatch" ,"startdmultimatch");
my @multimatch = split /\+/, $configrem;
my $multimatchconfig = shift @multimatch;
my $multimatchport = shift @multimatch;

CondorTest::debug("---Startd Multimatch config is $multimatchconfig and remote port is $multimatchport---\n",1);


$ENV{CONDOR_CONFIG} = $multimatchconfig;

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

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

exit(1);

system("cp *job_startd_multimatch* $mysaveme");

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

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

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

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

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

CondorTest::debug("job_startd_multimatch SUCCESS\n",1);
exit(0);
