#! /bin/sh
#
# Globus2 device setup.
#
makefile_name=Makefile.mpich-1.2.1

print_error()
{
    echo "*# $*" 2>&1 ;
}

#
# mpidi_mkdir_p()
#
# Simulate "mkdir -p" since it is not available everywhere.  Portions
# of this code were taken from mpich2/confdb/aclocal.m4.
#
mpidi_mkdir_p()
{(
    saveIFS="${IFS}"
    IFS="/"
    tmp_curdir=""
    for tmp_subdir in $1 ; do
        tmp_curdir="${tmp_curdir}${tmp_subdir}"
        if test -n "${tmp_curdir}" -a ! -d "${tmp_curdir}" ; then
	    mkdir "${tmp_curdir}"
	fi
        tmp_curdir="${tmp_curdir}/"
    done
    IFS="${saveIFS}"
)}

#
# Rename the MPICH libraries for G2 over vMPI-MPICH
#
MPILIBNAME="mpichg2"

#
# Verify that the user has set GLOBUS_LOCATION or GLOBUS_INSTALL_PATH
#
if test -n "${GLOBUS_LOCATION=}" ; then
    globus_packaged=1
    echo "using newly packaged Globus"
elif test -n "${GLOBUS_INSTALL_PATH=}" ; then
    globus_packaged=0
    echo "using old monolithic Globus"
else
    print_error "GLOBUS_INSTALL_PATH must be set"
    exit 1
fi

#
# Unpack device_args
#
GLOBUSDIR=""
DEBUG_ENABLED=0
DEBUG_MODULES_ENABLED="0"
DEBUG_INFO_ENABLED="DEBUG_INFO_ALL"
flavor_opts=
F77_SED=0
for arg in $device_args $@ ; do
    case $arg in 
     -dir=* | --dir=* | -globusdir=* | --globusdir=*)
        if test $globus_packaged -eq 0 ; then
	    GLOBUSDIR="`echo $arg|sed 's/.*=//'`"
	else
	    print_error "-dir only valid with Globus version 1.x"
	fi
        ;;
     -flavor=* | --flavor=*)
        if test $globus_packaged -eq 0 ; then
	    flavor_opts="`echo $arg|sed -e 's/.*=/-/' -e 's/,/ -/g'`"
	else
	    flavor="`echo $arg|sed -e 's/.*=//'`"
	fi
	;;
     -f77sed | --f77sed)
        F77_SED=1
	;;
     -debug=*)
        DEBUG_ENABLED=1
	debug_flags="`echo $arg|sed -e 's/.*=//' -e 's/,/ /g'`"
	dme="0"
	for flag in "$debug_flags" ; do
	    case $flag in
	      all)
		dme="$dme|DEBUG_MODULE_ALL"
		;;
	      func)
		dme="$dme|DEBUG_MODULE_FUNC"
		;;
	      mp)
		dme="$dme|DEBUG_MODULE_MP"
		;;
	      tcp)
		dme="$dme|DEBUG_MODULE_TCP"
		;;
	      send)
		dme="$dme|DEBUG_MODULE_SEND"
		;;
	      recv)
		dme="$dme|DEBUG_MODULE_RECV"
		;;
	      comm)
		dme="$dme|DEBUG_MODULE_COMM"
		;;
	      types)
		dme="$dme|DEBUG_MODULE_TYPES"
		;;
	      *)
	        echo "unrecognized debugging flag ($flag); ignoring..."
		;;
	    esac
	done
	DEBUG_MODULES_ENABLED="$dme"
        ;;
     -u | -usage | --usage | --usag | --usa | --us | --u | -help | --help )
        if test $globus_packaged -eq 0 ; then
	    cat >&2 <<EOF
--with-device=$DEVICE[:-dir=<developmentdir>|-flavor=<flavorlist>]

  -dir=developmentdir
    This option is used to identify the specific directory containing the
    desired development flavor (e.g.: 
    $GLOBUS_INSTALL_PATH/development/mips-sgi-irix6.5-n32_nothreads_mpi_debug)

  -flavor=flavorlist
    This option is used to identify which features the desired development
    flavor should contain.  A list of features can be obtained by executing
    'globus-development-path -help'.  The flavorlist is a comma separated
    list of features (excluding leading dashes and whitespace).  (e.g.:
    nothreads,64,mpi)

