#!/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 set up for Condor testsuite run
######################################################################

use Cwd;

my $BaseDir = getcwd();

if( ($ENV{NMI_PLATFORM} =~ /winnt/) ){
	my $newbase = `cygpath -u $BaseDir`;
	chomp($newbase);
	$BaseDir = $newbase;
} 

# autoflush our STDOUT
$| = 1;

######################################################################
# set up environment and create env file for the rest of the scripts
######################################################################

print "Setting up environtment file 'setup_test_env' for other scripts\n";
open( ENVSETUP, ">setup_test_env" ) || 
    die "cannot open setup_test_env for writing\n";

print ENVSETUP "BASE_DIR=$BaseDir\n";
print ENVSETUP "export BASE_DIR\n";

if( !($ENV{NMI_PLATFORM} =~ /winnt/) ) {
    print ENVSETUP "PATH=$BaseDir/nmi_tools/glue/test:$BaseDir/condor/bin:$BaseDir/condor/sbin:\$PATH\n";
    print ENVSETUP "CONDOR_CONFIG=$BaseDir/condor_tests/TestingPersonalCondor/condor_config\n";
} else {
    # we want UNIX-style paths for PATH (since we're using cygwin)
    print ENVSETUP "PATH=$BaseDir/nmi_tools/glue/test:$BaseDir/condor/bin:/bin:/usr/bin\n";

    # Condor will want Win32-style paths for CONDOR_CONFIG
    my $Win32BaseDir = getcwd();
    print ENVSETUP "CONDOR_CONFIG=$Win32BaseDir\\condor_tests\\TestingPersonalCondor\\condor_config\n";
    
    # also, throw in the WIN32 version of the base directory path for later use
    print ENVSETUP "WIN32_BASE_DIR=$Win32BaseDir\n";
    print ENVSETUP "export WIN32_BASE_DIR\n";
}

print ENVSETUP "export PATH\n";
print ENVSETUP "export CONDOR_CONFIG\n";
close(ENVSETUP);

if( ($ENV{NMI_PLATFORM} =~ /winnt/) ){
	$res = system("d2u  setup_test_env");
	if($res != 0) {
		die "Failed to convert environment variable file\n";
	}
}

close(ENVSETUP);

if( ($ENV{NMI_PLATFORM} =~ /winnt/) ){
	$res = system("d2u  setup_test_env");
	if($res != 0) {
		die "Failed to convert environment variable file\n";
	}
}

print "All steps completed successfully\n";
