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

$cmd = 'job_core_queue_sched.cmd';
$testdesc =  'condor_submit queue - scheduler U';
$testname = "job_core_queue_sched";
$queuelog = "submit_queue_scheduler_log.log";


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

my $socketname = "/tmp/queuetest";
my $newsocketname = $dir->dirname . "/queuetest";
CondorTest::debug("current directory is $newsocketname\n",1);
@filestoedit = qw/job_core_queue.pl/;
@filestochmod = qw/job_core_queue.pl/;

# now make sure each place using socket refers to the temporary directory!
foreach $name (@filestoedit)
{
	CondorTest::debug("Correcting socket name in $name\n",1);
	open(FF,"<$name") || die "Failed to open $name: $!\n";
	open(NEWFF,">$name.new") || die "Failed to open $name.new: $!\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);
	runcmd("mv $name.new $name");
}

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

my $queuelength = 10;
my $submitcount = 0;
my $donecount = 0;

open(QUEUECMD,"<$cmd") || die "Can't open cmd file $!\n";
open(NEWCMD,">$cmd.new") || die "Can not open new command file: $!\n";
while(<QUEUECMD>)
{
	CondorUtils::fullchomp($_);
	$line = $_;
	if( $line =~ /^\s*queue\s*.*$/ )
	{
		print NEWCMD "queue $queuelength\n";
	}
	else
	{
		print NEWCMD "$line\n";
	}
}
close(QUEUECMD);
close(NEWCMD);

runcmd("mv $cmd.new $cmd");

$ExitSuccessBase = sub {
	my %info = @_;

};

my @lines;


$success = sub
{
    my %args = @_;
	my $cluster = $args{"cluster"};
	$donecount += 1;
	if( $donecount == $submitcount )
	{
		# kill server
		#print "x_general_client.pl $newsocketname quit\n";
		runcmd("./x_general_client.pl $newsocketname quit");
		# test for correct number of hellos from jobs in jog
		open(FLOW,"./x_readqueuelog.pl $flowlog HELLO $queuelength | ") || die "Can not open flowlog: $!\n";
		while(<FLOW>)
		{
			my $ret;
			CondorUtils::fullchomp($_);
			$ret = $_;
			CondorTest::debug("$ret\n",1);
			if( $ret ne "0" )
			{
				die "failed to run exact number of pre/post scripts\n";
			}
		}
	}
};

$submit = sub
{
    my %args = @_;
	my $cluster = $args{"cluster"};
	$submitcount += 1;
};

# can not do this because we stick here as long as server runs
#runcmd("x_general_server.pl $queuelog");
runcmd("rm -f $queuelog");
defined(my $pid = fork) or die "Cannot fork: $!";
unless ($pid)
{
	#child runs and ceases to exist
	exec "./x_general_server.pl $newsocketname $queuelog";
	die "can not exec dumpcore!\n";
}
#main process continues here
sleep 2;

CondorTest::RegisterExitedSuccess( $testname, $success );
CondorTest::RegisterSubmit( $testname, $submit );


if( CondorTest::RunTest($testname, $cmd, 0) ) {
	CondorTest::debug("$testname: SUCCESS\n",1);
	exit(0);
} else {
	die "$testname: CondorTest::RunTest() failed\n";
}