You can acquire Globus from http://www.globus.org.

EOF
	else
	    cat >&2 <<EOF
--with-device=$DEVICE:-flavor=<flavor-name>

  -flavor=flavor-name
    This is used to identify which flavor of Globus MPICH-G2 is built against.
    The following command can be used to list the flavors installed:
    ls $GLOBUS_LOCATION/etc/globus_core/flavor_*.gpt | \
    sed -e 's/^flavor_//' - e 's/\.gpt$//'

You can acquire Globus from http://www.globus.org.

EOF
	fi
	exit 0;
	;;
     *) if test -n "$arg" ; then
	    echo "unrecognized argument $arg"
	    exit 1
	fi
	;;
    esac
done

mpidi_mkdir_p mpid/${DEVICE}
if test $globus_packaged -eq 0 ; then
    if test -z "$GLOBUSDIR" ; then
    	if test -x $GLOBUS_INSTALL_PATH/bin/globus-development-path ; then
    	    if test -n "$arch_IRIX64" ; then
    		flavor_opts="-64 $flavor_opts"
    	    elif test -n "$arch_IRIXN32" ; then
    		flavor_opts="-32 $flavor_opts"
    	    fi
    	    GLOBUSDIR="`$GLOBUS_INSTALL_PATH/bin/globus-development-path \
    		$flavor_opts`"
    	    echo "NOTE: using Globus development libraries in $GLOBUSDIR"
    	fi
    fi
    
    if test -z "$GLOBUSDIR" ; then
    	print_error "You must identify which flavor of Globus is to be used."
    	print_error "You can accomplish this by specifying either the"
    	print_error " -dir=<devdir> or -flavor=<flavorlist> suboptions.  For "
    	print_error "example: -device=globus2:-flavor=nothreads,standard,64"
    	exit 1
    fi
    
    if test ! -d $GLOBUSDIR ; then
        print_error "Could not find Globus in $GLOBUSDIR"
        exit 1
    fi

    globus_makefile_path=$GLOBUSDIR/etc/$makefile_name
else
    #
    # Starting with Globus v2.2 they have moved the location
    # of globus-makefile-header to $GLOBUS_LOCATION/bin.
    # Prior to v2.2 globus-makefile-header was in $GLOBUS_LOCATION/sbin.
    # We now need to check both locations ... so much for 
    # backward compatibility ;-).
    #
    if test -f $GLOBUS_LOCATION/sbin/globus-makefile-header ; then
	gmh=$GLOBUS_LOCATION/sbin/globus-makefile-header
    else
	if test -f $GLOBUS_LOCATION/bin/globus-makefile-header ; then
	    gmh=$GLOBUS_LOCATION/bin/globus-makefile-header
	else
	    print_error "Unable to locate either $GLOBUS_LOCATION/{sbin,bin}/globus-makefile-header" 
	    exit 1
	fi
    fi

    makefile_name=Makefile.mpich
    if test ! -f mpid/${DEVICE}/${makefile_name}  ; then
	cp ${top_srcdir}/mpid/${DEVICE}/${makefile_name} mpid/${DEVICE}/
    fi
    $gmh -flavor=$flavor \
	globus_common globus_gram_client globus_io globus_data_conversion \
	globus_duroc_runtime globus_duroc_bootstrap globus_ftp_control \
	globus_core \
	>mpid/${DEVICE}/Makefile.mpich.header
    if test $? != 0 ; then
	print_error "ERROR: failed globus-makefile-header: missing Globus components"
	cat mpid/${DEVICE}/Makefile.mpich.header
	exit 1
    fi
    globus_makefile_path=mpid/${DEVICE}/$makefile_name
fi

if test ! -f $globus_makefile_path ; then
    print_error "Could not find $globus_makefile_path"
    exit 1
fi

