#! /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 Cwd;
use File::Temp;

$flowlog = "job_core_priority_van.data";
$cmd = "job_core_priority_van.cmd";

my $curcwd = getcwd();
my $dir = File::Temp->newdir(CLEANUP => 0, DIR => $curcwd);
CondorTest::debug("current directory is $curcwd\n",1);

my $socketname = "priority";
my $newsocketname = $dir->dirname . "/priority";
CondorTest::debug("current directory is $newsocketname\n",1);

# start a server for this test

system("rm -f $flowlog");
defined(my $pid = fork) or die "Cannot fork: $!";
unless ($pid)
{
    #child runs and ceases to exist
    exec "./x_general_server.pl $newsocketname $flowlog";
    die "can not exec dumpcore!\n";
}

#main process continues here
sleep(60);

@filestoedit = qw/job_core_priority2/;
@filestochmod = qw/job_core_priority2.pl/;

# now make sure each place using socket refers to temporary directory!
foreach $name (@filestoedit)
{
	CondorTest::debug("Correcting socket name in $name\n",1);
	open(FF,"<$name.template") || die "Failed to open $name.template: $!\n";
	open(NEWFF,">$name.pl") || die "Failed to open $name.pl: $!\n";
	my $line = "";
	while(<FF>)
	{
		CondorUtils::fullchomp($_);
		$line = $_;
		if( $line =~ /(^\s*.*)$socketname(.*$)/)
		{
			CondorTest::debug("Found ---$1$socketname$2---\n",1);
			CondorTest::debug("replacing with $1$newsocketname$2\n",1);
			print NEWFF "$1$newsocketname$2\n";
		}
		else
		{
			print NEWFF "$line\n";
		}
	}
	close(FF);
	close(NEWFF);
}

foreach $name (@filestochmod)
{
	system("chmod 755 $name");
}

$testdesc =  'condor_submit priority works - vanilla U';
$testname = "job_core_priority_van";
$cmd = 'job_core_priority_van.cmd';

my $killedchosen = 0;
my $runcount = 6;
my $started = 0;

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

$abnormal = sub 
{

	die "Want to see only submit, execute and successful completion\n";
};

$aborted = sub 
{
	die "Abort event NOT expected\n";
};

$held = sub 
{
	die "Held event NOT expected\n";
};

$executed = sub
{
	$started = $started + 1;
	CondorTest::debug("Good. We need the dag to run(count now $started)\n",1);
};

$submitted = sub
{
	CondorTest::debug("submitted: This test will see submit, executing and successful completion\n",1);
};

@outcome = qw/1 2 3 5 8 10 quit/;
@results;

$success = sub
{
	my $scriptcounter = 0;
	my $postcounter = 0;
	my $precounter = 0;
	my $line = "";
	$runcount = $runcount - 1;
	CondorTest::debug("executed successfully - $runcount left\n",1);
	open(FLOW,"<$flowlog") || die "Can not open flowlog: $!\n";
	# shutdown the server we started
	if($runcount == 0) {
		sleep(10);
    	system("./x_general_client.pl $newsocketname quit");
		sleep(10);
		while(<FLOW>)
		{
			my $ret;
			CondorUtils::fullchomp($_);
			$ret = $_;
			push @results, $ret;
			CondorTest::debug("processing result $ret\n",1);
		}
		my $value;
		foreach $res (@outcome) {
			CondorTest::debug("looking for $res\n",1);
			$value = shift @results;
			if( $value ne $res ) {
				CondorTest::debug("Failing: found $value expecting $res\n",1);
				exit(1)
			}
		}
	}
};

CondorTest::RegisterExitedSuccess( $testname, $success);
CondorTest::RegisterExecute($testname, $executed);
CondorTest::RegisterExitedAbnormal( $testname, $abnormal );
CondorTest::RegisterAbort( $testname, $aborted );
CondorTest::RegisterHold( $testname, $held );
CondorTest::RegisterSubmit( $testname, $submitted );

if( CondorTest::RunTest($testname, $cmd, 0) ) {
	CondorTest::debug("$testname: SUCCESS\n",1);
	exit(0);
} else {
	# shutdown the server we started
    system("./x_general_client.pl $newsocketname quit");
	die "$testname: CondorTest::RunTest() failed\n";
}

