#! /bin/sh
#
# ch_p4 and ch_p4mpd setup
#
# Unpack device_args
#
for arg in $device_args $@; do
    case $arg in 
     -echo) set -x 
       ;;
      -listenersig=*)
         listener_sig=`echo A$arg|sed -e 's/A-listenersig=//g'`
         ;;

      -listener_sig=*)
         listener_sig=`echo A$arg|sed -e 's/A-listener_sig=//g'`
         ;;

     -dlast | --dlast)
	# Note that this is supported only for p4 now
	DLAST="-dlast"
	;;

      -socksize=*|--socksize=*)
   	SOCKSIZE="`echo $arg|sed 's/-*socksize=//'`"
     	;;

     -u | -usage | --usage | --usag | --usa | --us | --u | -help | --help )
        cat >&2 <<EOF
--with-device=$DEVICE[:-listener_sig=SIGNALNAME][-dlast][-socksize=BYTES]

The option '-listener_sig' applies to the $DEVICE device, and changes the
signal that is used to signal that a new connection should be made.  By
default, SIGUSR1 is used.

The option '-dlast' causes the p4 device to remember the last few debugging
messages, printing them out only when the job terminates abnormally.

The option '-socksize' changes the size of the socket buffers used.  
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 p4mpd device"
DEVICE_KIND=network
#
# The ch_p4mpd device makes use of the ch_p4/p4 code.  Rather than 
# always copy the data, we make sure that we have links to the appropriate 
# directories.
# First, the default directories
use_link=yes
for dir in alog bin servers usc doc lib_f messages contrib contrib_f \
    messages_f misc monitors ; do
    if [ ! -d $top_srcdir/mpid/ch_p4mpd/p4mpd/$dir ] ; then
        # Remove it incase an old link is now bogus
        rm -f $top_srcdir/mpid/ch_p4mpd/p4mpd/$dir 
        ln -s $top_srcdir/mpid/ch_p4/p4/$dir \
	      $top_srcdir/mpid/ch_p4mpd/p4mpd/$dir
    fi
done
# Now, the trickier ones: lib and .
# These are files that had minor differences
for file in p4_MD.c p4_MD.h p4_alloc.c p4_args.c p4_broadcast.c \
    p4_debug.c p4_defs.h p4_funcs.h p4_error.c \
    p4_rm.c p4_secure.c p4_sock_conn.c p4_sock_cr.c p4_sock_sr.c \
    p4_sock_util.c p4_sock_util.h p4_sys.h \
    p4_sys_funcs.h xdr_float.c ; do
    if [ ! -f $top_srcdir/mpid/ch_p4mpd/p4mpd/lib/$file ] ; then
        if [ "$use_link" = "yes" ] ; then
            # Remove it incase an old link is now bogus
            rm -f $top_srcdir/mpid/ch_p4mpd/p4mpd/lib/$file
            ln -s $top_srcdir/mpid/ch_p4/p4/lib/$file \
	         $top_srcdir/mpid/ch_p4mpd/p4mpd/lib/$file 
	else
            cp  $top_srcdir/mpid/ch_p4/p4/lib/$file \
	         $top_srcdir/mpid/ch_p4mpd/p4mpd/lib/$file 
	    chmod a-w $top_srcdir/mpid/ch_p4mpd/p4mpd/lib/$file 
        fi
    fi
done
# These were identical files between ch_p4/p4/lib and ch_p4mpd/p4mpd/lib
for file in listener_p4.c p4.h p4_fc.c p4_globals.c p4_globals.h \
    p4_macros.h p4_mon.c p4_mon.h p4_patchlevel.h p4_procgroup.c \
    p4_shmem_sr.c p4_sr.h sfcdchi.h makefile.proto ; do
    if [ ! -f $top_srcdir/mpid/ch_p4mpd/p4mpd/lib/$file ] ; then
        if [ "$use_link" = "yes" ] ; then
            # Remove it incase an old link is now bogus
            rm -f $top_srcdir/mpid/ch_p4mpd/p4mpd/lib/$file
            ln -s $top_srcdir/mpid/ch_p4/p4/lib/$file \
	          $top_srcdir/mpid/ch_p4mpd/p4mpd/lib/$file 
        else
            cp $top_srcdir/mpid/ch_p4/p4/lib/$file \
	          $top_srcdir/mpid/ch_p4mpd/p4mpd/lib/$file 
	    chmod a-w $top_srcdir/mpid/ch_p4mpd/p4mpd/lib/$file 
        fi
    fi
