#! /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-queueitemfrom-Stdin-run";

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

$mypid = $$;
my $kidspid = 0;
my $currentstate = "running";

my $counter = 0;
my $datafileswanted = 3;
my $datafilename = "from_file$mypid";
my $extension = ".Fnxmacro";
my $targetname = "";
my @results = ();
my @extraction = ();
my $extractionsize = 0;

print "Datafile for this test data is datafilename\n";

my @expect2see = (
	"Eat Food",
	"wash car",
	"Cook Beans",
);

$TimedCallback = sub {
	print "in TimedCallback with this state:$currentstate\n";
	if($currentstate ne "running test") {
		print "stuck in this system call:$currentstate\n";
		print "killing child:$childpid\n";
		kill 9, $childpid;
		$currentstate = "running test";
	}
};

my $filecontents = 
	'Eat Food
	wash car
	Cook Beans 
';

my $submitfile = "
	executable = x_print-args.pl
	output = \$(ACTION)$mypid.out
	should_transfer_files = yes
	when_to_transfer_output = ON_EXIT
	log = $testname$mypid.log
	Arguments = \$(ACTION) \$(ITEM)
	queue ACTION,ITEM from  -
";
	
my $nostdinsubmitfile = "
	executable = x_sleep.pl
	output = no.out
	should_transfer_files = yes
	when_to_transfer_output = ON_EXIT
	arguments = 60;
	log = $testname$mypid.log
	queue 3
";
my $submitfilename = "$testname$mypid";
open(SF,">$submitfilename") or die "failed to open $submitfilename:$!\n";
print SF "$submitfile";
close(SF);

my $nosubmitfilename = "no$testname$mypid";
open(SF,">$nosubmitfilename") or die "failed to open $nosubmitfilename:$!\n";
print SF "$nostdinsubmitfile";
close(SF);

open(FL,">$datafilename") or die "Failed to create $datafilename:$!\n";
print FL "$filecontents";
close(FL);

print "Just wrote this:\n";
system("cat $datafilename");

my $foo;
#CondorTest::RunTest($testname, $foo, 0) ; # just registers the test
CondorTest::RegisterTimed( $testname, $TimedCallback, 30 );


# child can not change the state
###$currentstate = "running test";

system("cat $datafilename | condor_submit $submitfilename");

sleep(15);

print "Test continuing\n";

my $jobsdone = "false";
my @queuestate = ();
my $remainingjobs = 0;
while($jobsdone eq "false") {
	@queuestate = ();
	$cmdstatus = runCondorTool("condor_q -tot -all", \@queuestate, 2, {emit_output=>1});
	$remainingjobs = CheckQueue(\@queuestate);
	if($remainingjobs == 0) {
		$jobsdone = "true";
	} elsif ($remainingjobs == -1) {
		print "No jobs line from condor_q\n";
		RegisterResult(0, "test_name", "$testname");
		last;
	} else {
		sleep(1);
	}
}

print "Lets do some validation\n";

my @argsprocessing = ();
foreach my $line ( @expect2see ) {
	@argsprocessing = ();
	@argsprocessing = split /\s/, $line;
	open(FT,"<$argsprocessing[0]$mypid.out") or die "Could not open output file:$argsprocessing[0]$mypid.out:$!\n";
	print "Opened $argsprocessing[0]$mypid.out expecting $line\n";
	my $read = <FT>;
	chomp($read);
	if($read ne "$argsprocessing[0]") {
		print "Read $read but expected $argsprocessing[0]\n";
		RegisterResult(0,"test_name", "$testname");
	} else {
		print "Good just read $read as expected\n";
		RegisterResult(1,"test_name", "$testname");
	}
	$read = <FT>;
	chomp($read);
	if($read ne "$argsprocessing[1]") {
		print "Read $read but expected $argsprocessing[1]\n";
		RegisterResult(0,"test_name", "$testname");
	} else {
		print "Good just read $read as expected\n";
		RegisterResult(1,"test_name", "$testname");
		system("rm $argsprocessing[0]$mypid.out");
	}
}


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

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

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

sub CheckQueue {
	my $queueref = shift;
	my $jobcount = 0;
	foreach my $line (@{$queueref}) {
		chomp($line);
		print "looking at:$line\n";
		if($line =~ /(\d+)\s+jobs;\s+/) {
			print "Jobs line seen:$line\n";
			$jobcount = $1;
			return($jobcount);
		} else {
			print "Not a jobs line:$line\n";
		}
	}
	return(-1);
}
