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

my $iswindows = CondorUtils::is_windows();

$cmd = 'job_dagman_prepost.dag';

$serversubFile = "job_dagman_prepost-srvr.cmd";

my $curcwd = getcwd();
my $dir = File::Temp->newdir(CLEANUP => 0, DIR => $curcwd);
CondorTest::debug("current directory is $curcwd\n",1);
CondorTest::LoadExemption("job_dagman_prepost,no,DC_AUTHENTICATE unable to receive auth_info!");

my $socketname = "prepostsock";
my $newsocketname = $dir->dirname . "/prepostsock";
my $flowlog = $dir->dirname . "/job_dagman_prepost-srvr-log.log";
if($iswindows) {
	$_ = $flowlog;
	s/\//\\\\/g;
	$flowlog = $_;
}
my $appendfile = "filetoappendto";
CondorTest::debug("current directory is $newsocketname\n",1);

# start a server for this test

runcmd("rm -f $flowlog");
if(!($iswindows)) {
	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

@filestoedit = qw/job_dagman_prepost-prehello job_dagman_prepost-posthello/;
@filestochmod = qw/job_dagman_prepost-prehello.pl job_dagman_prepost-posthello.pl/;

# now make sure each place using socket refers to temp 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(!($iswindows)) {
			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";
			}
		} else {
			if( $line =~ /(^\s*.*)$appendfile(.*$)/)
			{
				CondorTest::debug("Found ---$1$appendfile$2---\n",1);
				CondorTest::debug("replacing with $1$flowlog$2\n",1);
				print NEWFF "$1$flowlog$2\n";
			}
			else
			{
				print NEWFF "$line\n";
			}
		}
	}
	close(FF);
	close(NEWFF);
}

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

$testdesc =  'Condor submit dag - verify pre/post ran test - scheduler U';
$testname = "job_dagman_prepost";
$dagman_args = "-verbose";
$output = "job_dagman_prepost.dag.dagman.out";

my $killedchosen = 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
{
	CondorTest::debug("Good. We need the dag to run\n",1);
};

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

$success = sub
{
	my $scriptcounter = 0;
	my $postcounter = 0;
	my $precounter = 0;
	my $line = "";
	CondorTest::debug("executed successfully\n",1);
	open(FLOW,"./x_dagman_readprepostlog.pl $flowlog PREPOST 20 20 | ") || die "Can not open flowlog: $!\n";
	while(<FLOW>)
	{
		my $ret;
		CondorUtils::fullchomp($_);
		$ret = $_;
		CondorTest::debug("$ret\n",1);
		if( $ret ne "0" )
		{
			# shutdown the server we started
			if(!($iswindows)) {
            	runcmd("./x_general_client.pl $newsocketname quit");
			}
			die "failed to run exact number of pre/post scripts\n";
		}
	}
	# shutdown the server we started
	if(!($iswindows)) {
    	runcmd("./x_general_client.pl $newsocketname quit");
	}
};

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::RunDagTest($testname, $cmd, 0, $dagman_args) ) {
	CondorTest::debug("$testname: SUCCESS\n",1);
	exit(0);
} else {
	# shutdown the server we started
	if(!($iswindows)) {
    	runcmd("./x_general_client.pl $newsocketname quit");
	}
	die "$testname: CondorTest::RunTest() failed\n";
}