done
# These were identical files between ch_p4 and ch_p4mpd
for file in CHANGES MACHINES OPTIONS README config.guess defs.all \
    p4_config.h.in ; do 
    if [ ! -f $top_srcdir/mpid/ch_p4mpd/p4mpd/$file ] ; then
        if [ "$use_link" = "yes" ] ; then
            # Remove it incase an old link is now bogus
            rm -f $top_srcdir/mpid/ch_p4mpd/p4mpd/$file
            ln -s $top_srcdir/mpid/ch_p4/p4/$file \
	          $top_srcdir/mpid/ch_p4mpd/p4mpd/$file 
	else
            cp $top_srcdir/mpid/ch_p4/p4/$file \
	          $top_srcdir/mpid/ch_p4mpd/p4mpd/$file 
	    chmod a-x $top_srcdir/mpid/ch_p4mpd/p4mpd/$file 
        fi
    fi
done
# These were modified
for file in configure configure.in ; do
    if [ ! -f $top_srcdir/mpid/ch_p4mpd/p4mpd/$file ] ; then
        if [ "$use_link" = "yes" ] ; then
            # Remove it incase an old link is now bogus
            rm -f $top_srcdir/mpid/ch_p4mpd/p4mpd/$file
            ln -s $top_srcdir/mpid/ch_p4/p4/$file \
	          $top_srcdir/mpid/ch_p4mpd/p4mpd/$file 
	else 
            cp $top_srcdir/mpid/ch_p4/p4/$file \
	          $top_srcdir/mpid/ch_p4mpd/p4mpd/$file 
	    chmod a-w $top_srcdir/mpid/ch_p4mpd/p4mpd/$file 
        fi
    fi
done
#
# In the following, we add mpd to get the ch_p4mpd device if specified
#  INCLUDE_PATH="$INCLUDE_PATH -I$MPIR_HOME/mpid/ch_p4/p4/include"
# This needs to get the other libraries as required for the specific
# architecture.  
#
# P4's arch specification is different from MPI's (sun4 vs. SUN, etc.)
# Here we set P4_ARCH to the right value, which we deduce from $ARCH and $COMM
# Note that the IRIX code further below may make further changes to 
# P4_ARCH
if test "$ARCH" = "rs6000"; then
  case $COMM in
    eui|mpl)
      P4_ARCH=SP1_EUI ;;
    p4)
      P4_ARCH=RS6000 ;;
    *)
      P4_ARCH=RS6000 ;;
  esac

  ########################################################################
  #
  # Document the IRIX p4 parameter selection a bit, 7/13/95, bri@sgi.com
  #
  #       OS      COMM    CPU             P4_ARCH         DEFAULT_MACHINE
  #       --      ----    ---             -------         ---------------
  #       6.x     shared  R8000           SGI_CH64        sgi_mp
  #       6.x     shared  *               SGI_CH
  #       6.x     *       R8000           SGI             sgi
  #       6.x     *       *               SGI
  #       5.x     shared  *               SGI_MP          sgi_mp
  #       5.x     *       *               SGI
  #       *       *       *               SGI
  # 
  # What to do with R10000?  R5000?
  ########################################################################
  # NEW TABLE
  #
  #       OS      COMM    CPU             P4_ARCH         Other options
  #       --      ----    ---             -------         ---------------
  #       6                               SGI6
  #       6       shared                  SGI6            -p4comm=shared
  #       5                               SGI5
  #       5       shared                  SGI5            -p4comm=shared
  #       *                               SGI
  #       *       shared                  SGI             -p4comm=shared
  #
  # Also supported are special cases of SGI6: SGIN32, SGI32, and SGI64
  #              
  ########################################################################

