#! /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 CondorTest;
use CondorUtils;
use Check::SimpleJob;
use CheckOutputFormats;
use strict;
use warnings;

my $testname = "job_transform_names_test";
my $path_to_file;   # path to config file
my $path_to_dir;    # path to the dir of config file

my $pid = $$;
my $executable = "x_sleep.pl";
my $arguments = 60;
my $submit_content;
my $submitfile;
my @content;
my $transform;
my $result;

my $iswindows = CondorUtils::is_windows();

$path_to_dir = `condor_config_val LOCAL_DIR`;
$path_to_dir =~ s/\n//;
if ($iswindows) {
	$path_to_file = "$path_to_dir\\condor_config.local";
} else {
	$path_to_file = "$path_to_dir/condor_config.local";
}
$path_to_file =~ s/\n//;
print "$path_to_file\n";

$transform = 
"JOB_TRANSFORM_NAMES = BAD ILLEGAL ALWAYS NEVER ALWAYS_NEWSCRIPT
JOB_TRANSFORM_BAD \@=end
[
  set_NeverThere3 = \"Please work!\";
  12foo = asd\"as\";
  ugh = 7 \%\$ 8;
]
\@end
JOB_TRANSFORM_ILLEGAL \@=end
[
  requirements = illegal =?= true;
  set_ClusterId = -999;
]
\@end
JOB_TRANSFORM_ALWAYS \@=end
[
  requirements = True;
  set_AlwaysThere = \"Please work on \$(OPSYSANDVER)\";
  delete_NeverThere2 = true;
  eval_set_OnlyProcZero = ProcId == 0 ? \"Please work - proc zero\" : \"Please work - proc non-zero\"
]
\@end
JOB_TRANSFORM_NEVER \@=end
[
  requirements = False;
  set_NeverThere1 = \"Please work!\";
]
\@end
JOB_TRANSFORM_ALWAYS_NEWSCRIPT \@=end
  SET AlwaysThereNew = \"Please work on \$(OPSYSANDVER)\"
  DELETE NeverThereNew
\@end";

# write to config file
open my $in, '<', $path_to_file or print "Can't read old file: $!\n";
my $out;
if ($iswindows) {
	open $out, ">$path_to_dir\\condor_config_copy.local" or print "Can't write new file: $!\n";
} else {
	open $out, ">$path_to_dir/condor_config_copy.local" or print "Can't write new file: $!\n";
}
while (<$in>){
	print $out $_;
}
print $out $transform;
close $out;
close $in;

if ($iswindows) {
	rename $path_to_file, "$path_to_dir\\condor_config_orig.local";
	rename "$path_to_dir\\condor_config_copy.local", $path_to_file;
} else {
	rename $path_to_file, "$path_to_dir/condor_config_orig.local";
	rename "$path_to_dir/condor_config_copy.local", $path_to_file;
}


`condor_reconfig`;
if (wait_for_reconfig() eq 0) {
	print "Waited too long!!\n";
	exit(1);
}

$submit_content = 
"executable = $executable
hold = true
+NeverThere2 = \"Please work!\"
+illegal = \$(ILLEGAL:false)
queue 2";
$submitfile = "$testname$pid.sub";
emit_dag_files($testname, $submit_content, $pid);

`condor_submit hold=true illegal=true $submitfile`;
@content = `condor_q -nobatch`;
print @content;

if (how_many_entries(\@content) eq 0){
	RegisterResult(1, check_name => "illegal", test_name => $testname);
} else {
	print "        Error: there should be no job in the queue\n";
	RegisterResult(0, check_name => "illegal", test_name => $testname);
}
unlink $path_to_file;
if ($iswindows) {
	rename "$path_to_dir\\condor_config_orig.local", $path_to_file;
} else {
	rename "$path_to_dir/condor_config_orig.local", $path_to_file;
}

`condor_rm -all`;

print "####################################################################################################\n";

