#!/usr/bin/env bash

if [[ 1 == 1 ]]; then
  # Legacy McCode PATH setup 
  TOPENV="$0"

  # Check if we are being called with a non-full path
  if [[ $TOPENV != "/"* ]]; then
    TOPENV="$PWD/$TOPENV"
  fi

  # Iterate down a (possible) chain of symlinks (macOS does not have readlink -f)
  while [ -L "$TOPENV" ];
  do
    TOPENV=`readlink "$TOPENV"`
  done
  TOPENV=`dirname $TOPENV`
  TOPENV=`dirname $TOPENV`
  TOPENV=`dirname $TOPENV`

else
  TOPENV="\$( cd -P \"\$( dirname \"\${BASH_SOURCE[0]}\" )\" && pwd )"
fi



ARCH=`uname -m`
UNAME=`uname -s`

# On macOS, shorten TMPDIR 
if [[ ${UNAME} = Darwin* ]]; then
    export TMPDIR=/tmp
    OSXVER=`sw_vers -productVersion|cut -f 1 -d.`
fi

export PATH=$TOPENV/miniconda3/bin:$PATH

export MCXTRACE=$TOPENV/share/mcxtrace/resources

export PATH=$TOPENV/bin:$PATH
# Check if we are running with miniconda-provided python libs and set PYTHONPATH accordingly
if [ -d "$MCXTRACE/miniconda3" ]; then
    #Activation hook for conda 
    _conda_bindir="$MCXTRACE/miniconda3/bin"
    function conda() {
	unset conda
	eval "$(${_conda_bindir}/conda shell.bash hook 2> /dev/null)"
	conda "$@"
    }
    conda activate $MCXTRACE/miniconda3
    export PS1='(mcxtrace-3.9999-git/miniconda3) \W \$ '
else
    export PS1='mcxtrace-3.9999-git env \W \$ '
fi

 
if [ -d "$MCXTRACE/share/NCrystal/python/" ]; then
    export PYTHONPATH=$MCXTRACE/share/NCrystal/python/:$PYTHONPATH
fi

echo
echo The new shell started here is now set up for running this version of mcxtrace:
echo
mcxtrace -v
echo
echo To end using this version of mcxtrace, exit this shell.
echo
if ! [ "${CONDA_PREFIX}x" == "x" ]; then
    echo \( Using CONDA env placed in 
    echo    ${CONDA_PREFIX} \)
    echo
fi

if [ -x ${SHELL} ]; then
  ${SHELL}
else
  /bin/sh
fi

