#! /bin/sh

OLDLANG="$LANG"
LANG=C

# Python program to find a real file across symbolic links
nosymlink='
import sys,os
p=sys.executable # sys.argv[1]
while os.path.islink(p):
    x = os.readlink(p)
    if os.path.isabs(x):
        p = x
    else:
        p=os.path.join(os.path.dirname(p),x)
if not p: p=os.path.join(".", os.path.basename(p))
print os.path.abspath(p)'

# Find Python executable in $python

pythonexelist='python Python python2 Python2'
unset python
for pythonexe in $pythonexelist; do
    python=`which $pythonexe 2> /dev/null`
    if [ $? -eq 0 ]; then break; fi
done
[ -z "$python" ] && echo 'Cannot find Python executable' && exit 1

LANG="$OLDLANG"
unset OLDLANG

# Look for neuro.py
"$python" -m brainvisa.neuro "$@"
returnValue=$?

# After using IPython in brainvisa, console need to be reset otherwise
# characters typed by user are not displayed anymore.
#
# check that stty command exists
if ( [ ! "${TERM}" = "msys" ] && [ ! "${TERM}" = "cygwin" ] ); then
    if ( type stty > /dev/null 2>&1 ); then
        # If brainvisa is in background, do not launch stty otherwise
        # the process is stopped
        pgid=`ps -p $$ -o pgid=`
        tpgid=`ps -p $$ -o tpgid=`
        if [ "$pgid" = "$tpgid" ]; then
            # if stty command can be called then reset terminal
            stty -a > /dev/null 2>&1 && stty sane
        fi
    fi
fi

exit $returnValue