elif test "$ARCH" = "IRIX64" ; then
  P4_ARCH=SGI6       #SGI_CH64
  #    DEFAULT_MACHINE="sgi_mp"
elif test "$ARCH" = "IRIX" ; then
  P4_ARCH=SGI
  if test $osversion = 6 ; then
#        if test "$COMM" = "shared" ; then
#  	    # Use IRIX64 for SGI_CH64
# 	    P4_ARCH=SGI_CH32
##	    if test $cputype = 8000 ; then
##	        P4_ARCH=SGI_CH32
#	        DEFAULT_MACHINE="sgi_mp"
##	    fi
#	else
     	    P4_ARCH=SGI6
#	    if test $cputype = 8000 ; then
#	        P4_ARCH=SGI6
#	        #DEFAULT_MACHINE="sgi"
#	    fi
#	fi
  elif test $osversion = 5 ; then 
      P4_ARCH=SGI5         # SGI
#        if test "$COMM" = "shared" ; then
#	    P4_ARCH=SGI_MP
#	    DEFAULT_MACHINE="sgi_mp"
#	fi
  fi
  if test "$COMM" = "shared" ; then
      DEFAULT_MACHINE="sgi_mp"
  fi
else
  # Note that this will change IRIXxxx to SGIxxx (i.e., IRIX32 to SGI32)
  P4_ARCH=`echo $ARCH | sed \
	    -e 's/sun4/SUN/g'              -e 's/intelnx/IPSC860/g'  \
	    -e 's/IRIX/SGI/g'              -e 's/hpux/HP/g'          \
	    -e 's/solaris86/I86_SOLARIS/g' -e 's/solaris/SUN_SOLARIS/g' \
	    -e 's/c2mp/CONVEX/g'           -e 's/uxpv/UXPV/g'          \
	    -e 's/alpha/ALPHA/g'           -e 's/dec5000/DEC5000/g'       \
	    -e 's/NeXT/NEXT/g'             -e 's/paragon/PARAGONIP/g'     \
	    -e 's/inteldelta/DELTA/g'      -e 's/symmetry/SYMMETRY/g'\
	    -e 's/cray/CRAY/g'             -e 's/tc2000/TC_2000/g'   \
	    -e 's/ksr/KSR/g'               -e 's/freebsd/FREEBSD/g'  \
	    -e 's/cm5/CM5/g'               -e 's/meiko/MEIKO_CS2/g'  \
            -e 's/rs6000/RS6000/g'	   -e 's/symm_ptx/SYMMETRY_PTX/g' \
            -e 's/sppux/HP/g'              -e 's/netbsd/NETBSD/g'`
fi
echo "Finished setting p4arch..."
#
echo "Configuring p4mpd subsystem..."
# Make sure that the lib Makefile gets remade
if test -n "$SOCKSIZE" ; then
    otherargs="-socksize=$SOCKSIZE"
fi
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
if test -n "$DLAST" ; then
    otherargs="$otherargs $DLAST"
fi
if test -n "$listener_sig" ; then
    otherargs="$otherargs -listenersig=$listener_sig"
fi
if test -n "$P4_OPTS" ; then
    otherargs="$otherargs $P4_OPTS"
fi
if test -n "$enable_strict" ; then
    otherargs="--enable-strict=$enable_strict"
    export enable_strict_done
fi
if test -n "$enable_g" ; then
    otherargs="--enable-g=$enable_g"
    export enable_g_simple
fi
# specify the C compiler in case it is different from the P4 choice
if [ ! -d mpid/ch_p4mpd/p4mpd ] ; then 
    mkdir mpid/ch_p4mpd/p4mpd
