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


my $paramfile = "param_info.in";
my $testname = "lib_include";

my $expectedcount = 4;
my %expectedvalues = (
	"foo1" => "4",
	"foo2" => "5",
	"foo3" => "6",
	"foo4" => "7",
);

my %testing = (
	"foo1" => "lower case command",
	"foo2" => "lower case include file",
	"foo3" => "mixed case commmand",
	"foo4" => "upper case include file",
);

my @expected = (
	"foo1",
	"foo2",
	"foo3",
	"foo4",
);

sub ccv_getval {
	my $lookup = shift;
	my @response = ();
	my $cmd_retval = 0;
	my $res = runCondorTool("condor_config_val $lookup",\@response,2,{emit_output=>1,expect_result=>\ANY},\$cmd_retval);
	print "ccv_getval: lookup:$lookup res:$res cmd_retval:$cmd_retval\n";
	foreach my $lup (@response) {
		print "- $lup -\n";
	}
	if(($res == 1)&&($cmd_cmd_retval == 0)) {
		my $value = $response[0];
		fullchomp($value);
		print "returning $value\n";
		return($value);
	} else {
		my $value = $response[0];
		fullchomp($value);
		print "ERROR down below: $value\n";
		return("ccv of $lookup failed");
	}
};


my $firstappend_condor_config = '
	DAEMON_LIST = MASTER
	include : Includes/scriptfoo1_4 |
	include : Includes/includefoo2_5
	INClude : Includes/scriptfoo3_6 |
	INCLUDE : Includes/includefoo4_7
';

my $windowsappend_condor_config = '
	DAEMON_LIST = MASTER
	include : Includes\scriptfoo1_4.bat |
	include : Includes\includefoo2_5
	INClude : Includes\scriptfoo3_6.bat |
	INCLUDE : Includes\includefoo4_7
';

my $configfile = "";

if(CondorUtils::is_windows() == 1) {
	$configfile = CondorTest::CreateLocalConfig($windowsappend_condor_config,"libinclude");
} else {
	$configfile = CondorTest::CreateLocalConfig($firstappend_condor_config,"libinclude");
}

CondorTest::StartCondorWithParams(
	condor_name => "libinclude",
	fresh_local => "TRUE",
	condorlocalsrc => "$configfile",
	do_not_start => "TRUE",
);

my $fetch = "";
foreach my $term (@expected) {
	$fetch = "";
	$fetch = ccv_getval($term);
	print "Testing:$testing{$term} fetched value:$fetch\n";
	if($expectedvalues{$term} eq $fetch) {
		print "Good: $term expected as $expectedvalues{$term}\n"; 
		RegisterResult(1,"test_name",$testname);
	} else {
		print "Failure: $term expected as $expectedvalues{$term} NOT:$fetch\n"; 
		RegisterResult(0,"test_name",$testname);
	}
}


CondorTest::EndTest();

