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

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

$testdesc =  'job_flocking_to';
$testname = "job_flocking_to";
$corename = "job_flocking_to";

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

$primarycollector = $currenthost;
my @adarray;

print scalar localtime() . "\n";

$mypid = $$;
$mysaveme = $corename . ".saveme";

if($mypid == 0) {
	die "Saveme setup for test failed\n";
}

$mysubmitnm = $corename . $mypid . ".cmd";
$mysubmitnmout = $corename . $mypid . ".cmd.out";
$mysubmitnmlog = $corename . $mypid . ".log";
$mysubmitnmout = $corename . $mypid . ".out";
$mycorenm = $corename . ".cmd";
$mycorenmout = $corename . ".cmd.out";
$mycorenmlog = $corename . ".log";

CondorTest::debug("Master PID is $mypid\n",1);
CondorTest::debug("Master PID submit file is $mysubmitnm\n",1);

my $version = "flockto";
# get a local scheduler running (side a)
my $configloc = CondorTest::StartPersonal( "job_flocking_to", "x_param.flocktestflockto" ,$version);
my @local = split /\+/, $configloc;
my $locconfig = shift @local;
my $locport = shift @local;

CondorTest::debug("---local config is $locconfig and local port is $locport---\n",1);

$primarycollector = $primarycollector . ":" . $locport;

CondorTest::debug("Primary collector for other nodes <<$primarycollector>>\n",1);

$ENV{CONDOR_CONFIG} = $locconfig;
CondorTest::debug("New collector is this:\n",1);
runcmd("condor_config_val COLLECTOR_HOST");

my $line;
open(PARAMSRC,"<x_flockto_configpostsrc.template") || die "Could not open x_param.x_flockto_configpostsrc.template: $!\n";
open(PASRAMDONE,">x_flockto_configpostsrc") || die "Could not open x_flockto_configpostsrc: $!\n";
while(<PARAMSRC>) {
	$line = $_;
	chomp($line);
	if($line =~ /^\s*FLOCK_TO\s*=.*$/) {
		print PASRAMDONE "FLOCK_TO  = $primarycollector\n";
	} else {
		print PASRAMDONE "$line\n";
	}
}
close(PASRAMSRC);
close(PASRAMDONE);

$version = "flockfrom";
# get another node running
my $configflockto = CondorTest::StartPersonal( "job_flocking_to", "x_param.flocktestlocal" ,$version);
my @flockto = split /\+/, $configflockto;
my $flocktoconfig = shift @flockto;
my $flocktoport = shift @flockto;

CondorTest::debug("---home collector config is $flocktoconfig and collector port is $flocktoport---\n",1);

my $bail = 0;

my $flockstatus = CondorTest::verbose_system("./job_flocking_to.pl");
CondorTest::debug("Status of flocked job is \"$flockstatus\"\n",1);

CondorTest::debug("Saving things and cleaning up....\n",1);

#fails due to directory matching pattern.
runcmd("cp *job_flocking_to* $mysaveme", {expect_result=>\&ANY});

if( -e "$mysubmitnm" )
{
	runcmd("cp $mysubmitnm $mycorenm");
}

if( -e "$mysubmitnmout" )
{
	runcmd("cp $mysubmitnmout $mycorenmout");
}

if( -e "$mysubmitnmlog" )
{
	runcmd("cp $mysubmitnmlog $mycorenmlog");
}

CondorTest::KillPersonal($locconfig);
CondorTest::KillPersonal($flocktoconfig);
print scalar localtime() . "\n";

if( $flockstatus != 0 )
{
	# actual test failed but we allowed cleanup
	CondorTest::debug("job_flocking_to FAILED\n",1);
	print scalar localtime() . "\n";
	exit(1);
}

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