# -----------------------------------------------------------------------------
# script to start a JWM / Linux session
#
# usage:
#
#  . init_session
#
# History
#   30.10.2013/bs
#     first public release
#
#   29.08.2018 /bs
#     added the python directory from JWM to the variable PYTHONPATH
#
# search and read the general include file
#
JWM_ENV_FILE="jwm_env"
if [ -r ./${JWM_ENV_FILE} ] ; then
  . ./${JWM_ENV_FILE}

   export JWM_SSH_SESSION=0
   JWM_ENV_OK=0

elif [ -r ./jwm/${JWM_ENV_FILE} ] ; then
  . ./${JWM_ENV_FILE}

   export JWM_SSH_SESSION=0
   JWM_ENV_OK=0
else
  echo "ERROR: ./jwm directory not found."
  JWM_ENV_OK=1
fi

# current directory is now the JWM / Linux directory
#
ENV_SCRIPT="init_session"

if [[ "$0" == */${ENV_SCRIPT} ]] ; then
  echo "ERROR: This script should be used via \". ./${ENV_SCRIPT}\". Exiting now ..."
  JWM_ENV_OK=1
fi

# set the environment variable for JWM / Linux sessions
#
export JWM_DIR="${THIS_DIR}"

if [ ${JWM_ENV_OK} = 0 ] ; then
  logMsg "Starting environment for \"${ARCHIVE_DESC}\" ..."

  if [ "${JWM_ENV_INIT_OK}"x = "0"x ] ; then
    logMsg "JWM environment already set (JWM_ENV_INIT_OK=${JWM_ENV_INIT_OK}) - nothing to do now."
  else
    logMsg "Initializing JWM environment ..."
    LD_LIBRARY_PATH=${LD_LIBRARY_PATH}:${THIS_DIR}/lib
    PATH=${THIS_DIR}/scripts:${THIS_DIR}/bin:${THIS_DIR}/sbin:$PATH:${THIS_DIR}/busybox:${THIS_DIR}/bin1
    MANPATH=${THIS_DIR}/share/man:$MANPATH
    export LD_LIBRARY_PATH PATH MANPATH
    export PS1="[JWM session] $PS1"

    if [ -d "${JWM_DIR}/perf-tools" ] ; then
      PATH=$PATH:${JWM_DIR}/perf-tools:
      MANPATH=$MANPATH:${JWM_DIR}/perf-tools/man
      export PATH MANPATH
    fi

    if [ -d ${JWM_DIR}/lib/python2.7/site-packages ] ; then
      export PYTHONPATH=${PYTHONPATH}:${JWM_DIR}/lib/python2.7/site-packages/
    fi

    if [ "${JWM_DIR}"x != "${THIS_DIR}"x ] ; then
      logWarning "Some tools are compiled for the directory \"${JWM_DIR}\" and might NOT work in this environment."
    fi

    logMsg "JWM environment initialized. The tools available are:"
    ls ${THIS_DIR}/bin  ${THIS_DIR}/sbin ${THIS_DIR}/bin1  ${THIS_DIR}/scripts
    echo ""
    echo "Additional general Linux tools are in the directory ${THIS_DIR}/busybox"
    echo ""
    echo "Additional performance monitoring tools are in ${THIS_DIR}/perf-tools"
    echo ""

    alias jwm_version="echo \"${ARCHIVE_DESC}\""
    alias jwm_tools="ls ${THIS_DIR}/bin  ${THIS_DIR}/sbin ${THIS_DIR}/scripts ${THIS_DIR}/busybox"
    alias jwm_dir="echo \"JWM / Linux base dir is ${JWM_DIR}\" "
    alias jwm_home="cd ${JWM_DIR}"

    JWM_ENV_INIT_OK=0
  fi
fi

