# conduit setup script, used by upcr configure and make
# inputs:
# GASNET_CONDUITS - the list of conduits which GASNet actually supports
# CONDUITS - the upcr conduit list, in order of increasingly preferred default
#   MUST be a superset of GASNET_CONDUITS
# PARSEQ - the PARSEQ configure setting
# PERL - location of PERL
# PTHREADS - "yes" if GASNet detected pthreads enabled, "no" otherwise
# outputs:
# CONDUITS - the conduit list, trimmed to include just those in GASNET_CONDUITS
# DEFAULT_CONDUIT - the default conduit to use
# TARGETLIBS - the target libs to build
# ASSISTANTLIBS - the Totalview assistant libs to build
# PARSEQ - the PARSEQ setting, possibly trimmed based on PTHREADS

if test "$CONDUIT_SETUP_MODE" != "configure" -a "$CONDUIT_SETUP_MODE" != "make" ; then
  echo this script is not meant to be run directly
  exit 1
fi

if test "$PERL" = "" -o \
        "$PARSEQ" = "" -o \
        "$PTHREADS" = "" -o \
        "$CONDUITS" = "" -o \
        "$TOTALVIEW_ENABLED" = "" -o \
        "$GASNET_CONDUITS" = "" ; then 
  echo "INTERNAL ERROR: conduit-setup missing an input variable" ; exit 1
fi

# check subset property
for conduit in $GASNET_CONDUITS ; do
 found=`echo " $CONDUITS " | grep " $conduit " ; exit 0`
 if test -z "$found" ; then
  echo "INTERNAL ERROR: conduit subset property violated - CONDUITS must be a superset of GASNET_CONDUITS"
  echo "GASNET_CONDUITS=$GASNET_CONDUITS"
  echo "CONDUITS=$CONDUITS"
 fi
done

# trim space
old_CONDUITS=`echo $CONDUITS`
CONDUITS=
for conduit in $old_CONDUITS ; do
 found=`echo " $GASNET_CONDUITS " | grep " $conduit " ; exit 0`
 if test "$found"; then
   CONDUITS="$CONDUITS $conduit"
   DEFAULT_CONDUIT="$conduit"
 fi
done
# trim space
CONDUITS=`echo $CONDUITS`

if test -z "$CONDUITS" ; then
  echo "ERROR: No GASNet conduits found! See gasnet/configure --help" 
  exit 1
fi 

old_PARSEQ=`echo $PARSEQ`
if test "$old_PARSEQ" = "par seq" -a "$PTHREADS" != "yes" ; then
  PARSEQ="seq" # pthreads missing - disable support
fi

# Determine which libraries are to be built
# - each conduit also has a Totalview upcr and assistant library (seq only)
TARGETLIBS=
ASSISTANTLIBS=
for conduit in $CONDUITS; do
    if test $conduit = "shmem"; then
        TARGETLIBS="libupcr-$conduit-seq.a $TARGETLIBS"
    else
	for parseq in $PARSEQ; do
	    TARGETLIBS="libupcr-$conduit-$parseq.a $TARGETLIBS"
	done
    fi
    # Totalview upcr and assistant library
    if test "$TOTALVIEW_ENABLED" = "yes"; then
        TARGETLIBS="libupcr-$conduit-tv.a $TARGETLIBS"
        ASSISTANTLIBS="libupcda-$conduit-tv.la $ASSISTANTLIBS"
    fi
done

if test "$CONDUIT_SETUP_MODE" = "make" ; then
  if test "$CONDUITS" != "$old_CONDUITS" -o "$PARSEQ" != "$old_PARSEQ" ; then
    echo "-----------------------------------------------------------------------------"
    echo " The configure script for the GASNet networking layer detected support       "
    echo " for the following conduits:                                                 "
    echo "    $GASNET_CONDUITS"                                                       
    echo " If you believe the missing conduits should be enabled (e.g. if you have     "
    echo " Myrinet hardware and gm was not detected, or similarly for the other        "
    echo " native high-performance network APIs), then you should check the GASNet     "
    echo " and Berkeley UPC install documentation to see how to enable that network API"
    echo " for your system (most likely the correct vendor drivers were not found -    "
    echo " see the output from the GASNet configure script and gasnet/config.log       "
    echo " for more info).                                                             "
    echo " "
    echo "Reconfiguring upcr to use the updated conduit list: $CONDUITS" 
    # config.status may be running, so need this hack for cygwin to avoid stupid windows file sharing locks
    mv ./config.status ./config.status.bak
    cp ./config.status.bak ./config.status.new
    for varname in CONDUITS DEFAULT_CONDUIT TARGETLIBS ASSISTANTLIBS PARSEQ ; do
      varval=`eval echo \\$$varname`
      $PERL -pi.orig -e 's/s(.)\@'$varname'\@(.)[a-zA-Z\-\. ]*([^\$]*)/s$1\@'$varname'\@$2'"$varval"'$3/g' ./config.status.new
      # some buggy versions of perl create a backup file even when the options tell it not to - 
      # so we create one with a known extension and delete it
      rm -f ./config.status.new.orig 
    done
    echo "-----------------------------------------------------------------------------"
    mv ./config.status.new ./config.status
    ./config.status
    # JCD: if totalview support on, do same thing for its config.status file
    if test -f ./totalview/config.status; then 
        mv ./totalview/config.status ./totalview/config.status.bak
        cp ./totalview/config.status.bak ./totalview/config.status.new
        for varname in CONDUITS DEFAULT_CONDUIT TARGETLIBS ASSISTANTLIBS PARSEQ ; do
          varval=`eval echo \\$$varname`
          $PERL -pi.orig -e 's/s(.)\@'$varname'\@(.)[a-zA-Z\-\. ]*([^\$]*)/s$1\@'$varname'\@$2'"$varval"'$3/g' ./totalview/config.status.new
          # some buggy versions of perl create a backup file even when the options tell it not to - 
          # so we create one with a known extension and delete it
          rm -f ./totalview/config.status.new.orig 
        done
        echo "-----------------------------------------------------------------------------"
        mv ./totalview/config.status.new ./totalview/config.status
        (cd totalview && ./config.status)
    fi
  fi
fi

