#!/bin/sh

# ccp4-python can be used before the setup is sourced (e.g. during build)
CCP4="$(cd "$(dirname "$0")/.." && pwd)"

# Old PYTHONPATH is ignored -- it may be set for different Python version.
# Special case when Windows Python is called from MSYS/MinGW shell is handled
# near to "python.exe" below (";" must be used as a separator in that case).
export PYTHONPATH="$CCP4/lib/python2.7/site-packages:$CCP4/lib/py2:$CCP4/lib/py2/libtbx/pythonpath:$CCP4/share/python"

# LIBTBX_BUILD is used in cctbx to find libtbx_env file (pickle)
export LIBTBX_BUILD=$CCP4/lib/py2

if test -e $CCP4/libexec/python2.7; then
    exec $CCP4/libexec/python2.7 "$@"   # location in dev. build
elif test -d $CCP4/Frameworks/Python.framework; then
    export DYLD_LIBRARY_PATH=$CCP4/lib:$DYLD_LIBRARY_PATH
    exec $CCP4/Frameworks/Python.framework/Versions/2.7/bin/python2.7 "$@"
elif test -e $CCP4/../python2.7/python.exe; then
    PYTHONPATH=${PYTHONPATH//:/;} # works in bash/zsh/ksh - enough for Windows
    exec $CCP4/../python2.7/python.exe "$@"   # on Windows python is not in bin/
else
    # $CCP4/libexec/python has RPATH set to $CCP4/lib. LD_LIBRARY_PATH is
    # needed only for system Python, because extension modules on its own
    # don't have rpaths pointing to $CCP4/lib (yet)
    export LD_LIBRARY_PATH=$CCP4/lib:$LD_LIBRARY_PATH
    exec python2.7 "$@"   # any python 2.7 should also be fine
fi