fi
export VPATH
(cd mpid/ch_p4mpd/p4mpd ; \
$top_srcdir/mpid/ch_p4mpd/p4mpd/configure -p4arch=$P4_ARCH \
    -p4make="$MAKE" -opt="$OPTFLAGS" -cc="$CC" -p4comm="$COMM" \
    -p4device=$DEVICE -srcdir=$top_srcdir/mpid/ch_p4mpd/p4mpd \
    -exec-prefix=$exec_prefix \
    -libbuild_dir=$libbuild_dir \
    -cflags="$USER_CFLAGS $P4_CFLAGS $CC_SHARED_OPT" $otherargs )
if test $? != 0 ; then
    print_error "Configure of the p4mpd sub-system may have failed!"
    exit 1
fi
echo "Finished configuring the p4mpd subsystem"
P4_MDEPLIBS=`$MAKE -f mpid/ch_p4mpd/p4mpd/lib/Makefile p4mdeplibs | grep -v make`
LIB_LIST="$LIB_LIST $P4_MDEPLIBS"
#
P4_MDEPCFLAGS=`$MAKE -f mpid/ch_p4mpd/p4mpd/lib/Makefile p4mdepcflags | grep -v make`
if test -n "$P4_MDEPCFLAGS" ; then
    CFLAGS="$CFLAGS $P4_MDEPCFLAGS"
fi
P4_MDEPFFLAGS=`$MAKE -f mpid/ch_p4mpd/p4mpd/lib/Makefile p4mdepfflags | grep -v make`
if test -n "$P4_MDEPFFLAGS" ; then
    FFLAGS="$FFLAGS $P4_MDEPFFLAGS"
fi

# These directories are built above.  
#if test ! -d build/$ARCH/ch_p4mpd/lib ; then 
#    mkdir build/$ARCH/ch_p4mpd/lib
#fi 
#   
#   We need to change the makefile.protos in mpid/ch_p4/p4 to use
#   the correct libraries
#   if test -n "$COMM" ; then 
#       COMM="ch_p4_$COMM"
#   else
     COMM="ch_p4"
#   fi
#  if test ! -d $libdir ; then 
#       mkdir $libdir
#  fi 
#   p4 library is intergrated with libmpi.a...
#   LIB_PATH="$LIB_PATH -L$libdir"
#   LIB_LIST="$LIB_LIST -lp4";
IS_HETERO=1
# Look for SIGBLOCK used
if grep HAVE_SIGBLOCK mpid/ch_p4mpd/p4mpd/lib/Makefile >/dev/null 2>&1 ; then
    device_functions="sigblock"
fi
# Make sure mpd directory exists
if [ ! -d mpid/mpd ] ; then 
    mkdir mpid/mpd
fi
# Run configure in the mpd directory.  Include any with_args from the top
# level configure
RSHCOMMAND="${RSHCOMMAND-$rshcommand}"
echo "Configuring mpd subsystem..."
# We set CFLAGS to ensure that mpd gets, in particular, any Shared library
# options.
(cd mpid/mpd ; \
    CFLAGS="$USER_CFLAGS $P4_CFLAGS $CC_SHARED_OPT" \
    export VPATH ; export RSHCOMMAND ;\
    export CC ; export CLINKER ; export CFLAGS ; export CC_SHARED_OPT ; \
    export SHAREDKIND ; \
    $top_srcdir/mpid/mpd/configure --with-cc="$CC" \
                --with-opt="$OPTFLAGS" \
                --with-top_srcdir=$top_srcdir $with_args \
                -srcdir=$top_srcdir/mpid/mpd \
		-prefix=$prefix \
		-exec-prefix=$exec_prefix \
                -libdir=$libdir $enable_args )
if test $? != 0 ; then
    print_error "Configure of the mpd sub-system may have failed!"
    exit 1
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 CFLAGS
export VPATH
( cd mpid/ch_p4mpd ; export VPATH ; export RSHCOMMAND ;\
  $top_srcdir/mpid/ch_p4mpd/configure --srcdir=$top_srcdir/mpid/ch_p4mpd)
# Variable assignments from configure are in localdefs
#. mpid/ch_p4mpd/localdefs
#
# 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
