#!/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 POSIX 'strftime';
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 %defines = (
    prefix => "-DCMAKE_INSTALL_PREFIX:PATH=$BaseDir/release_dir",
    );

# autoflush our STDOUT
$| = 1;

if ($ENV{NMI_PLATFORM} =~ /winnt/) {
    $ENV{PATH} = "C:\\Program Files\\CMake 2.8\\bin;$ENV{VS90COMNTOOLS}..\\IDE;$ENV{VS90COMNTOOLS}..\\..\\VC\\BIN;$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";
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";
my @stat = stat(BUILDID);
$date = strftime( "%b %d %Y", localtime($stat[9]) );
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";
$defines{buildid} = "-DBUILDID:STRING=$buildid";
$defines{date} = "-DBUILD_DATE:STRING=\"$date\"";

print "platform is: $platform\n";
$defines{platform} = "-DPLATFORM:STRING=$platform";
$defines{condor_platform} = "-DCONDOR_PLATFORM: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 @command = ( "cmake",
		".",
		join(" ", values(%defines)),
		join(' ', @ARGV)
    );
my $configexecstr = join( " ", @command );

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;
