#!/usr/bin/env perl
##**************************************************************
##
## Copyright (C) 1990-2011, 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.
##
##**************************************************************


######################################################################
# Submit-side NMI build system infrastructure to setup platform
# specific input for test jobs.  All we have to do is trim down the
# architecture-dependent results.tar.gz file to remove all the
# tarballs we don't care about for testing, and just leave the single 
# input file we care about.  We'll write the name of that file into a
# well-known file for our remote-side scripts to easily find it.
######################################################################

######################################################################
# We do need to remove things in such a way that this task can restart
# so we will flag certain steps and not repeat activities if we have 
# done them prior. This way we can remove things like results.tar.gz
# once we have extracted what we care about etc...
######################################################################
use strict;
use warnings;
use Cwd;
use File::Basename;
use File::Spec;

my $dir = dirname($0);
print " What is <$dir>\n";
unshift @INC, $dir;
require "TestGlue.pm";
TestGlue::print_debug_header();

my $extractdone = "RESULTEXTRACTIONDONE";
my $HaveWorkspace = 0;

# autoflush our STDOUT
$| = 1;

my $BaseDir = getcwd();
print "BaseDir = <$BaseDir>\n";

my $tarball_file = "CONDOR-TARBALL-NAME";

my $results = "results.tar.gz";

#maybe we are testing out of a workspace?
if( -f $results) {
	print "Cool, we have a tarfile\n";
}

# if we have the workspace, we want to run those non-compiled fresher
# tests and also grab the latest perl modules from src/condor_scripts
# and go with the freshest condor_examples files. Check to see if we
# brought this stuff along.

print "Checking to see if this is a workspace test source\n";
my $testpath = "src";
if(-d $testpath) {
	print "We have a $testpath folder\n";
}
$testpath = $testpath . "/condor_tests";
if(-d $testpath) {
	print "We have a $testpath folder\n";
	$HaveWorkspace = 1;
}
system("ls");
system("ls public");
die "$results does not exist!\n" unless(-f $results);

######################################################################
# -1) If we are dealing with a Windows build simply save the
#     results.tar.gz name in the marker file because it holds only
#     what we want. Then leave.
######################################################################
if( TestGlue::is_windows() ) {
    # Note that even though this is Windows this code executes on a Linux submit host
    # so it is ok to use Unix commands (tar, mv, chmod, etc)
    print "Untarring results.tar.gz\n";
    system("tar -xzvf $results");
    print "\nMoving public/* to $BaseDir\n";
    system("mv public/* $BaseDir");
    system("chmod a+r *.tar.gz");

	# are we running tests from our workspace and NOT the ones from this build?
	# NOTE: ANY COMPILED TESTS WILL RUN FROM WHERE THEY WERE BUILT DURING THIS
	# BUILD. So what we want, we overwrite what is already staged including
	# getting the fresh perlmodules from condor_scripts
	#
	if($HaveWorkspace == 1) {
		print "Getting tests from workspace\n";
		# doing recursive to make sure we get the Check folder of tools
		system("cp -r src/condor_tests/* $BaseDir/condor_tests");
		print "Getting config file examples from workspace\n";
		system("cp src/condor_examples/* $BaseDir/condor_examples");
		print "Getting fresh perl modules from workspace\n";
		system("cp src/condor_scripts/*.pm $BaseDir/condor_tests");
		print "Getting a fresh batch_test.pl from workspace\n";
		system("cp src/condor_scripts/batch_test.pl $BaseDir/condor_tests");
		print "Getting a fresh safe_append.pl from workspace\n";
		system("cp src/condor_scripts/safe_append.pl $BaseDir/condor_tests");
	}
    print "All steps completed successfully\n";
    print "Contents of '$BaseDir':\n";
    system("ls -l .");
    
    # NOTE: windows quits here!!
    exit 0;
}

print "Preparing input test job in $BaseDir\n";

######################################################################
# 1) find the specific binary we care about
#####################################################################
if(!-f $extractdone) {
    my $pattern = "public/condor-*-stripped.tar.gz";
    print "Extracting '$pattern', condor_examples, and condor_tests from results.tar.gz\n";

    my $ret = system("tar -xvzf $results '$pattern' public/condor_tests public/condor_examples");
	print "After tar extraction, it looks like this\n";
	system("ls");

    if($ret) {
    	die "Could not extract tarball or test dir from $results: $!\n";
    }
    
    print "Time: " . scalar(localtime) . "\n\n";
    my $saved_tarball = glob($pattern);
    print "Tarball extracted: '$saved_tarball'\n";
    my $tar_name = basename($saved_tarball);
    print "Tarball name is: $tar_name\n";
    
    ######################################################################
    # 2) Now that we found the binary we want, untar it from the tarball,
    #    move it to this parent directory.
    ######################################################################
    
    print "\n";
    print "Moving $tar_name to $BaseDir\n";
    if(not rename($saved_tarball, "$BaseDir/$tar_name")) {
    	die "Could not move $saved_tarball to $BaseDir: $!";
    }
       
    ######################################################################
    # 2) Now that we found the binary we want, untar the pre-built test
    #	 programs from the tarball and move them to this parent directory.
    ######################################################################
       
    # Now, copy it to the parent
    my $test_dir = "public/condor_tests";
    print "Moving $test_dir to $BaseDir\n";
    if(not rename($test_dir, "$BaseDir/condor_tests")) {
    	die "Could not move $test_dir into $BaseDir: $!\n";
    }

    my $example_dir = "public/condor_examples";
    print "Moving $example_dir to $BaseDir\n";
    if(not rename($example_dir, "$BaseDir/condor_examples")) {
    	die "Could not move $example_dir into $BaseDir: $!\n";
    }
    
	# are we running tests from our workspace and NOT the ones from this build?
	# NOTE: ANY COMPILED TESTS WILL RUN FROM WHERE THEY WERE BUILT DURING THIS
	# BUILD. So what we want, we overwrite what is already staged including
	# getting the fresh perlmodules from condor_scripts
	#
	if($HaveWorkspace == 1) {
		print "Getting tests from workspace\n";
		# doing recursive to make sure we get the Check folder of tools
		system("cp -r src/condor_tests/* $BaseDir/condor_tests");
		print "Getting config file examples from workspace\n";
		system("cp src/condor_examples/* $BaseDir/condor_examples");
		print "Getting fresh perl modules from workspace\n";
		system("cp src/condor_scripts/*.pm $BaseDir/condor_tests");
		print "Getting a fresh batch_test.pl from workspace\n";
		system("cp src/condor_scripts/batch_test.pl $BaseDir/condor_tests");
		print "Getting a fresh safe_append.pl from workspace\n";
		system("cp src/condor_scripts/safe_append.pl $BaseDir/condor_tests");
	}
    print "Writing tarball filename to $tarball_file.\n";
    open(TARBALL_FILE, '>', $tarball_file ) || die "Can't open $tarball_file for writing: $!\n";
    print TARBALL_FILE "$tar_name\n";
    close(TARBALL_FILE);
   }

# Mark all processing done and results.tar.gz processing etc is done
open(SENTINEL, '>', $extractdone) or die("Can't touch $extractdone: $!");
close(SENTINEL);

# Finally, blow away anything still in public and the results.tar.gz
print "Removing all other arch-dependent data\n";
system( "rm -rf public $results results" );
if( $? ) {
    die "'rm -rf public $results results' failed with status $? ($!)\n";
}

print "All steps completed successfully\n";
print "End time: " . scalar(localtime) . "\n";
exit 0;