$transform = 
"JOB_TRANSFORM_NAMES = BAD ALWAYS NEVER ALWAYS_NEWSCRIPT
JOB_TRANSFORM_BAD \@=end
[
  set_NeverThere3 = \"Please work!\";
  12foo = asd\"as\";
  ugh = 7 \%\$ 8;
]
\@end
JOB_TRANSFORM_ALWAYS \@=end
[
  requirements = True;
  set_AlwaysThere = \"Please work on \$(OPSYSANDVER)\";
  delete_NeverThere2 = true;
  eval_set_OnlyProcZero = ProcId == 0 ? \"Please work - proc zero\" : \"Please work - proc non-zero\"
]
\@end
JOB_TRANSFORM_NEVER \@=end
[
  requirements = False;
  set_NeverThere1 = \"Please work!\";
]
\@end
JOB_TRANSFORM_ALWAYS_NEWSCRIPT \@=end
  SET AlwaysThereNew = \"Please work on \$(OPSYSANDVER)\"
  DELETE NeverThereNew
\@end";

# write to config file
open my $in1, '<', $path_to_file or print "Can't read old file: $!\n";
my $out1;
if ($iswindows) {
	open $out1, ">$path_to_dir\\condor_config_copy.local" or print "Can't write new file: $!\n";
} else {	
	open $out1, ">$path_to_dir/condor_config_copy.local" or print "Can't write new file: $!\n";
}
while (<$in1>){
	print $out1 $_;
}
print $out1 $transform;
close $out1;
close $in1;

if ($iswindows) {
	rename $path_to_file, "$path_to_dir\\condor_config_orig.local";
	rename "$path_to_dir\\condor_config_copy.local", $path_to_file;
} else {
	rename $path_to_file, "$path_to_dir/condor_config_orig.local";
	rename "$path_to_dir/condor_config_copy.local", $path_to_file;
}

`condor_reconfig`;
#if (wait_for_reconfig() eq 0) {
#	print "Waited too long!!\n";
#	exit(1);
#}

`condor_submit $submitfile`;
@content = `condor_q -nobatch`;
print @content;
my @q_content = `condor_q -l | grep Please`;
print @q_content;

if(how_many_entries(\@content) ne 2) {
	print "        Error: there should be 2 jobs in the queue\n";
	RegisterResult(0, check_name => "2 jobs", test_name => $testname);
} else {
	RegisterResult(1, check_name => "2 jobs", test_name => $testname);

	my $result1 = 0;
	if ($q_content[0] =~ /^AlwaysThere = \"Please work on \S+\"$/ &&
	    $q_content[1] =~ /^AlwaysThereNew = \"Please work on \S+\"$/ &&
	    $q_content[2] eq "OnlyProcZero = \"Please work - proc zero\"\n" &&
	    $q_content[3] eq $q_content[0] &&
	    $q_content[4] eq $q_content[1] &&
	    $q_content[5] eq "OnlyProcZero = \"Please work - proc non-zero\"\n") {
		$result1 = 1;
	} else {
		print "        should be :\n"
			. "\tAlwaysThere = \"Please work on \$(OPSYSANDVER)\"\n"
			. "\tAlwaysThereNew = \"Please work on \$(OPSYSANDVER)\"\n"
			. "\tOnlyProcZero = \"Please work - proc zero\"\n"
			. "\tAlwaysThere = \"Please work on \$(OPSYSANDVER)\"\n"
			. "\tAlwaysThereNew = \"Please work on \$(OPSYSANDVER)\"\n"
			. "\tOnlyProcZero = \"Please work - proc non-zero\"\n";
	}
	RegisterResult($result1, check_name => "Jobs have expected attributes", test_name => $testname);

	# check that OPSYSANDVER expanded in both old and new transforms to the same value
	#
	my $opsys0 = $q_content[0]; $opsys0 =~ s/^Always.+ = //;
	my $opsys1 = $q_content[1]; $opsys1 =~ s/^Always.+ = //;
	my $result2 = 0;
	if ($opsys0 eq $opsys1) {
		$result2 = 1;
	} else {
		print "        $opsys0 != $opsys1\n";
		print "        AlwaysThere should have the same value as AlwaysThereNew\n";
	}
	RegisterResult($result2, check_name => "OPSYSANDVER expands the same in new and old syntax routes", test_name => $testname);
}

`condor_rm -all`;

unlink $path_to_file;
if ($iswindows) {
	rename "$path_to_dir\\condor_config_orig.local", $path_to_file;
} else {
	rename "$path_to_dir/condor_config_orig.local", $path_to_file;
}

`condor_reconfig`;

CondorTest::EndTest();
exit(0);