#
# Tell top-level configure not to check for rsh/ssh
#
DEVICE_KIND=wan
#
# Tell top-level configure not to define MPIRUN_DEVICE
#
define_mpirun_device="no"
#
# Function to extract compiler settings, etc. from the globus supplied
# configuration make file.
#
get_globus_setting()
{
    eval $1=\""`$MAKE -f $globus_makefile_path $2 | \
	sed -e 's/^ *//' -e 's/ *$//'`"\"
    if test $? != 0 ; then
	print_error "Your version of globus does not support $2 target"
	exit 1
    fi
}

#
# Determine whether or not we are using the vendor's MPI as one of our
# transport protocols. If we are, then we need to rename all of the
# MPICH MPI_ and PMPI_ prefixes to MPQ_ and PMPQ_ respectively.  This
# can be accomplished easily in C/C++ using a header file with the
# appropriate #defines.  However, Fortran 77/90 requires that we
# preprocess the code with a sed script that does the necessary
# replacement.  To make this preprocessing step as transparent as
# possible, we use compiler scripts (created below) which do all of
# the dirty work.
#
get_globus_setting vmpi globus_mpi_enabled
if test "$vmpi" = "yes" ; then
    VMPI=1
else
    VMPI=0
fi

#
# We need to supply some header files to do the VMPI name transformations
#
mpidi_mkdir_p ${includebuild_dir}
if test $VMPI -ne 0 ; then
    HAVE_MPID_DEFS_H=1
    cp ${top_srcdir}/mpid/$DEVICE/mpid_defs.h ${includebuild_dir}
    cp ${top_srcdir}/mpid/$DEVICE/global_c_symb.h ${includebuild_dir}
    cp ${top_srcdir}/mpid/$DEVICE/global_fort_symb.h ${includebuild_dir}
else
    F77_SED=0
fi

if test $F77_SED -ne 0 ; then
    # doing sed replacement causes some problems with F90 modules.
    # we could probably fix this, but not before the first release. -- (Brian)
    NO_F90=1
fi
mpidi_mkdir_p ${builddir}/src/fortran/include
HAVE_MPID_FORTDEFS_H=1
export HAVE_MPID_FORTDEFS_H
sed -e "s!@F77_SED@!${F77_SED}!" ${top_srcdir}/mpid/$DEVICE/mpid_fortdefs.h.in \
    >${builddir}/src/fortran/include/mpid_fortdefs.h
sed -e "s!@F77_SED@!${F77_SED}!" ${top_srcdir}/mpid/$DEVICE/mpid_fortdefs.h.in \
    >${includebuild_dir}/mpid_fortdefs.h

#
# Get the libraries, etc (there's also a globuslibpath and globuslib)
# For some reason, Globus thinks that only MPICH is interested in this
# information.  I'm surprised that no other Globus users need this info.
#
get_globus_setting GLOBUS_LIB_PATH userlibpath
get_globus_setting GLOBUS_LIBS userlib
GLOBUS_LIBS="$GLOBUS_LIB_PATH $GLOBUS_LIBS"

#
# Globus requires different compilers (BRT - MPICH configure does some testing
# before this is set, which implies that this file should either be included
# earlier, or that the user is going to have specify the right compilers or
# architecture so the tests use the same compilers and flags as the build)
#

#
# Gather C compiler and linker information
#
get_globus_setting GLOBUS_CC cc
GLOBUS_TEST_CC=$GLOBUS_CC
get_globus_setting GLOBUS_CFLAGS cflags
get_globus_setting GLOBUS_TEST_CFLAGS base_cflags
get_globus_setting GLOBUS_CLINKER clinker
GLOBUS_TEST_CLINKER=$GLOBUS_CLINKER
get_globus_setting GLOBUS_CLDFLAGS clinkerflags
get_globus_setting GLOBUS_TEST_CLDFLAGS base_clinkerflags

CC=${binbuild_dir}/mpich-globus2-cc
TESTCC=$CC
if test "$USERCC" = 1 ; then
    print_error "Globus device overrode C compiler setting"
fi
USERCC=1

CLINKER=${binbuild_dir}/mpich-globus2-cc
if test "$USERCLINKER" = 1 ; then
    print_error "Globus device overrode C linker setting"
fi
USERCLINKER=1

#
# Gather C++ compiler and linker information
#
if test "$NOCXX" != 1 ; then
    get_globus_setting GLOBUS_CXX CC
    GLOBUS_TEST_CXX=$GLOBUS_CXX
    get_globus_setting GLOBUS_CXXFLAGS CCflags
    get_globus_setting GLOBUS_TEST_CXXFLAGS base_CCflags
    get_globus_setting GLOBUS_CXXLINKER CClinker
    GLOBUS_TEST_CXXLINKER=$GLOBUS_CXXLINKER
    get_globus_setting GLOBUS_CXXLDFLAGS CClinkerflags
    get_globus_setting GLOBUS_TEST_CXXLDFLAGS base_CClinkerflags

    CPP_COMPILER=${binbuild_dir}/mpich-globus2-cxx
    if test "$USERCPP" = 1 ; then
    	print_error "Globus device overrode C++ compiler setting"
    fi
    USERCPP=1
    
    CCLINKER=${binbuild_dir}/mpich-globus2-cxx
    if test "$USERCCLINKER" = 1 ; then
    	print_error "Globus device overrode C++ linker setting"
    fi
    USERCCLINKER=1
fi

#
# Gather F77 compiler and linker information
#
if test $NOF77 -eq 0 ; then
    get_globus_setting GLOBUS_F77 f77
    GLOBUS_TEST_F77=$GLOBUS_F77
    get_globus_setting GLOBUS_F77FLAGS f77flags
    get_globus_setting GLOBUS_TEST_F77FLAGS base_f77flags
    get_globus_setting GLOBUS_F77LINKER f77linker
    GLOBUS_TEST_F77LINKER=$GLOBUS_F77LINKER
    get_globus_setting GLOBUS_F77LDFLAGS f77linkerflags
    get_globus_setting GLOBUS_TEST_F77LDFLAGS base_f77linkerflags

    FC=${binbuild_dir}/mpich-globus2-f77
    F77=$FC
    TESTF77=$FC
    if test "$USERF77" = 1 ; then
    	print_error "Globus device overrode F77 compiler setting"
    fi
    USERF77=1
    
    FLINKER=${binbuild_dir}/mpich-globus2-f77
    if test "$USERFLINKER" = 1 ; then
    	print_error "Globus device overrode F77 linker setting"
    fi
    USERFLINKER=1
fi

#
# Gather F90 compiler and linker information
#
if test $NO_F90 -eq 0 ; then
    get_globus_setting GLOBUS_F90 f90
    GLOBUS_TEST_F90=$GLOBUS_F90
    get_globus_setting GLOBUS_F90FLAGS f90flags
    get_globus_setting GLOBUS_TEST_F90FLAGS base_f90flags
    get_globus_setting GLOBUS_F90LINKER f90linker
    GLOBUS_TEST_F90LINKER=$GLOBUS_F90LINKER
    get_globus_setting GLOBUS_F90LDFLAGS f90linkerflags
    get_globus_setting GLOBUS_TEST_F90LDFLAGS base_f90linkerflags

    #
    # On Redhat Linux 6.x, f90 is not supplied, but Globus gets it wrong.  If
    # F90 does not point at a working compiler and if f77 is really g77, then
    # we enable the -ff90 option.  (-ff90 doesn't seem to work; disabling f90
    # support for now...)
    #
    if which $GLOBUS_F90 >/dev/null 2>&1 ; then
	true
    else
	# if $GLOBUS_F77 --version 2>/dev/null | \
	#   grep GNU >/dev/null 2>&1 ; then
	#     echo "NOTE: using GNU F77 compiler as the F90 compiler"
	#     GLOBUS_F90=$GLOBUS_F77
	#     GLOBUS_F90FLAGS="-ff90 $GLOBUS_F77FLAGS"
	#     GLOBUS_F90LINKER=$GLOBUS_F77LINKER
	#     GLOBUS_F90LDFLAGS="-ff90 $GLOBUS_F77LDFLAGS"
	#     GLOBUS_TEST_F90=$GLOBUS_TEST_F77
	#     GLOBUS_TEST_F90FLAGS="-ff90 $GLOBUS_TEST_F77FLAGS"
	#     GLOBUS_TEST_F90LINKER=$GLOBUS_TEST_F77LINKER
	#     GLOBUS_TEST_F90LDFLAGS="-ff90 $GLOBUS_TEST_F77LDFLAGS"
	# else
	#     print_error "F90 compiler not present; disabling F90 support"
        #     enable_args="$enable_args --disable-f90"
	#     NO_F90=1
	# fi

	print_error "F90 compiler not present; disabling F90 support"
        enable_args="$enable_args --disable-f90"
	NO_F90=1
    fi

    if test $NO_F90 -eq 0 ; then
    	F90=${binbuild_dir}/mpich-globus2-f90
	TESTF90=$F90
	if test "$USERF90" = 1 ; then
	    print_error "Globus device overrode F90 compiler setting"
	fi
	USERF90=1
	
	F90LINKER=${binbuild_dir}/mpich-globus2-f90
	if test "$USER_SET_F90LINKER" = 1 ; then
	    print_error "Globus device overrode F90 linker setting"
	fi
	USER_SET_F90LINKER=1
    fi
