#!/bin/sh

# Set up a Qiime working environment.  This should either run the Qiime command directly
# or else drop the user to a shell where Qiime is ready to run.
# For now, if $SHELL is zsh use that, otherwise run bash.

if [ "$*" = "" ] ; then
    #Run a shell
    if [ `basename "$SHELL"` = "zsh" ] ; then
	export ZOLDDOTDIR=${ZSHDOTDIR:-~}
	export ZDOTDIR=/usr/lib/qiime/shell
	QIIMESHELL="$SHELL"
	QSNAME="ZSH ($SHELL)"
    elif [ `basename "$SHELL"` = "bash" ] ; then
	#Set bash dot directory.
    	QIIMESHELL="$SHELL --rcfile /usr/lib/qiime/shell/.bashrc"
	QSNAME="BASH ($SHELL)"
    else
	if which bash > /dev/null ; then
	    QIIMESHELL="bash --rcfile /usr/lib/qiime/shell/.bashrc"
	    QSNAME="BASH"
	else
	    #This should never happen due to package dependencies.
	    echo "To start an interactive QIIME shell, you need to have 'bash' available and"
	    echo "in your path."
	    exit 1
	fi
    fi

#Determine Qiime version:
QIIME_VERSION=`/usr/lib/qiime/bin/print_qiime_config.py --version 2>/dev/null | awk '{print $NF}'`
QIIME_VERSION=${QIIME_VERSION:-UNKNOWN}

echo """
Setting up $QSNAME environment to run QIIME commands.

You have QIIME version $QIIME_VERSION.
Note that this will not produce identical results to the official QIIME release
as it uses updated versions of several tools.

Type 'help' for help, 'print_qiime_config_all' to see the current QIIME
settings, or 'exit' to return to regular shell.
"""

    eval exec $QIIMESHELL

elif [ "$1" = help ] ; then
    echo "Type 'man qiime' for help."
else
    #Just run the command

    # Set environment
    . /usr/lib/qiime/shell/qiime_environment

    cmd=/usr/lib/qiime/bin/`basename "$1" .py`
    shift

    [ -e "$cmd" ] || cmd="${cmd}.py"
    exec "$cmd" "$@"
fi
