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

use CondorTest;
use CondorUtils;
use Cwd;
#use Time::localtime;
use strict;
use warnings;

my $cmd = "cmd_wait_shows.cmd";
my $debuglevel = 2;

my $pid = $$;
#runcmd("rm -rf $pid");
CreateDir("-p $pid");
my $curcwd = getcwd();
CondorTest::debug("current directory is $curcwd\n",$debuglevel);

my $socketname = "cmd_wait";
my $resultfile = $curcwd . "/" . $pid . "/results";
my $flowlog = $curcwd . "/" . $pid . "/cmd_wait_shows-timeout.log";
my $writelog = "./x_write_joblog_events.exe";

CondorTest::debug("Result File is $resultfile\n",$debuglevel);

####################################################################
####################################################################
#
#
####################################################################
####################################################################

# seed log with 1 runs
system("$writelog $flowlog submit 1") == 0 || die "$writelog failed\n";
print "Drop a submit event\n";

# Start condor_wait
my $spawncmd = "condor_wait -wait 10 $flowlog";
CondorTest::spawn_cmd($spawncmd, $resultfile);
print "Call condor_wait with 10 second timeout\n";

print "Sleep 20\n";
sleep(20);
# start 1 runs
system("$writelog $flowlog execute 1") == 0 || die "$writelog failed\n";

# end 1 runs
system("$writelog $flowlog terminated 1") == 0 || die "$writelog failed\n";
print "Drop execute and terminate event into the log\n";

print "Waiting for rersults - ";
while(!(-f $resultfile)) {
	CondorTest::debug("Waiting for $resultfile to exist\n",$debuglevel);
	sleep(2);
}

CondorTest::debug("$resultfile exits!\n",$debuglevel);

while( (-s $resultfile) == 0 ) {
	CondorTest::debug("Waiting for results\n",$debuglevel);
	sleep(2);
}

CondorTest::debug("$resultfile not empty!\n",$debuglevel);
print "ok\n";

print "Check results status - ";
open(RES,"<$resultfile") || die "Can not open <$resultfile>:$!\n";
my $line = "";
while(<RES>) {
	$line = $_;
	if($line =~ /^Exit\s+(\d+).*$/) {
		print "result of cmd_wait:$1\n";
		if($1 != 0) {
			CondorTest::debug("Exit status $1 != 0 expected. Good!\n",$debuglevel);
			print "ok\n";
			exit(0);
		} else {
			print "bad\n";
			CondorTest::debug("Exit status $1 == 0 not expected. bad!\n",$debuglevel);
			exit(1);
		}
	} else {
			print "bad\n";
			exit(1);
	}
}
