#!/bin/bash
#set -x

# force a friendly umask
umask 002

# Runs on Linux
#################################
## GMVAULT Distribution home directory
#################################
HERE=$(unset CDPATH; cd `dirname $0`; pwd)
GMVAULT_HOME=$(unset CDPATH; cd $HERE/..; pwd)

#################################
## setup PYTHON_BIN
#################################
#look as if it was the packaged binary distribution
if [ -f "$GMVAULT_HOME/lib/python-lib/bin/python" ]; then
    PYTHON_BIN=$GMVAULT_HOME/lib/python-lib/bin/python
    export PYTHONPATH=$GMVAULT_HOME/lib:$PYTHONPATH
    #set pythonhome but it should not be necessary
    export PYTHONHOME=$GMVAULT_HOME/lib/python-lib

elif [ -f "$GMVAULT_HOME/bin/python" ]; then
    #installed from src distribution (python bin is in the same dir as gmvault script)
    PYTHON_BIN=$GMVAULT_HOME/bin/python

else
    PYTHON_BIN=`which python2.7 2>/dev/null`
    echo "Odd. Use default python2.7.x distribution"

fi

if [ ! $PYTHON_BIN ] || [ ! -f $PYTHON_BIN ]; then
    echo "Error: Cannot find the python executable to set env var PYTHON_BIN."
    echo "Please check where your python binary is."
    exit 1
fi

###################################
## create bootstrap and launch program
## the bootstrap is always under
## /tmp/rnpicker.bootstrap
###################################


# keep args in variables
ARGS="$@"

cat > /tmp/gmvault.bootstrap.$$ << EOF
import gmv.gmv_cmd as runner
runner.bootstrap_run()
EOF

#call gmvault bootstrap
$PYTHON_BIN /tmp/gmvault.bootstrap.$$ "$@"
res="$?"

rm -f /tmp/gmvault.bootstrap.$$

exit $res
