#!/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.
##
##**************************************************************


######################################################################
# $Id: remote_post,v 1.4 2007-11-08 22:53:44 nleroy Exp $
# post script to cleanup after a Condor build, successful or not
######################################################################

# autoflush our STDOUT
$| = 1;

use strict;
use warnings;
use Cwd;
use File::Basename;

my $BaseDir = getcwd();
my $SrcDir = "$BaseDir/src";
my $pub_dir = "$BaseDir/public";
my $pub_logs = "$pub_dir/logs";
my $pub_testbin = "$pub_dir/testbin";
my $failed = $ENV{_NMI_STEP_FAILED};

# This is debugging output for the sake of the NWO infrastructure.
# However, it might be useful to us some day so we can see what's
# going on in case of failures...
if( defined $ENV{_NMI_STEP_FAILED} ) { 
    my $nmi_task_failure = "$ENV{_NMI_STEP_FAILED}";
    print "The value of _NMI_STEP_FAILED is: '$nmi_task_failure'\n";
} else {
    print "The _NMI_STEP_FAILED variable is not set\n";
}

if ($ENV{NMI_PLATFORM} =~ /winnt/) {
	$ENV{PATH} ="C:\\Program Files\\Microsoft Visual Studio 9.0\\Common7\\IDE;C:\\prereq\\ActivePerl-5.10.1\\site\\bin;C:\\prereq\\ActivePerl-5.10.1\\bin;C:\\Perl\\site\\bin;C:\\Perl\\bin;C:\\WINDOWS\\system32;C:\\WINDOWS;C:\\Program Files\\CMake 2.8\\bin;C:\\Program Files\\7-Zip;$ENV{PATH}";
} else {
	$ENV{PATH} ="$ENV{PATH}:/sw/bin:/sw/sbin:/usr/kerberos/bin:/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin:/usr/bin/X11:/usr/X11R6/bin:/usr/local/condor/bin:/usr/local/condor/sbin:/usr/local/bin:/bin:/usr/bin:/usr/X11R6/bin:/usr/ccs/bin:/usr/lib/java/bin";
}
print "------------------------- ENV DUMP ------------------------\n";
foreach my $key ( sort {uc($a) cmp uc($b)} (keys %ENV) ) {
    print "$key=".$ENV{$key}."\n";
}
print "------------------------- ENV DUMP ------------------------\n";


######################################################################
# save debugging results
######################################################################

# create a directory to save the data
if( ! -d $pub_dir ) {
    mkdir( "$pub_dir", 0777 ) || die("Can't mkdir($pub_dir): $!\n");
}

# copy in the package targets
if( $ENV{NMI_PLATFORM} =~ /winnt/ ) {
	# system("mv *.msi *.zip *.tar.gz $pub_dir");
	system("nmi_tools\\glue\\build\\munge.bat $pub_dir cleanup");
	system("mv *.zip $pub_dir");
	system("mv *.msi $pub_dir");
	system("mv condor-*.tar.gz $pub_dir");
}
else {
    system("mv *.tar.gz *.rpm *.deb $pub_dir");
    system("cmake -E md5sum $pub_dir/* md5s.txt");
}


# copy all the bits in which are required to run batch_test (omg what a cf) 
system("cp -r $SrcDir/condor_tests $pub_dir");
system("cp -r $SrcDir/condor_examples $pub_dir");

######################################################################
# tar up build results
######################################################################

print "Tarring up results\n";
open( TAR, "tar zcvf results.tar.gz public|" ) || 
    die "Can't open tar as a pipe: $!\n";
while( <TAR> ) { 
    print;
}
close( TAR );
my $tarstatus = $? >> 8;
if( $tarstatus ) {
    die "Can't tar zcf results.tar.gz public: status $tarstatus\n";
}
print "Done tarring results\n";
exit 0;
