#! /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
# 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
# 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
echo "Configuring mpd subsystem..."
export VPATH
(cd mpid/mpd ; \
    $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=$top_srcdir/lib )
if test $? != 0 ; then
    print_error "Configure of the mpd sub-system may have failed!"
    exit 1
fi
echo "Finished configuring the mpd subsystem"
# Run configure in the device directory
export CC 
export VPATH
( cd mpid/ch_p4mpd ; $top_srcdir/mpid/ch_p4mpd/configure --srcdir=$srcdir)
# Variable assignments from configure are in localdefs
#. mpid/ch_p4mpd/localdefs
