#! /bin/sh
#
# ch_gm setup
#
# Unpack device_args
#
for arg in $device_args $@; do
    case $arg in 
     -echo) set -x 
       ;;
     -disable-registration | --disable-registration)
	otherargs="$otherargs -disable-registration"
       ;;
     -u | -usage | --usage | --usag | --usa | --us | --u | -help | --help )
        # backslash quote caused problems with MacOSX.
        cat >&2 <<EOF
--with-device=$DEVICE[:-disable-registration]

The option '-disable-registration' disables the usage of memory 
registration in GM, using memory copies for all size of messages.  
EOF
	exit 0;
	;;
     *) if test -n "$arg" ; then
	    echo "unrecognized argument $arg"
	    exit 1
	fi
	;;
    esac
done

if [ -z "$top_srcdir" ] ; then 
    top_srcdir="."
fi

echo "About to do ch_gm device"
DEVICE_KIND=network
#
echo "Configuring ch_gm subsystem..."
# Make sure that the lib Makefile gets remade
if test -n "$configure_echo" ; then
    otherargs="$otherargs -echo"
fi
if test -n "$rshcommand" ; then
    otherargs="$otherargs -rsh=$rshcommand"
fi
if test -n "$rshnol" ; then
    otherargs="$otherargs -rshnol"
fi

IS_HETERO=0

# Make sure mpd directory exists
if [ ! -d mpid/mpd ] ; then 
    mkdir mpid/mpd
fi
# Run configure in the mpd directory
RSHCOMMAND="${RSHCOMMAND-$rshcommand}"
echo "Configuring mpd subsystem..."
(cd mpid/mpd ; \
    export VPATH ; export RSHCOMMAND ;\
    export CC ; export CLINKER ; \
    $top_srcdir/mpid/mpd/configure --with-cc="$CC" \
                --with-opt="$OPTFLAGS" \
                --with-top_srcdir=$top_srcdir \
                -srcdir=$top_srcdir/mpid/mpd \
		-prefix=$prefix \
		-exec-prefix=$exec_prefix \
                -libdir=$libdir $enable_args )
if test $? != 0 ; then
    echo "Configure of the mpd sub-system may have failed!"
    exit 1
fi
if test "$COMM" = "bproc" ; then
    # Here would go bproc options, such as
    #LIBS="$LIBS -lbproc -Wl,--undefined=beowulf_sched_shim,--undefined=get_beowulf_job_map  -lbeomap -lbeostat -ldl
    # Note that we have to make sure that this is exported back up to the MPICH
    # configure
    # Do define the remote shell and copy commands
    RSHCOMMAND="/usr/bin/bpsh"
    # Note that RCPCOMMAND is not yet exported to mpirun.ch_gm.in
    RCPCOMMAND="${RCPCOMMAND-/usr/bin/bpcp}"

fi
#
# The following were moved here from the top-level configure
if test -s mpid/mpd/mpd.hosts  ; then
    # Is this the right thing to do?  
    /bin/rm -f mpid/mpd/mpd.hosts
fi
HOST=""
HOST="`hostname 2>/dev/null`"
# Note that uname -n may not produce a usable hostname.  
# Any suggestions?
if test "$HOST" = "" ; then
    HOST="`uname -n`"
fi
cat > mpid/mpd/mpd.hosts <<EOF
# Change this file to contain the machines that you want to use
# to run MPI jobs on.  The format is one host name per line, as in
#    hostname
# The hostname should be the same as the result from the command "hostname"
$HOST
$HOST
$HOST
$HOST
$HOST
EOF
#
echo "Finished configuring the mpd subsystem"
# Run configure in the device directory
export CC
export VPATH

eval DEFAULT_MACHINES_DIR=${datadir}
eval DEFAULT_MACHINES_DIR=$DEFAULT_MACHINES_DIR
export CC ; export RSHCOMMAND ; export ARCH ; export DEFAULT_MACHINES_DIR
# Remove the configuration file if present
rm -f $top_srcdir/mpid/ch_gm/mpich-mpid.h mpid/ch_gm/mpich-mpid.h
(cd mpid/ch_gm ; $top_srcdir/mpid/ch_gm/configure $otherargs)
# Variable assignments from configure are in localdefs
if [ ! -f mpid/ch_gm/localdefs ] ; then
    echo "Failed to build file of local definitions"
    echo "configure in mpid/ch_gm failed."
    exit 1
fi
. mpid/ch_gm/localdefs
#
if test -n "$rshcommand" ; then
    otherargs="$otherargs -rsh='"$rshcommand"'"
elif test -n "$RSHCOMMAND" ; then 
    otherargs="$otherargs -rsh='"$RSHCOMMAND"'"
fi

# Setup the MPIBOOT etc.
# Use eval to expand the value of bindir.  Note that bindir is often
# set to ${exec_prefix}/bin, so we eval again
eval MPIBOOT=${bindir}/mpichboot
eval MPIBOOT=$MPIBOOT
eval MPIUNBOOT=${bindir}/mpichstop
eval MPIUNBOOT=$MPIUNBOOT
