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

$cmd      = 'job_filexfer_twosets_van.cmd';
$testdesc =  'Base file transfer - two different sets of files supposed to be moved - vanilla U';
$testname = "job_filexfer_twosets_van";

# truly const variables in perl
sub IDLE{1};
sub HELD{5};
sub RUNNING{2};

my $job = $$;
my $baseoutfile = "submit_filetrans_output" . "$job";
my $oute = "$baseoutfile"."e.txt";
my $outf = "$baseoutfile"."f.txt";
my $outg = "$baseoutfile"."g.txt";
my $outh = "$baseoutfile"."h.txt";
my $outi = "$baseoutfile"."i.txt";
my $outj = "$baseoutfile"."j.txt";

my $alreadydone=0;

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

	CondorTest::debug("Running $cluster\n",1);

};

$success = sub
{
	CondorTest::debug("Base file transfer job!\n",1);
	if(!-f "$oute"){ print "$oute failed to arrive\n"; exit(1)};
	if(!-f "$outf"){ print "$outf failed to arrive\n"; exit(1)};
	if(!-f "$outg"){ print "$outg failed to arrive\n"; exit(1)};
	if(!-f "$outh"){ print "$outh failed to arrive\n"; exit(1)};
	if(!-f "$outi"){ print "$outh failed to arrive\n"; exit(1)};
	if(!-f "$outj"){ print "$outi failed to arrive\n"; exit(1)};
};

$abort = sub {
	CondorTest::debug("Job is gone now, cool.\n",1);
};

$timed = sub
{
	die "Job should have ended by now. file transfer broken!\n";
};

# make some needed files. All 0 sized and xxxxxx.txt for
# easy cleanup

my $job = $$;
CondorTest::debug("Process Id for this script is  $job\n",1);
my $basefile = "submit_filetrans_input" . "$job";
my $in = "$basefile".".txt";
my $ina = "$basefile"."a.txt";
my $inb = "$basefile"."b.txt";
my $inc = "$basefile"."c.txt";

my $inputdir = "job_"."$job"."_dir";

system("mkdir -p $inputdir");
system("touch $inputdir/$in");
system("touch $inputdir/$ina");
system("touch $inputdir/$inb");
system("touch $inputdir/$inc");

my $line = "";
my $args = "--job=$job --long ";

# pass pid for output file production
# open submitfile and fix
open(CMD,"<$cmd") || die "Can not open command file: $!\n";
open(NEWCMD,">$cmd.new") || die "Can not open command file: $!\n";
while(<CMD>)
{
	CondorUtils::fullchomp($_);
	CondorTest::debug("$_\n",1);
	$line = $_;
	if( $line =~ /^\s*input\s*=\s*job_\d+_dir\/([a-zA-Z_]+)\d*\.txt\s*$/)
	{
		my $input = "$1"."$job".".txt";
		CondorTest::debug("Input file was $1\n",1);
		print NEWCMD "input = $inputdir/$input\n"
	}
	elsif( $line =~ /^\s*transfer_input_files\s*=\s*.*$/ )
	{
		print NEWCMD "transfer_input_files = $inputdir/$ina,$inputdir/$inb,$inputdir/$inc\n";
	}
	elsif( $line =~ /^\s*transfer_output_files\s*=\s*.*$/ )
	{
		print NEWCMD "transfer_output_files = $oute,$outf,$outg\n";
	}
	elsif( $line =~ /^\s*arguments\s*=\s*.*$/)
	{
		print NEWCMD "arguments = $args\n";
	}
	else
	{
		print NEWCMD "$line\n";
	}

}
close(CMD);
close(NEWCMD);
system("mv $cmd.new $cmd");

CondorTest::RegisterExecute($testname, $execute);
CondorTest::RegisterAbort($testname, $abort);
CondorTest::RegisterExitedSuccess($testname, $success);
#CondorTest::RegisterTimed($testname, $timed, 3600);

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

