#!/usr/bin/env bash

PREFIX="$0"

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

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

PREFIX=`dirname $PREFIX`
PREFIX=`dirname $PREFIX`
PREFIX=`dirname $PREFIX`

ARCH=`arch`
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=$PREFIX/mcstas/3.3/miniconda3/bin:$PATH

export MCSTAS=$PREFIX/mcstas/3.3
export MCSTAS_TOOLS=$PREFIX/mcstas/3.3/tools/Perl/
export PATH=$PREFIX/mcstas/3.3/bin:$PATH
# Check if we are running with miniconda-provided python libs and set PYTHONPATH accordingly
if [ -d "$MCSTAS/miniconda3" ]; then
    source $MCSTAS/miniconda3/bin/activate base
fi
echo
echo The new shell started here is now set up for running this version of mcstas:
echo
mcstas -v
echo
echo To end using this version of mcstas, exit this shell.
echo
export PS1='mcstas-3.3 env \W \$ '
if [ -x "${SHELL}" ]; then
  ${SHELL}
else
  /bin/sh
fi

