#! /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 Check::SimpleJob;
use CheckOutputFormats;
use strict;
use warnings;

my $testname = "cmd_submit-factory";
my $curcwd = getcwd();
my $iswindows = CondorUtils::is_windows();

my $append_config;
$append_config = "
	# this is now the default
	#SCHEDD_ALLOW_LATE_MATERIALIZE = true
";

###########  create submit file ##################
my $pid = $$;
my $submit_content = 
"executable = x_sleep.pl
arguments = 600
queue";
my $submitfile = "$testname$pid.sub";
emit_dag_files($testname, $submit_content, $pid);

print "========== simple test: add config SCHEDD_ALLOW_LATE_MATERIALIZATION = true ==========\n";
my $configfile = CondorTest::CreateLocalConfig($append_config, "factory1");
CondorTest::StartCondorWithParams(
	condor_name=>"factory1",
	fresh_local => "TRUE",
	condorlocalsrc => "$configfile",
);

`condor_submit $submitfile -factory`;
my @content = `condor_q`;
my $res = count_jobs(\@content);
if ($res eq 1) {
	RegisterResult(1, check_name=>"simple test", test_name=>$testname);
} else {
	print "ERROR: there should be 4 jobs in the queue\n";
	print @content;
	RegisterResult(0, check_name=>"simple test", test_name=>$testname);
}

print "========== check if a digest file exist ==========\n";
my $clusterId = `condor_q -format %v ClusterId`;
my $digest_file;
if ($iswindows) {
	$digest_file = $curcwd.'\condor_submit.'.$clusterId.".digest"
} else {
	$digest_file = $curcwd."/condor_submit.".$clusterId.".digest";
}
my $if_exist = 1 if -e $digest_file;
if ($if_exist) {
	print "digest file $digest_file exists\n";
	RegisterResult(1, check_name => "if digest file exists", test_name => $testname);
} else {
	#tj: turning this check off for now. it should be fixed to check location specified in the cluster ad.
	#print "ERROR: did not find digest file\n";
	#RegisterResult(0, check_name => "if digest file exists", test_name => $testname);
}

print "========== max_materialize = 10 and submit 12 jobs ==========\n";

CondorTest::StartCondorWithParams(
	condor_name=>"factory2",
	fresh_local => "TRUE",
	condorlocalsrc => "$configfile",
);

$submit_content = 
"executable = x_sleep.pl
arguments = 600
max_materialize = 10
queue 12";
open(FH, ">$submitfile") || print "ERROR opening file $submitfile";
print FH $submit_content;
close(FH);
my @submit_message = `condor_submit $submitfile -factory`;
my $submit_total = count_jobs(\@submit_message);
if ($submit_total != 12) {
	print "ERROR: $submit_total jobs submitted, should submit 12 jobs";
	RegisterResult(0, check_name => "check total jobs submitted", test_name => $testname);
} else {
	RegisterResult(1, check_name => "check total jobs submitted", test_name => $testname);
}

@content = `condor_q`;
$res = count_jobs(\@content);

if ($res eq 10) {
	RegisterResult(1, check_name=>"max_materialize = 10", test_name=>$testname);
} else {
	print "ERROR: there should be 10 jobs in the queue:\n";
	print @content;
	RegisterResult(0, check_name=>"max_materialize = 10", test_name=>$testname);
}
`condor_rm -all`;

sub count_jobs {
	my @content = @{$_[0]};
	my $res = -1;
	for my $i (0..(scalar @content)-1) {
		# extract count of jobs N from the line that says "submitted N jobs"
		if ($content[$i] =~ /([0-9]+)\s*job\(?s\)?/) {
			$res = $1;
			last;
		}
	}
	return $res;
}

CondorTest::EndTest();
exit(0);