fi

#
# Are the globus libraries multi-threaded?
#
get_globus_setting globus_threads_enabled globus_threads_enabled

#
# Some MPI compilers, like IBM's mpcc, are effectively cross
# compilers.  For these compilers, we need to define things so that
# the compiler produces configuration test codes that can be executed
# locally.  Obviously, using this technique, we are making the
# assumption that the host platform and the target platform are the
# same.  If they are different, then the configure code is going to
# detect the wrong features and values.  The only way to solve this
# problem in general is to make configure understand how to run on the
# target machine or have the user supply all of the necessary values,
# both of which are painful (for us and the users).  For now, we
# assume that the code is being compiled on an identical platform.
# Someday, this script will need to be updated to allow the user to
# specify the parameters in a configuration file.
#
if test "$ARCH" = "rs6000" -a $VMPI -ne 0 ; then
    if test "$globus_threads_enabled" = "yes" ; then    
    	GLOBUS_TEST_CLINKER=xlc_r
    	GLOBUS_TEST_CXXLINKER=xlC_r
    	GLOBUS_TEST_F77LINKER=xlf_r
    	GLOBUS_TEST_F90LINKER=xlf90_r
    else
    	GLOBUS_TEST_CLINKER=xlc
    	GLOBUS_TEST_CXXLINKER=xlC
    	GLOBUS_TEST_F77LINKER=xlf
    	GLOBUS_TEST_F90LINKER=xlf90
    fi
fi

#
# When testing for architecure and operating systems features, we may need
# to use a different compiler options (or even a different compiler).
# Setting MPICH_GLOBUS2_CONFIGURE_ACTIVE forces the mpich-globus2 compiler
# scripts to use these alternate settings.
#
MPICH_GLOBUS2_CONFIGURE_ACTIVE=1
export MPICH_GLOBUS2_CONFIGURE_ACTIVE

#
# Create compiler scripts
#
mpidi_mkdir_p ${binbuild_dir}
sed -e "s!@GC@!$GLOBUS_CC!" \
    -e "s!@GCFLAGS@!$GLOBUS_CFLAGS!" \
    -e "s!@GL@!$GLOBUS_CLINKER!" \
    -e "s!@GLFLAGS@!$GLOBUS_CLDFLAGS!" \
    -e "s!@LIBS@!$GLOBUS_LIBS!" \
    -e "s!@TEST_GC@!$GLOBUS_TEST_CC!" \
    -e "s!@TEST_GCFLAGS@!$GLOBUS_TEST_CFLAGS!" \
    -e "s!@TEST_GL@!$GLOBUS_TEST_CLINKER!" \
    -e "s!@TEST_GLFLAGS@!$GLOBUS_TEST_CLDFLAGS!" \
    ${top_srcdir}/mpid/$DEVICE/mpich-globus2-cc.in \
    >${binbuild_dir}/mpich-globus2-cc
