#! /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 )
        # backslash quote caused problems with MacOSX.
        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 p4$P4EXT device"
DEVICE_KIND=network
# In the following, we add $P4EXT 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)
  # It also makes Cygwin look like Linux, since both are GNU-based
  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/freebsd_ppc/FREEBSD_PPC/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/CYGWIN_NT/LINUX/g' \
            -e 's/sppux/HP/g'              -e 's/netbsd/NETBSD/g'` 
fi
echo "Finished setting p4arch..."
#
echo "Configuring p4$P4EXT subsystem..."
baseotherargs=
# 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"
    baseotherargs="$baseotherargs --enable-echo"
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 "$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_p4.in
    RCPCOMMAND="${RCPCOMMAND-/usr/bin/bpcp}"
fi
#
# Run configure in the device directory
# What is this here for?  It is:
#     For the timer stuff
#     for an rsh command
#     To check whether shared-memory works
RSHCOMMAND="${RSHCOMMAND-$rshcommand}"
export CC ; export CFLAGS ; export RSHCOMMAND 
export CROSS_SIZEOF_INT ; export CROSS_SIZEOF_LONG
# Remove the configuration file if present
rm -f $top_srcdir/mpid/ch_p4/mpich-mpid.h mpid/ch_p4/mpich-mpid.h mpid/ch_p4/localdefs
# Export ARCH so that the machines file will get made in the right place
(cd mpid/ch_p4 ; export ARCH ; \
"$top_srcdir/mpid/ch_p4/configure" --with-comm=$COMM $baseotherargs) # --srcdir=$srcdir)
# Variable assignments from configure are in localdefs
if [ ! -f mpid/ch_p4/localdefs ] ; then
    echo "Failed to build file of local definitions"
    echo "configure in mpid/ch_p4 failed."
    exit 1
fi
. mpid/ch_p4/localdefs
#
if test -n "$rshcommand" ; then
    otherargs="$otherargs -rsh='"$rshcommand"'"
elif test -n "$RSHCOMMAND" ; then 
    otherargs="$otherargs -rsh='"$RSHCOMMAND"'"
fi

# specify the C compiler in case it is different from the P4 choice
if [ ! -d mpid/ch_p4$P4EXT/p4$P4EXT ] ; then 
    mkdir mpid/ch_p4$P4EXT/p4$P4EXT
fi
export VPATH
# We use the eval to make sure that otherargs is evaluate correct in the
# case where it contains quoted arguments with blanks 
# (e.g., rshcommand="ssh -q")
(cd mpid/ch_p4$P4EXT/p4$P4EXT ; \
eval "$top_srcdir/mpid/ch_p4$P4EXT/p4$P4EXT/configure" -p4arch=$P4_ARCH \
    -p4make="'$MAKE'" -opt="'$OPTFLAGS'" -cc="'$CC'" -p4comm="$COMM" \
    -p4device=$DEVICE -srcdir=$top_srcdir/mpid/ch_p4$P4EXT/p4$P4EXT \
    -exec-prefix=$exec_prefix \
    -libbuild_dir=$libbuild_dir \
    -cflags="'$USER_CFLAGS $P4_CFLAGS $CC_SHARED_OPT'" $otherargs $enable_args )
if test $? != 0 ; then
    print_error "Configure of the p4$P4EXT sub-system may have failed!"
    exit 1
fi
echo "Finished configuring the p4$P4EXT subsystem"
P4_MDEPLIBS=`$MAKE -f mpid/ch_p4$P4EXT/p4$P4EXT/lib/Makefile p4mdeplibs | grep -v make`
LIB_LIST="$LIB_LIST $P4_MDEPLIBS"
#
P4_MDEPCFLAGS=`$MAKE -f mpid/ch_p4$P4EXT/p4$P4EXT/lib/Makefile p4mdepcflags | grep -v make`
if test -n "$P4_MDEPCFLAGS" ; then
    CFLAGS="$CFLAGS $P4_MDEPCFLAGS"
fi
P4_MDEPFFLAGS=`$MAKE -f mpid/ch_p4$P4EXT/p4$P4EXT/lib/Makefile p4mdepfflags | grep -v make`
if test -n "$P4_MDEPFFLAGS" ; then
    FFLAGS="$FFLAGS $P4_MDEPFFLAGS"
fi

# Look for SIGBLOCK used
if grep HAVE_SIGBLOCK mpid/ch_p4/p4/lib/Makefile >/dev/null 2>&1 ; then
    device_functions="sigblock"
fi
# These directories are built above.  
#if test ! -d build/$ARCH/ch_p4$P4EXT/lib ; then 
#    mkdir build/$ARCH/ch_p4$P4EXT/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
#
# Try to configure the secure server
if [ ! -d mpid/server ] ; then mkdir mpid/server ; fi
echo "Configuring new p4 secure server"
(cd mpid/server ; \
    CC="$CC" ; export CC ; export CFLAGS ; \
    "$top_srcdir/mpid/server/configure" )
if test $? != 0 ; then
    print_error "Configure of the secure server may have failed!"
    print_error "The secure server is not required for p4, but can be "
    print_error "used to speedup process startup."
    # No exit, because we want to be able to continue
fi
#
echo "Finished configuring the p4 secure server"
