#! /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.
##
##**************************************************************

# new test now is a single file and does both local and vanilla universe
# bt 2/13/15

use CondorUtils;
use CondorTest;
use Cwd;
use POSIX;
use Check::SimpleJob;

$topdir = getcwd();
print scalar localtime() . "\n";

$testname = "job_foreach_submit-initialdir";

# where am I running
$currenthost = CondorTest::getFqdnHost();

$mypid = $$;

my $filedata = '
	this is data for an
	input file.
';

my $pid = $$;

my $datafolders = "";
if(is_windows()) {
	$datafolders = "$topdir\\jobfolders$pid"; 
} else {
	$datafolders = "$topdir/jobfolders$pid"; 
}


CreateDir("$datafolders");

my @foldername = (
	"why",
	"two",
	"three3",
	"forme",
);

my $counter = 0;
my $datafileswanted = 3;
my $datafilename = "job_foreach_submit-initialrun";
my $extension = ".testdata";
my $targetname = "";
my @results = ();
my @extraction = ();
my $extractionsize = 0;

foreach my $dirname (@foldername) {
	$counter = 0;
	CreateDir("$datafolders/$dirname");
	while($counter < $datafileswanted) {
		if(is_windows()) {
			$targetname = "$datafolders\\$dirname" . "\\$datafilename" . "$counter" . "$extension";
		} else {
			$targetname = "$datafolders/$dirname" . "/$datafilename" . "$counter" . "$extension";
		}
		open(FD,">$targetname") or die "Failed to open:$targetname:$!\n";
		print FD "$filedata";
		close(FD);
		$counter += 1;
	}
}

my $submitfile = "";
if(is_windows()) {
$submitfile = "
	executable = x_getsandbox.pl
	arguments = 3
	should_transfer_files = yes
	when_to_transfer_output = ON_EXIT
	log = $testname$mypid.log
	output = $testname$mypid.out
	error = $testname$mypid.err
	transfer_input_files = ./
	queue InitialDir matching DIRS( $datafolders\\* )
";
} else {
$submitfile = "
	executable = x_getsandbox.pl
	arguments = 3
	should_transfer_files = yes
	when_to_transfer_output = ON_EXIT
	log = $testname$mypid.log
	output = $testname$mypid.out
	error = $testname$mypid.err
	transfer_input_files = ./
	queue InitialDir matching DIRS ($datafolders/*/)
";
}

my $submitfilename = "$testname$mypid.cmd";
open(SF,">$submitfilename") or die "failed submit file write:$submitfilename:$!\n";
print SF "$submitfile";
close(SF);

my $datafilename = CondorTest::TempFileName("$testname");
my $cmdtorun = "condor_submit -dry-run $datafilename $submitfilename";
my $res = system("$cmdtorun");
LoadResults(\@results,$datafilename);

#runCondorTool("condor_submit -dry-run $submitfilename",\@results,2,{emit_output=>1});

# We are looking different paths for each user log and for the Iwd

DryExtract(\@results, \@extraction, "UserLog");

print "checking for unique userlogs\n";
my $foundit = 0;
foreach my $path (@extraction) {
	$foundit = 0;
	print "looking for:$requesteddir - ";
	foreach my $requesteddir (@foldername) {
		print "Candidate is :$path:\n";
		my $fullpath = "";
		if(is_windows()) {
			$fullpath = "$datafolders\\$requesteddir";
			$_ = $fullpath;
			s/\\/\\\\/g;
			$fullpath = $_;
		} else {
			$fullpath = "$datafolders/$requesteddir";
		}
		print "regx using $fullpath\n";
		if($path =~ /$fullpath/) {
			$foundit = 1;
			last;
		}
	}
	if($foundit == 1) {
		print "Good\n";
		RegisterResult(1,"test_name","$testname");
	} else {
		print "Bad\n";
		RegisterResult(0,"test_name","$testname");
	}
}

@extraction = ();
DryExtract(\@results, \@extraction, "Iwd");

print "checking for unique working directories\n";

foreach my $path (@extraction) {
	$foundit = 0;
	print "looking for:$requesteddir - ";
	foreach my $requesteddir (@foldername) {
		print "Candidate is :$path:\n";
		if(is_windows()) {
			$fullpath = "$datafolders\\$requesteddir";
			$_ = $fullpath;
			s/\\/\\\\/g;
			$fullpath = $_;
		} else {
			$fullpath = "$datafolders/$requesteddir";
		}
		print "regx using $fullpath\n";
		if($path =~ /$fullpath/) {
			$foundit = 1;
			last;
		}
	}
	if($foundit == 1) {
		print "Good\n";
		RegisterResult(1,"test_name","$testname");
	} else {
		print "Bad\n";
		RegisterResult(0,"test_name","$testname");
	}
}

CondorTest::EndTest();
print scalar localtime() . "\n";
my $result = 0;

if( $result != 0 )
{
	exit(1);
}

CondorTest::debug("$testname SUCCESS\n",1);
exit(0);