chmod +x ${binbuild_dir}/mpich-globus2-cc

if test "$NOCXX" != 1 ; then
    sed -e "s!@GC@!$GLOBUS_CXX!" \
    	-e "s!@GCFLAGS@!$GLOBUS_CXXFLAGS!" \
    	-e "s!@GL@!$GLOBUS_CXXLINKER!" \
    	-e "s!@GLFLAGS@!$GLOBUS_CXXLDFLAGS!" \
    	-e "s!@LIBS@!$GLOBUS_LIBS!" \
    	-e "s!@TEST_GC@!$GLOBUS_TEST_CXX!" \
    	-e "s!@TEST_GCFLAGS@!$GLOBUS_TEST_CXXFLAGS!" \
    	-e "s!@TEST_GL@!$GLOBUS_TEST_CXXLINKER!" \
    	-e "s!@TEST_GLFLAGS@!$GLOBUS_TEST_CXXLDFLAGS!" \
    	${top_srcdir}/mpid/$DEVICE/mpich-globus2-cc.in \
    	>${binbuild_dir}/mpich-globus2-cxx
    chmod +x ${binbuild_dir}/mpich-globus2-cxx
fi

if test $NOF77 -eq 0 ; then
    sed -e "s!@GC@!$GLOBUS_F77!" \
    	-e "s!@GCFLAGS@!$GLOBUS_F77FLAGS!" \
    	-e "s!@GL@!$GLOBUS_F77LINKER!" \
    	-e "s!@GLFLAGS@!$GLOBUS_F77LDFLAGS!" \
    	-e "s!@LIBS@!$GLOBUS_LIBS!" \
    	-e "s!@TEST_GC@!$GLOBUS_TEST_F77!" \
    	-e "s!@TEST_GCFLAGS@!$GLOBUS_TEST_F77FLAGS!" \
    	-e "s!@TEST_GL@!$GLOBUS_TEST_F77LINKER!" \
    	-e "s!@TEST_GLFLAGS@!$GLOBUS_TEST_F77LDFLAGS!" \
    	-e "s!@F77_SED@!$F77_SED!" \
    	${top_srcdir}/mpid/$DEVICE/mpich-globus2-fc.in \
    	>${binbuild_dir}/mpich-globus2-f77
    chmod +x ${binbuild_dir}/mpich-globus2-f77
fi

if test $NO_F90 -eq 0 ; then
    sed -e "s!@GC@!$GLOBUS_F90!" \
    	-e "s!@GCFLAGS@!$GLOBUS_F90FLAGS!" \
    	-e "s!@GL@!$GLOBUS_F90LINKER!" \
    	-e "s!@GLFLAGS@!$GLOBUS_F90LDFLAGS!" \
    	-e "s!@LIBS@!$GLOBUS_LIBS!" \
    	-e "s!@TEST_GC@!$GLOBUS_TEST_F90!" \
    	-e "s!@TEST_GCFLAGS@!$GLOBUS_TEST_F90FLAGS!" \
    	-e "s!@TEST_GL@!$GLOBUS_TEST_F90LINKER!" \
    	-e "s!@TEST_GLFLAGS@!$GLOBUS_TEST_F90LDFLAGS!" \
    	-e "s!@F77_SED@!$F77_SED!" \
    	${top_srcdir}/mpid/$DEVICE/mpich-globus2-fc.in \
    	>${binbuild_dir}/mpich-globus2-f90
    chmod +x ${binbuild_dir}/mpich-globus2-f90
fi

