#! /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 Check::SimpleJob;
#use Check::CondorLog;
use Check::ProcInfo;

my $test_name =  "job_basic_param_config_check";

#testreq: personal
my $config = <<CONDOR_TESTREQ_CONFIG;
	DAEMON_LIST = MASTER,SCHEDD,COLLECTOR,NEGOTIATOR,STARTD
	NUM_CPUS = 5
	foo = 1
	bar = $(foo) + 1
	master.foo = 2
	GOO = baz
	master.GOO = $(GOO), bar
	ALL_DEBUG = 
CONDOR_TESTREQ_CONFIG
#endtestreq

my $result = 0;

my $fooindex = 0;
my @foos = (
	"1 + 1",
	"2 + 1",
	"baz, bar",
);

my @foodata = ();
my @goodata = ();
my $on_submit = sub {
	my $fdata = "";
	@foodata = ();
	if( $fooindex == 0 ) {
		@foodata = `condor_config_val bar`;
		system("condor_config_val bar");
	} elsif($fooindex == 1 ) {
		@foodata = `condor_config_val -master bar`;
		system("condor_config_val -master bar");
	} elsif($fooindex == 2 ) {
		@foodata = `condor_config_val -master goo`;
		system("condor_config_val -master goo");
	}
	$fdata = $foodata[0];
	chomp($fdata);
	if($fdata ne $foos[$fooindex]) {
		$fooindex += 1;
		die "Expected <$foos[$fooindex]> Got <$fdata>\n";
	} else {
		print "Got what we wanted <$fdata>\n";
		$fooindex += 1;
	}
	print "FOOINDEX now <$fooindex> \n";
	#@goodata = `condor_config_val -master goo`;
	#foreach my $line (@goodata) {
		#print "$line";
	#}
};

print "First test basic job\n";
$result = SimpleJob::RunCheck();

$result = SimpleJob::RunCheck(
	test_name => "$test_name",
	on_submit => $on_submit,
	duration => 10,
);

print "******************** Check for general value for param foo\n";
if($result == 1) {
print "ok\n\n\n";
} else {
print "bad\n\n\n";
}

my $result = SimpleJob::RunCheck(
	test_name => "$test_name",
	on_submit => $on_submit,
	duration => 10,
);

print "******************** Check for master specific value for param foo\n";
if($result == 1) {
print "ok\n\n\n";
} else {
print "bad\n\n\n";
}

my $result = SimpleJob::RunCheck(
	test_name => "$test_name",
	on_submit => $on_submit,
	duration => 10,
);

print "******************** Check for master specific value for compound param goo\n";
if($result == 1) {
print "ok\n\n\n";
} else {
print "bad\n\n\n";
}

#CondorTest::RegisterResult(1,{test_name => $test_name});

CondorTest::EndTest();


sub SubmitReturnCluster
{
	my $submitfile = shift;
	my $cluster = 0;

	my @output = `condor_submit $submitfile`;

	foreach my $line (@output) {
		chomp($line);
		if($line =~ /^.*cluster (\d+).*$/) {
			#print "$line\n";
			#print "returning $1\n";
			$cluster = $1;
			return($cluster);
		}
	}
	return(0);
}
