#!/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);
unshift @INC, $dir;
require "TestGlue.pm";
TestGlue::print_debug_header();

my $extractdone = "RESULTEXTRACTIONDONE";

# autoflush our STDOUT
$| = 1;

my $BaseDir = getcwd();

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

my $results = "results.tar.gz";
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");

    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");

    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";
    }
    
    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;