#
# Figure out the size of a particular vendor's MPI type
#
get_vendor_size()
{
    cat >conftest.c <<EOF
#include <mpi.h>
int main(int argc, char ** argv)
{
    printf("%d\n", sizeof($1));
    return 0;
}
EOF
    if test -z "$ac_echo_test" ; then
	echo $ac_n "determing size of vendor's $1""... $ac_c"
    else
	echo $ac_n "determing size of vendor's $1""... $ac_c" 1>&1
    fi

    if $CC -o conftest conftest.c >/dev/null 2>&1; then
        result=`./conftest`
        rm -f conftest*
	if test -z "$ac_echo_test" ; then
	    echo "$ac_t""$result"
	else
	    echo "$ac_t""$result" 1>&1
	fi
	vendor_var=`echo $1 | tr "[a-z]" "[A-Z]"`
	vendor_var=VENDOR_${vendor_var}_SIZE
	eval $vendor_var=$result
    else
	if test -z "$ac_echo_test" ; then
	    echo "$ac_t""FAILED"
	else
	    echo "$ac_t""FAILED" 1>&1
	fi
	echo "ERROR: unable to determine size of $1"
        rm -f conftest*
	exit 1
    fi
}

#
# See if the vendor's MPI support a particular data type
#
check_vendor_datatype()
{
    cat >conftest.c <<EOF
#include <mpi.h>
int main(int argc, char ** argv)
{
    MPI_Datatype foo = $1;
    return 0;
}
EOF
    if test -z "$ac_echo_test" ; then
	echo $ac_n "checking if vendor supports $1""... $ac_c"
    else
	echo $ac_n "checking if vendor supports $1""... $ac_c" 1>&1
    fi

    vendor_var=`echo $1 | tr "[a-z]" "[A-Z]"`
    vendor_var=VENDOR_HAS_${vendor_var}
    if $CC -c conftest.c >/dev/null 2>&1; then
	result=yes
	eval $vendor_var=1
    else
	result=no
	eval $vendor_var=0
    fi
    if test -z "$ac_echo_test" ; then
	echo "$ac_t""$result"
    else
	echo "$ac_t""$result" 1>&1
    fi
    rm -f conftest*
}


#
# If the device is using a vendor implementation of MPI as one of its
# transport mechanisms, then we need to gather some information and set
# some options
#
if test "$vmpi" = "yes" ; then
    echo "Enabling VendorMPI option"
   
    #
    # Determine extra space needed in MPI_Status
    #
    get_vendor_size MPI_Status
    get_vendor_size int
    VENDOR_MPI_STATUS_SIZE=`expr $VENDOR_MPI_STATUS_SIZE + \
	$VENDOR_INT_SIZE - 1`
    VENDOR_MPI_STATUS_SIZE=`expr $VENDOR_MPI_STATUS_SIZE / $VENDOR_INT_SIZE`
    MPI_STATUS_SIZE=`expr $MPI_STATUS_SIZE + $VENDOR_MPI_STATUS_SIZE + 1`
    echo "settting MPI_STATUS_SIZE to $MPI_STATUS_SIZE"

    get_vendor_size MPI_Datatype
    get_vendor_size MPI_Request
    check_vendor_datatype MPI_2COMPLEX
    check_vendor_datatype MPI_2DOUBLE_COMPLEX
    check_vendor_datatype MPI_LONG_LONG
    check_vendor_datatype MPI_LONG_LONG_INT

    #
    # Getting the size of vMPI MPI_Aint and passing that back
    # up to configure at the top level in the variable MPI_AINT_SIZE
    # so that MPICH can pick a C datatype to typedef its MPI_Aint
    # that is exactly the same size as the vMPI's MPI_Aint.
    # NOTE: MPI_Aint must be large enough to hold (void *), but 
    #       it may be larger.
    #
    get_vendor_size MPI_Aint
    MPI_AINT_SIZE = $VENDOR_MPI_AINT_SIZE

    #
    # If the vendor's MPI doesn't support MPI_LONG_LONG or MPI_LONG_LONG_INT
    # then we need to disable long long support in MPICH
    #
    if test $VENDOR_HAS_MPI_LONG_LONG -eq 0 -a \
	    $VENDOR_HAS_MPI_LONG_LONG_INT -eq 0 ; then
	echo "Disabling long long and ROMIO support (requires long long)"
	NO_LONG_LONG="yes"
	enable_args="$enable_args --disable-long-long"
	ROMIO=0
    fi

    #
    # We can't use weak symbols with VMPI because the symbols names in the
    # pragma directives aren't affected by the name change definitions in
    # include/mpich_vmpi.h
    #
    echo "Disabling weak symbols (not compatible with VendorMPI option)"
    TRY_WEAK_SYMBOLS=0
    enable_args="$enable_args --disable-weak-symbols"
    ROMIO_ARGS="$ROMIO_ARGS --disable-weak-symbols"
