#! /usr/bin/env perl
#testreq: personal
##**************************************************************
##
## 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 strict;
use warnings;

use CondorTest;
use CondorUtils;

my $cmd = 'job_core_args_van.cmd';
my $testdesc =  'Arguments are preserved - vanilla U';
my $testname = "job_core_args_van";
my $debuglevel = 2;

# we can really expect some things not to match like some quotes
sub exceptadjust
{
	my $arg = shift;
	my $result = "";
	#print "Testing $arg\n";

	if( $arg =~ /(.)(.*)/ )
	{
		if( $1 eq "\\")
		{
			#print "Slash start: ";

			my $store1 = $1;
			my $store2 = $2;
			if( $2 =~ /\"(.*)/ )
			{
				$result = "\"$1";
			}
			elsif( $2 =~ /\'(.*)/ )
			{
				$result = "\'$1";
			}
			elsif( $2 =~ /\?(.*)/ )
			{
				$result = "\?$1";
			}
			elsif( $2 =~ /\\(.*)/ )
			{
				$result = "\\$1";
			}
			else
			{
				$result = "$store1$store2\n";
			}
		}
		else
		{
			return($arg);
		}
		return($result);
	}
	else
	{
		return($arg);
	}
};

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

	print "Test exited. about to examine args\n";
	# Pull out requested environment values
	my $args   = $info{"arguments"};
	if(!defined($args)) {
		die "Can't pull arguments from submit file";
	}
	CondorUtils::fullchomp($args);
	print "Get the original arguments to the job\n";
	my(@args_reqd) = split / /, $args;

	my $output = $info{"output"};
	open(IN, "<$output" ) || die "Can't open $output: $!\n";
	print "Open the output files where the job placed them.\n";

	# snarf up file in one huge chunk
	local $/;
	my $file = <IN>;
	close IN;

	# see which actual values were passed along
	my(@args_found) = split /\n/, $file;
	print "Comparing the two - ";
	if(scalar(@args_reqd) != scalar(@args_found)) {
		print "bad\n";
		die "Args reqd has " . scalar(@args_reqd) . " items but " .
		    "Args found has " . scalar(@args_found) . "items.\n";
	}
	my $i;
	for(my $i = 0; $i < scalar(@args_reqd); $i++) {
		CondorTest::debug("Checking $args_reqd[$i]...\n",$debuglevel);
		print "checking Nth arg $i:request<$args_reqd[$i]>found<$args_found[$i]>\n";
		CondorUtils::fullchomp($args_found[$i]);
		if($args_reqd[$i] ne $args_found[$i]) 
		{
			my $adjust = exceptadjust($args_reqd[$i]); # translate escapes
			print "After adjust:<$adjust>\n";
			if($adjust ne $args_found[$i])
			{
				print "bad\n";
				die "Argument " . $args_reqd[$i] . " was mangled into " .
			    	$args_found[$i] . "\n";
			}
		}
	}
	print "ok\n";
};


CondorTest::RegisterExitedSuccess( $testname, $ExitSuccess );

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


