# -*- shell-script -*-
#
########################################################################
# Start Lmod BASHENV
########################################################################

# Remember the XTRACE and VERBOSE states so that they are restored
# at the end of this script.
[[ ${-/x} != $- ]] && XTRACE_STATE="-x"  || XTRACE_STATE="+x"
[[ ${-/v} != $- ]] && VERBOSE_STATE="-v" || VERBOSE_STATE="+v"
set +xv     # Turn off tracing for now.

export LMOD_PKG=/usr/share/lmod/lmod
export LMOD_DIR=/usr/share/lmod/lmod/libexec
export LMOD_CMD=/usr/share/lmod/lmod/libexec/lmod
export MODULESHOME=/usr/share/lmod/lmod
export LMOD_SETTARG_CMD=":"
export LMOD_FULL_SETTARG_SUPPORT=no

########################################################################
#  Define the module command:  The first line runs the "lmod" command
#  to generate text:
#      export PATH="..."
#  then the "eval" converts the text into changes in the current shell.
#
#  The second command is the settarg command.  Normally LMOD_SETTARG_CMD
#  is undefined or is ":".  Either way the eval does nothing.  When the
#  settarg module is loaded, it defines LMOD_SETTARG_CMD.  The settarg
#  command knows how to read the ModuleTable that Lmod maintains and
#  generates a series of env. vars that describe the current state of
#  loaded modules.  So if one is on a x86_64 linux computer with gcc/4.7.2
#  and openmpi/1.6.3 loaded, then settarg will assign:
#
#     TARG=_x86_64_gcc-4.7.2_openmpi-1.6.3
#     TARG_COMPILER=gcc-4.7.2
#     TARG_COMPILER_FAMILY=gcc
#     TARG_MACH=x86_64
#     TARG_MPI=openmpi-1.6.3
#     TARG_MPI_FAMILY=openmpi
#     TARG_SUMMARY=x86_64_gcc-4.7.2_openmpi-1.6.3
#     TARG_TITLE_BAR=gcc-4.7.2 O-1.6.3
#     TARG_TITLE_BAR_PAREN=(gcc-4.7.2 O-1.6.3)
#
#  unloading openmpi/1.6.3 automatically changes these vars to be:
#
#     TARG=_x86_64_gcc-4.6.3
#     TARG_COMPILER=gcc-4.6.3
#     TARG_COMPILER_FAMILY=gcc
#     TARG_MACH=x86_64
#     TARG_SUMMARY=x86_64_gcc-4.6.3
#     TARG_TITLE_BAR=gcc-4.6.3
#     TARG_TITLE_BAR_PAREN=(gcc-4.6.3)
#
# See Lmod web site for more details.

module()
{
  eval $($LMOD_CMD bash "$@") && eval $(${LMOD_SETTARG_CMD:-:} -s sh)
} 

LMOD_VERSION="7.4.17"
export LMOD_VERSION

if [ "${LMOD_SETTARG_CMD:-:}" != ":" ]; then
  settarg () {
    eval $(${LMOD_SETTARG_CMD:-:} -s sh "$@" )
  }
fi


########################################################################
#  ml is a shorthand tool for people who can't type moduel, err, module
#  It is also a combination command:
#     ml            -> module list
#     ml gcc        -> module load gcc
#     ml -gcc intel -> module unload gcc; module load intel
#  It does much more do: "ml --help" for more information.


unalias ml 2> /dev/null || true
ml()
{
  eval $($LMOD_DIR/ml_cmd "$@")
}

if [ -n "${BASH_VERSION:-}" -a "yes" != no ]; then
  export -f module
  export -f ml
fi
unset export_module

########################################################################
#  clearMT removes the ModuleTable from your environment.  It is rarely
#  needed but it useful sometimes.

clearMT()
{
  eval $($LMOD_DIR/clearMT_cmd bash)
}

########################################################################
#  The following make the action of the settarg available to the titlebar
#  for both xterm's and screen but only for interactive shells.
if [ -n "${PS1:-}" ]; then
  if [ -n "${LMOD_FULL_SETTARG_SUPPORT:-}" -a "$LMOD_FULL_SETTARG_SUPPORT" != no ]; then
    xSetTitleLmod()
    {
      builtin echo -n -e "\033]2;$1\007";
    }
    SET_TITLE_BAR=:

    case "$TERM" in
      xterm*)
        SET_TITLE_BAR=xSetTitleLmod
        ;;
    esac

    SHOST=${SHOST-${HOSTNAME%%.*}}
    precmd()
    {
      eval $(${LMOD_SETTARG_CMD:-:} -s bash)
      ${SET_TITLE_BAR:-:} "${TARG_TITLE_BAR_PAREN}${USER}@${SHOST}:${PWD/#$HOME/~}"
      ${USER_PROMPT_CMD:-:}
    }

    # define the PROMPT_COMMAND to be precmd iff it isn't defined already.
    : ${PROMPT_COMMAND:=precmd}
  fi
fi

########################################################################
#  Make tab completions available to bash users.

if [ ${BASH_VERSINFO:-0} -ge 3 ] && [ -r  /usr/share/lmod/lmod/init/lmod_bash_completions ] && [ -n "${PS1:-}" ]; then
 . /usr/share/lmod/lmod/init/lmod_bash_completions
fi



# Restoring XTRACE and VERBOSE states.
set  $XTRACE_STATE $VERBOSE_STATE
unset XTRACE_STATE  VERBOSE_STATE

########################################################################
# End Lmod BASHENV
########################################################################
#
# Local Variables:
# mode: shell-script
# indent-tabs-mode: nil
# End:
