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

######################################################################
# script to setup the Condor build
######################################################################
use Cwd;

use Getopt::Long;
use vars qw/ $opt_use_externals_cache $opt_clear_externals_cache /;
# We use -- and everything after it will be configure arguments only.
GetOptions(
            'clear_externals_cache' => \$opt_clear_externals_cache,
            'clear-externals-cache' => \$opt_clear_externals_cache,
            'clear_externals_cache_weekly' => \$opt_clear_externals_cache_weekly,
            'clear-externals-cache-weekly' => \$opt_clear_externals_cache_weekly,
);

my $BaseDir = getcwd();
my $SrcDir = "$BaseDir/src";
my $buildid_file = "BUILD-ID";
my $buildid;
my $externals_loc="/scratch/externals/cmake";
my $platform = "$ENV{NMI_PLATFORM}";
my $prefix = "-DCMAKE_INSTALL_PREFIX:PATH=$BaseDir/release_dir ";

# autoflush our STDOUT
$| = 1;

print "------------------------- ENV DUMP ------------------------\n";
if ($ENV{NMI_PLATFORM} =~ /winnt/) {

	# This is an incredible hack to get around
	$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;$BaseDir/msconfig;$ENV{PATH}";
	system("set");

	# this can save on * etc from the web.
	# my $externals_loc="C:/temp/scratch/externals/cmake"
} 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";
    system("env");
}
print "------------------------- ENV DUMP ------------------------\n";
print "Configure args: " . join(' ', @ARGV) . "\n";

# very useful when we land on windows if/when cygwin is in PATH
system("cmake --version");

######################################################################
# grab out the build id so we can tell cmake what to use for buildid
######################################################################
print "Finding build id of Condor\n";
open( BUILDID, "$buildid_file" ) || die "Can't open $buildid_file: $!\n";
while( <BUILDID> ) {
    chomp;
    $buildid = $_;
}
close( BUILDID );
if( ! $buildid ) {
    die "Can't find Condor build id in $buildid_file!\n";
}
print "Build id is: $buildid\n";
$buildid = "-DBUILDID:STRING=$buildid ";

print "platform is: $platform\n";
$platform = "-DPLATFORM:STRING=$platform ";

######################################################################
# run cmake on the source tree
######################################################################
if ( defined($opt_clear_externals_cache)||defined($opt_clear_externals_cache_weekly) ) {

	if ($ENV{NMI_PLATFORM} =~ /winnt/) {
		print "cached externals is temporarily disabled on windows";
	}
	else {
		print "deleting externals cache -- disabled\n";
		#$exit_status = system("rm -rf $externals_loc/*");

		#if( $exit_status ) {
		#	die "failed with status $exit_status\n";
		#}

		#system("mkdir -p $externals_loc");
		#system("chmod -f -R a+rwX $externals_loc");
	}

}

######################################################################
# run cmake on the source tree
######################################################################
my $configexecstr =
			"cmake . " .
			$buildid .
			$platform .
			$prefix .
			join(' ', @ARGV);

print "$configexecstr\n";

#Store config string to file for later use (building native packages)
open (CMAKE_CONFIG, ">cmake_args.txt") || die "Cannot open cmake_args.txt: $!";
print CMAKE_CONFIG $configexecstr;
close (CMAKE_CONFIG);

$exit_status = system("$configexecstr 2>&1");

if( $exit_status ) {
    die "failed with status $exit_status\n";
}

exit 0;
