#! /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;
use Sys::Hostname;

#testreq: personal
my $config = <<CONDOR_TESTREQ_CONFIG;
	# test assumes slots have constant memory size
	use FEATURE : STATICSLOTS
CONDOR_TESTREQ_CONFIG
#endtestreq

$cmd = 'job_core_macros-dollardollar_van.cmd';
$testdesc =  'condor_submit macros - vanilla U';
$testname = "job_core_macros-dollardollar_van";


$ExitSuccessBase = sub {
	my %info = @_;

};

my @lines;

sub load_up_lines
{
	open(IN,"<job_core_macros-dollardollar_van.out") || die "Can not open job_core_macros-dollardollar_van.out: $!\n";
	while(<IN>)
	{
		CondorUtils::fullchomp($_);
		push @lines, $_;
	}
};

my $statusmachines = "";
my $wontbe = "";
my $statusmemory = "";

$success = sub
{
    my %args = @_;
	my $cluster = $args{"cluster"};

	load_up_lines();
	foreach my $item ( @lines )
	{
		#CondorTest::debug("ARG -$item-\n",1);
		if($item =~ /^(.*\..*)$/) {
			print "MACHINE:$1\n";
			$statusmachines = $1;
		} elsif($item =~ /^(\/be\/here\/now).*$/) {
			print "WONTBE:$1\n";
			$wontbe = $1;
			# last arg quit looking for these three things
			last;
		} elsif($item =~ /^(\d+)$/) {
			$statusmemory = $1;
			print "MEMORY:$statusmemory\n";
		}
	}

	#foreach my $name (@lines) {
		#print "Line:$name\n";
	#}

	CondorTest::ParseMachineAds( $statusmachines );
	%ads = CondorTest::FetchMachineAds();
	my $machinemem = CondorTest::FetchMachineAdValue("memory");
	print "MEMORY from condor_status:$memory\n";
	my $wontbethere = CondorTest::FetchMachineAdValue("wontbethere");
	my $machinename = CondorTest::FetchMachineAdValue("machine");
	print "MACHINE from condor_status:$machinename\n";
	$_ = $machinename;
	s/"//g;
	$machinename = $_;

	if(!(defined $machinemem)) {
		RegisterResult(0,"test_name", $testname);
	} else {
		RegisterResult(1,"test_name", $testname);
		print "Good got Memory from machine ads:$machinemem\n";
	}
	
	if(defined $wontbethere) {
		RegisterResult(0,"test_name", $testname);
	} else {
		RegisterResult(1,"test_name", $testname);
		print "Good  WONTBE is env and arg only\n";
	}
	
	if(!(defined $machinename)) {
		RegisterResult(0,"test_name", $testname);
	} else {
		RegisterResult(1,"test_name", $testname);
		print "Good got machine from machine ads:$machinename\n";
	}
	
	if($statusmemory eq $machinemem) {
		RegisterResult(1,"test_name", $testname);
		print "Arg memory and condor_status memory agree\n";
	} else {
		RegisterResult(0,"test_name", $testname);
		print "BAD:Arg memory and condor_status memory disagree\n";
	}

	if($statusmachines eq $machinename) {
		RegisterResult(1,"test_name", $testname);
		print "Arg machine and condor_status machine agree\n";
	} else {
		RegisterResult(0,"test_name", $testname);
		print "BAD:Arg machine and condor_status machine disagree\n";
		print "Arg:$statusmachine vs Status:$machinename\n";
	}

	my $envmemfound = 0;
	my $envmachinefound = 0;
	my $envwontbefound = 0;

	foreach my $name (@lines) {
		#print "examine $name\n";
		if( $name =~ /^\s*MEMORY\s+=\s+(\d+).*$/ ) {
			if( $1 eq $machinemem) {
				$envmemfound = 1;
				RegisterResult(1,"test_name", $testname);
				print "MEMORY matched\n";
			} else {
				print "Arg:$machinemem and Env:$1 MISMATCH\n";
				RegisterResult(0,"test_name", $testname);
			}
		}
		elsif( $name =~ /^\s*MACHINE\s+=\s+(.*\..*)\s*$/ )
		{
			if( $1 eq $machinename) {
				$envmachinefound = 1;
				RegisterResult(1,"test_name", $testname);
				print "MACHINE matched\n";
			} else {
				print "Arg:$machinename and Env:$1 MISMATCH\n";
				RegisterResult(0,"test_name", $testname);
			}
		}
		elsif( $name =~ /^\s*WONTBE\s+=\s+(.*?now).*$/ )
		{
			if( $1 eq $wontbe) {
				$envwontbefound = 1;
				RegisterResult(1,"test_name", $testname);
				print "WONTBE matched\n";
			} else {
				print "Arg:$wontbethere and Env:$1 MISMATCH\n";
				RegisterResult(0,"test_name", $testname);
			}
		}
	}

	#my $envmemfound = 0;
	#my $envmachinefound = 0;
	#my $envwontbefound = 0;

	if($envmemfound == 0) {
		RegisterResult(0,"test_name", $testname);
		print "Failed to find MEMORY in the jobs environment\n";
	}

	if($envmachinefound == 0) {
		RegisterResult(0,"test_name", $testname);
		print "Failed to find MACHINE in the jobs environment\n";
	}

	if($envwontbefound == 0) {
		RegisterResult(0,"test_name", $testname);
		print "Failed to find WONTBE in the jobs environment\n";
	}

	EndTest();

};

#CondorTest::RegisterExecute($testname, $execute);
#CondorTest::RegisterExitedSuccess( $testname, \&CondorTest::DefaultOutputTest );
CondorTest::RegisterExitedSuccess( $testname, $success );


if( CondorTest::RunTest($testname, $cmd, 0) ) {
	CondorTest::debug("$testname: SUCCESS\n",1);
	exit(0);
} else {
	die "$testname: CondorTest::RunTest() failed\n";
}

