#! /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 Digest::SHA;
#use strict;
use warnings;

my $stats = 0;

my $cmd = 'job_core_copytospool_van.cmd';
my $testdesc =  'Does copytospool work - vanilla U';
my $testname = "job_core_copytospool_van";
my $debuglevel = 2;

my $aborted = sub {
	my %info = @_;
	my $done;
	CondorTest::debug("Abort event expected as we clear job\n",$debuglevel);
};

my $submitted = sub {
	my %info = @_;
	my $cluster = $info{"cluster"};
	my $program = $info{"executable"};
	my $spool = `condor_config_val SPOOL`;
	CondorUtils::fullchomp($spool);

	CondorTest::debug("Checking spool dir --$spool--\n",$debuglevel);
	CondorTest::debug("Executable is $program\n",$debuglevel);
	CondorTest::debug("Transfer job worked..... need to check spool file.\n",$debuglevel);

	CondorTest::debug("SHA of $program is needed first\n",$debuglevel);

	my $datasha256 = Digest::SHA->new(256);
	$datasha256->addfile($program);
	my $hexexesha256 = $datasha256->hexdigest;
	CondorTest::debug("executable sha256: $hexexesha256\n",$debuglevel);

	my $shared_cluster_dir = $cluster % 10000;

	my $spoolfile = $spool . "/${shared_cluster_dir}/cluster$cluster" . ".ickpt.subproc0";
	CondorTest::debug("Spool file should be $spoolfile\n",$debuglevel);
	#system("ls $spool");

	my $sdatasha256 = Digest::SHA->new(256);
	$sdatasha256->addfile($spoolfile);
	my $shexexesha256 = $sdatasha256->hexdigest;
	CondorTest::debug("spooled executable sha256: $shexexesha256\n",$debuglevel);

	CondorTest::debug("OK remove the job!\n",$debuglevel);

	print "Comparing sha256 of original file and copy in spool - ";
	if( $hexexesha256 ne $shexexesha256 )
	{
		print "bad\n";
		die "Spooled executable and executable have DIFFERENT SHA256 checksums!!!\n";
	}

	print "ok\n";
	my @adarray;
	my $status = 1;
	my $cmd = "condor_rm $cluster";
	$status = CondorTest::runCondorTool($cmd,\@adarray,2);
	if(!$status)
	{
		CondorTest::debug("Test failure due to Condor Tool Failure<$cmd>\n",$debuglevel);
		exit(1)
	}

};

CondorTest::RegisterAbort( $testname, $aborted );
CondorTest::RegisterSubmit($testname, $submitted);

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

