#! /bin/sh
## This Bourne-shell script is used to initialize and run Ferret using 
## the pyferret Python module.  The intent of this script is to provide 
## a traditional Ferret interface through the pyferret Python module.

## set python_exe to the (optionally full-path) python executable to use
python_exe="##PYTHON_EXECUTABLE##"

## Make sure the FER_* environment variables are assigned
if [ -z "${FER_DIR}" ]; then
## Either source the ferret_paths script to assign the environment variables
    . "##FERRET_PATHS_SH_SCRIPT##"
## or just throw an error if they should have already been defined
#   echo "**ERROR: Ferret environment variables are not defined"
#   exit 1
##
fi


##
## ==== The following should not need any modifications ====
##


## The pyferret assignment to PYTHONPATH and LD_LIBRARY_PATH 
## is now done in the ferret_paths script.

## Add ${FER_DIR}/dylibs to DYLD_FALLBACK_LIBRARY_PATH (for Mac OS X)
## This must be done in this script; it is not seen if done in the ferret_paths scripts.
if [ ! -z "$DYLD_FALLBACK_LIBRARY_PATH" ]; then
    if ! echo "${DYLD_FALLBACK_LIBRARY_PATH}" | grep -q "${FER_DIR}/dylibs"; then
        export DYLD_FALLBACK_LIBRARY_PATH="${FER_DIR}/dylibs:${DYLD_FALLBACK_LIBRARY_PATH}"
    fi
else
    export DYLD_FALLBACK_LIBRARY_PATH="${FER_DIR}/dylibs"
fi

## Execute an in-line Python script to run Ferret using the pyferret 
## module.  The init method explicity processes the $PYTHONSTARTUP file, 
## if it exists and if '-secure' was not given as a command-line argument.
if echo "$@" | grep -q -- "-linebuffer"; then
    GFORTRAN_UNBUFFERED_PRECONNECTED=1
    export GFORTRAN_UNBUFFERED_PRECONNECTED
    python_flags="-u -i"
else
    python_flags="-i"
fi
exec ${python_exe} ${python_flags} -c "import sys; import pyferret; (errval, errmsg) = pyferret.init(sys.argv[1:], True)" "$@"

