#!/bin/sh

DARSHAN_LIB_PATH="/usr/lib64/mpich/lib"
DARSHAN_SHARE_PATH="/usr/share/darshan"
DARSHAN_LD_FLAGS="-Wl,-z,relro "
DARSHAN_LOG_PATH=""
DARSHAN_LOG_ENV="DARSHAN_LOGPATH"

# NOTE:
# - we deliberately list libdarshan twice in the link command when
#   statically linking.  The first sets symbol wrapping options early in the
#   link line. The final one is necessary to give the linker a change to
#   resolve indirect dependencies on PnetCDF and HDF5 symbols (if the
#   app used a library which in turn used one of those HLLs).

PRE_LD_FLAGS="-L$DARSHAN_LIB_PATH $DARSHAN_LD_FLAGS -ldarshan -lz -Wl,@$DARSHAN_SHARE_PATH/ld-opts/darshan-base-ld-opts"
POST_LD_FLAGS="-L$DARSHAN_LIB_PATH -ldarshan -lz -lrt -lpthread"

# NOTE:
# - when dynamic linking there is no need for wrapping options, we simply
#   need to get the darshan symbol definitions early enough in the link
#   order.  We also set no-as-needed for linkers that may not identify
DYN_LD_FLAGS="-L$DARSHAN_LIB_PATH $DARSHAN_LD_FLAGS -Wl,-rpath=$DARSHAN_LIB_PATH -Wl,-no-as-needed -ldarshan "

# NOTE:
# - construct complete list of log path options, separated by commas.
#   LOG_ENV takes precedent over LOG_PATH, but both could be specified.  The
#   LOG_ENV may already be a comma saparated list, but we insert '$'
#   characters to denote that these are environment variables.
if [ "$DARSHAN_LOG_ENV" ]; then
    DARSHAN_LOG_ENV=`echo $DARSHAN_LOG_ENV | sed -e 's/^/\$/g'`
    DARSHAN_LOG_ENV=`echo $DARSHAN_LOG_ENV | sed -e 's/,/,\$/g'`
fi

# figure out if we need a comma separator to construct env and path list
if [ "$DARSHAN_LOG_ENV" -a "$DARSHAN_LOG_PATH" ]; then
    DARSHAN_LOG_ALL="$DARSHAN_LOG_ENV,$DARSHAN_LOG_PATH"
elif [ "$DARSHAN_LOG_ENV" ]; then
    DARSHAN_LOG_ALL=$DARSHAN_LOG_ENV
else
    DARSHAN_LOG_ALL=$DARSHAN_LOG_PATH
fi

usage="\
Usage: darshan-config [--pre-ld-flags] [--post-ld-flags] [--dyn-ld-flags] [--log-path]"

if test $# -eq 0; then
      echo "${usage}" 1>&2
      exit 1
fi

while test $# -gt 0; do
  case "$1" in
  -*=*) optarg=`echo "$1" | sed 's/[-_a-zA-Z0-9]*=//'` ;;
  *) optarg= ;;
  esac

  case $1 in
    --pre-ld-flags)
      echo $PRE_LD_FLAGS
      ;;
    --post-ld-flags)
      echo $POST_LD_FLAGS
      ;;
    --dyn-ld-flags)
      echo $DYN_LD_FLAGS
      ;;
    --log-path)
      echo $DARSHAN_LOG_ALL
      ;;
    *)
      echo "${usage}" 1>&2
      exit 1
      ;;
  esac
  shift
done
