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

my $testname = "cmd_q_shows-name";
my $debuglevel = 2;
my $iswindows = CondorUtils::is_windows();

my $localappend_condor_config = '
	DAEMON_LIST = MASTER,SCHEDD,COLLECTOR,NEGOTIATOR,STARTD
	MASTER_NAME = master_local
	SCHEDD_NAME = master_local_schd
	STARTD_NAME = master_local_strtd
';

my $localconfigfile = CondorTest::CreateLocalConfig($localappend_condor_config,"cmd_q_shows-name-local");

print "localconfigfile:$localconfigfile\n";

my $condor_instance_local = CondorTest::StartCondorWithParams(
	condor_name => "cmd_q_shows-name-local",
	fresh_local => "TRUE",
	condorlocalsrc => "$localconfigfile",
);

my $collectorport = $condor_instance_local->GetCollectorAddress();
my $localconfig = $condor_instance_local->GetCondorConfig();

# where am I running
my $currenthost = `condor_status -af Machine -limit 1`;
chomp($currenthost);

my $primarycollector = $currenthost;
$primarycollector = $primarycollector . ":" . $collectorport;

#if($iswindows) {
	#my @nameparts = split /\./, $currenthost;
	#foreach my $name (@nameparts) {
		#print "aadr:$name\n";
	#}
	#$currenthost = $nameparts[0];
#}


my $sheddoneappend_condor_config = '
	SHARED_PORT_PORT = 0
	DAEMON_LIST = MASTER,SCHEDD,STARTD
	MASTER_NAME = master_scheddone
	SCHEDD_NAME = master_scheddone_schd
	STARTD_NAME = master_scheddone_strtd
';

my @readyconfigplus = ();

my $scheddoneconfigfile = CondorTest::CreateLocalConfig($sheddoneappend_condor_config,"cmd_q_shows-name-scheddone");

print "scheddoneconfigfile:$scheddoneconfigfile\n";
my @addedconfig =
(
"COLLECTOR_HOST = XXXXXXXXXXX"
);

foreach my $line (@addedconfig) {
	if($line =~ /COLLECTOR_HOST/) {
		$line = "COLLECTOR_HOST = $collectorport";
		push @readyconfigplus, $line;
		print "Storing collector addr:$collectorport\n";
	}
}

my $condor_instance_scheddone = CondorTest::StartCondorWithParams(
	condor_name => "cmd_q_shows-name-scheddone",
	fresh_local => "TRUE",
	condorlocalsrc => "$scheddoneconfigfile",
	append_condor_config_plus => \@readyconfigplus,
);

my $scheddoneconfig = $condor_instance_scheddone->GetCondorConfig();

# submit into scheddone
$ENV{CONDOR_CONFIG} = $scheddoneconfig;

my $mypid = $$;
my $bail = 0;
my $line = "";

my $abort = sub {
	print "abort from removing jobs ignored\n";
	return(0);
};

print "Start a job which runs from remote schedd - ";
my $forever = SimpleJob::RunCheck(
	duration => 3600,
	no_wait => 1,
	#on_abort => $abort,
);
if($forever != 0) {
	print "bad\n";
	CondorTest::debug("Bailing because running job failed to submit\n",$debuglevel);
	$bail = 1;
}
print "ok\n";

# submit into collector node schedd
$ENV{CONDOR_CONFIG} = $localconfig;



print "Start a job on hold from local schedd - ";
my $holdone;
if($bail != 1) {
my $holdone = SimpleJob::RunCheck(
	hold => "True",
	no_wait => 1,
	#on_abort => $abort,
);
	if($holdone != 0) {
		print "bad\n";
		CondorTest::debug("Bailing because running job failed to submit\n",$debuglevel);
		$bail = 1;
	}
}
print "after Start a job on hold from local schedd - ";
print "ok\n";

my $cmdstatus;
my @adarray;
my $sched1 = "master_local_schd" . "@" . $currenthost;;
my $sched2 = "master_scheddone_schd" . "@" . $currenthost;;

CondorTest::debug("Looking for sched1($sched1) and sched2($sched2)\n",$debuglevel);

# allow time for all the nodes to update the collector
# by allowing N attempts
my $nattempts = 8;
my $count = 0;
my $done = 0;
my $found1 = 0;
my $targets = 0;

print "Lets see what the collector knows about schedds\n";

my $scmd = "condor_status -sched";
$cmdstatus = CondorTest::runCondorTool($scmd,\@adarray,2,{emit_output=>1});
if(!$cmdstatus) {
	CondorTest::debug("Test failure due to Condor Tool Failure<$scmd>\n",$debuglevel);
	exit(1)
}


if($bail != 1) {
	my @schedds = ( $sched1, $sched2 );
	foreach my $schedd (@schedds) {
		@adarray = ();
		CondorTest::debug("Looking for condor_q -name $schedd\n",$debuglevel);
		while($count < $nattempts) {
			$found1 = 0;
			my $cmd = "condor_q -name $schedd";
			$cmdstatus = CondorTest::runCondorTool($cmd,\@adarray,2,{emit_output=>1});
	    	if(!$cmdstatus)
	    	{
	        	CondorTest::debug("Test failure due to Condor Tool Failure<$cmd>\n",$debuglevel);
				#CondorTest::KillPersonal($locconfig);
				#CondorTest::KillPersonal($scheddoneconfig);
	        	exit(1)
	    	}

			CondorTest::debug("Looking for schedd($schedd)\n",$debuglevel);

			foreach $line (@adarray) {
				if($line =~ /^.*($schedd).*$/) {
					CondorTest::debug("found $schedd: $line\n",$debuglevel);
					$targets += 1;
					$found1 = 1;
				} else {
					CondorTest::debug("skip: $line\n",$debuglevel);
				}
			}
			if($found1 == 1){
				CondorTest::debug("Happy and quitting\n",$debuglevel);
				$done = 1;
				last;
			} else {
				CondorTest::debug("Unhappy: <$found1> \n",$debuglevel);
			}

			$count = $count + 1;
			sleep($count * 5);
		}
		if($found1 == 0) {
			last;
		}
	}

	print "Checking that condor_q -name can see specific schedds - ";
	if($targets != 2) {
		print "bad\n";
		$bail = 1;
		#CondorTest::KillPersonal($locconfig);
		#CondorTest::KillPersonal($scheddoneconfig);
	}
	print "ok\n";
}

my $rmcmd = "condor_rm -all";
my @rmarray1 = ();
my @rmarray2 = ();

$cmdstatus = CondorTest::runCondorTool($rmcmd,\@rmarray1,2,{emit_output=>0});
if(!$cmdstatus)
{
	CondorTest::debug("Test failure due to Condor Tool Failure<$rmcmd>\n",$debuglevel);
	exit(1)
}

$ENV{CONDOR_CONFIG} = $scheddoneconfig;

$cmdstatus = CondorTest::runCondorTool($rmcmd,\@rmarray2,2,{emit_output=>0});
if(!$cmdstatus)
{
	CondorTest::debug("Test failure due to Condor Tool Failure<$rmcmd>\n",$debuglevel);
	exit(1)
}


if($bail == 1) {
CondorTest::RegisterResult(0);
} else {
CondorTest::RegisterResult(1);
}
CondorTest::EndTest();

exit(0);
