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


##################################################
# Run in a specified directory?
##################################################
if [ "$1" != "" ] ; then
    if [ -d $1 ] ; then
	echo "Running in $1"
	cd $1
	shift
    else
	echo "$1 is not a directory"
    fi
fi

##################################################
# Determine operating system flavors
##################################################

os=`uname -s | sed 's/IRIX.*/IRIX/'`
if [ $os = "Darwin" ] ; then
	Imakecppflags="gcc -E -w  -no-cpp-precomp \$*"
else
	Imakecppflags="gcc -E -w \$*"
fi

##################################################
# Find imake.  
# If it doesn't yet exist, build it.
##################################################

IMAKE=imake
if [ X$USE_OLD_IMAKE = X ] ; then

if [ -d ../imake ] ; then
    IMAKEROOT="../imake"
elif [ -d ../../imake ] ; then
    IMAKEROOT="../../imake"
elif [ -d ../../../imake ] ; then
    IMAKEROOT="../../../imake"
else
	echo "Can't find the root directory for imake!"
	echo "You need a copy of imake build Condor."
	echo "Please make sure your version of the Condor source"
	echo "contains a directory called \"imake\" that is at the"
	echo "top-level of your source tree (next to \"src\", \"config\")"
	exit 1
fi

IMAKE="$IMAKEROOT/imake"
if [ ! -x $IMAKE ] ; then
    echo "Attemping to build imake from source"
    pwd=`pwd`
    cd $IMAKEROOT
    make
    cd $pwd
fi
if [ ! -x $IMAKE ] ; then
    # if it's not here yet, fatal error
    echo "Failed to build imake!"
	exit 1
fi

# use our supplied script to figure out which cpp to call on what architecture
IMAKECPP="$IMAKEROOT/ansi_cpp"
export IMAKECPP

# end of USE_OLD_IMAKE
fi

##################################################
# Finally, call imake with all the flags
##################################################
${IMAKE} -s Makefile -I. -I.. -I../.. -I../config -I../../config $*

