#!/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`

# On macOS, shorten TMPDIR 
if [ "$(uname)" == "Darwin" ]; then
    export TMPDIR=/tmp
fi
export MCSTAS=$PREFIX/mcstas/2.5
export MCSTAS_TOOLS=$PREFIX/mcstas/2.5/tools/Perl/
export PATH=$PREFIX/mcstas/2.5/miniconda3/bin:$PREFIX/mcstas/2.5/bin:$PATH
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-2.5 env \W \$ '
if [ -x "${SHELL}" ]; then
  ${SHELL}
else
  /bin/sh
fi