else
    MPI_STATUS_SIZE=`expr $MPI_STATUS_SIZE + 2`
    VENDOR_MPI_DATATYPE_SIZE=0
    VENDOR_MPI_REQUEST_SIZE=0
    VENDOR_HAS_MPI_2COMPLEX=0
    VENDOR_HAS_MPI_2DOUBLE_COMPLEX=0
    VENDOR_HAS_MPI_LONG_LONG=0
    VENDOR_HAS_MPI_LONG_LONG_INT=0
fi

#
# The Globus data conversion library does not support long double.  Therefore,
# we need to disable support for that type (for now).
#
echo "Disabling long double (not supported by Globus data conversion library)"
NO_LONG_DOUBLE="yes"
enable_args="$enable_args --disable-long-double"

#
# Create the device configuration header file
#
sed -e "s!@VMPI@!$VMPI!" \
    -e "s!@VENDOR_MPI_DATATYPE_SIZE@!$VENDOR_MPI_DATATYPE_SIZE!" \
    -e "s!@VENDOR_MPI_REQUEST_SIZE@!$VENDOR_MPI_REQUEST_SIZE!" \
    -e "s!@VENDOR_HAS_MPI_2COMPLEX@!$VENDOR_HAS_MPI_2COMPLEX!" \
    -e "s!@VENDOR_HAS_MPI_2DOUBLE_COMPLEX@!$VENDOR_HAS_MPI_2DOUBLE_COMPLEX!" \
    -e "s!@VENDOR_HAS_MPI_LONG_LONG@!$VENDOR_HAS_MPI_LONG_LONG!" \
    -e "s!@VENDOR_HAS_MPI_LONG_LONG_INT@!$VENDOR_HAS_MPI_LONG_LONG_INT!" \
    -e "s!@DEBUG_ENABLED@!$DEBUG_ENABLED!" \
    -e "s!@DEBUG_MODULES_ENABLED@!$DEBUG_MODULES_ENABLED!" \
    -e "s!@DEBUG_INFO_ENABLED@!$DEBUG_INFO_ENABLED!" \
    ${top_srcdir}/mpid/${DEVICE}/chconfig.h.in \
    >${builddir}/mpid/${DEVICE}/chconfig.h

#
# Create a device specific settings script
#
sed -e "s!@VMPI@!$VMPI!" \
    -e "s!@F77_SED@!$F77_SED!" \
    ${top_srcdir}/mpid/${DEVICE}/mpich-globus2-settings.in \
    >${builddir}/mpid/${DEVICE}/mpich-globus2-settings

#
# Create the device specific mpirun script
#
if test $globus_packaged -ne 0 ; then
    globus_bindir=$GLOBUS_LOCATION/bin
else
    globus_bindir=$GLOBUSDIR/tools/bin
fi

sed -e "s!@VMPI@!$VMPI!" \
    -e "s!@GLOBUSBINDIR@!$globus_bindir!" \
    ${top_srcdir}/mpid/${DEVICE}/mpirun.globus2.in \
    >${builddir}/mpid/${DEVICE}/mpirun.globus2

#
# Make sure scripts are executable
#
chmod +x ${top_srcdir}/mpid/${DEVICE}/install_globus2

#
# Add a default machines file if one does not exist
#
if [ ! -f ${binbuild_dir}/machines ] ; then
    echo "Creating a default machines file in ${binbuild_dir}.  This will"
    echo "allow testing of MPICH-G2, but you will need to create a new"
    echo "file to exploit the full capabilities of MPICH-G2.  Please see"
    echo "the documentation."
    host=`uname -n 2>&1`
    if [ -n "$host" ] ; then
        echo "$host" >> ${binbuild_dir}/machines
    fi
fi
