# /bin/bash - help gedit syntax highlight do the right thing
# autoenv script (https://github.com/kennethreitz/autoenv)
# Use ". .env -h" for help.
_venv_virtualenv=/usr/bin/virtualenv
_venv_temp=/usr/local/bin/virtualenv
if test ! -x $_venv_virtualenv -a -x $_venv_temp; then
    _venv_virtualenv=$_venv_temp
fi

_venv_readlink=readlink
case "$(uname -s)" in
    # See http://en.wikipedia.org/wiki/Uname#Examples
    Darwin)
        _venv_temp="/usr/local/opt/coreutils/libexec/gnubin/readlink"
        if test -x $_venv_temp; then
            _venv_readlink=$_venv_temp
        else
            echo "*** No readlink command, do a 'brew install coreutils'..."
            return 1
        fi
        ;;
    CYGWIN*)
        ;;
    Linux|*)
        ;;
esac

test -z "$JENKINS_URL" || echo '***' "\$0=$0" "\$BASH_SOURCE=${BASH_SOURCE[0]}"
if test "$0" = "-bash" -o "$(basename -- "$0")" = "bash" -o "$(basename -- "${BASH_SOURCE[0]}")" = ".env"; then
    _venv_script=$($_venv_readlink -f ${BASH_SOURCE[0]})
else
    _venv_script="/dont-know-really/.env"
fi
_venv_xtrace=$(set +o | grep xtrace)
set +x
_venv_name="$(basename $(pwd))"

_venv_pip_log() {
    if $_venv_verbose; then
        cat
    else
        egrep "Found|Collecting|Installing.collected|Searching.for|Installed|Finished"
    fi
}

# command line flags, mostly for CI server usage
_venv_create=false
_venv_develop=false
_venv_verbose=false
while test "${1:0:1}" = "-"; do
    case "$1" in
        --yes) _venv_create=true ;;
        --develop) _venv_develop=true ;;
        --verbose | -v) _venv_verbose=true ;;
        --virtualenv) shift; _venv_virtualenv="$1" ;;
        --help | -h)
            #     ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
            echo "usage: . $_venv_script <options>"
            echo
            echo "Create and/or activate a Python virtual environment. Updates pip and other"
            echo "system packages, and recognizes 'dev-requirements.txt' and 'setup.py' or"
            echo "'requirements.txt'."
            echo
            echo "This is an autoenv script (see https://github.com/kennethreitz/autoenv)."
            echo
            echo "Options:"
            echo "    --yes                 create missing venv without prompting"
            echo "    --develop             call 'develop -U' on activation"
            echo "    -v | --verbose        don't filter install logs on terminal"
            echo "                          (full logs in '.venv/pip-install.log')"
            echo "    -h | --help           this help message"
            echo "    --virtualenv PATH     set explicit path of virtualenv binary to use"
            return 1
            ;;
        *) echo "WARNING: Ignored unknown option '$1'" ;;
    esac
    shift
done

# Outside the tree of the .env script?
if pwd | egrep -v '^'$(dirname "$_venv_script")'($|/)' >/dev/null; then
    : echo Outside "[$0 $1 ; $_venv_script]"

# Inside the tree of the .env script, but have another local '.env'?
elif test \! -f .env -o "$_venv_script" != "$(pwd)/.env"; then
    : echo Inside "[$0 $1 ; $_venv_script]"

# Only try virtualenv creation outside of template dirs; the egrep pattern is escaped for hiding it from Jinja2
elif pwd | egrep -v '/{''{''.*''}''}(/|$)' >/dev/null || $_venv_create; then
    test -f ".env" && _venv_ask=true || _venv_ask=false

    # Look for existing venv at common locations
    for _venv_base in .venv venv . ..; do
        if test -f "$_venv_base/$_venv_name/bin/activate"; then
            deactivate 2>/dev/null || :
            . "$_venv_base/$_venv_name/bin/activate"
            if test -f setup.py; then
                $_venv_develop && python setup.py -q develop -U || :
                python setup.py --name --version --url | tr \\n \\0 \
                    | xargs -0 printf "*** Activated %s %s @ %s\\n" || :
            else
                echo "*** Activated $_venv_base/$_venv_name"
            fi
            _venv_ask=false
            break
        fi
    done

    if $_venv_ask && test \! -d .venv; then
        $_venv_create || { read -p "No virtualenv found, shall I create one for you? [Y/n] " -n 1 -r || REPLY='n'; echo; }
        if $_venv_create || [[ $REPLY =~ ^[Yy]?$ ]]; then
            # Create, activate, and update virtualenv
            $_venv_virtualenv ".venv/$_venv_name"
            . ".venv/$_venv_name/bin/activate"
            ".venv/$_venv_name/bin/pip" --log ".venv/pip-install.log" install -U pip 2>&1 | _venv_pip_log \
                || echo >&2 "!!! pip failed, see .venv/pip-install.log for details"
            ".venv/$_venv_name/bin/pip" --log ".venv/pip-install.log" install -U "setuptools>=14.3" "wheel>=0.24.0" 2>&1 | _venv_pip_log \
                || echo >&2 "!!! pip failed, see .venv/pip-install.log for details"

            # Get rid of cruft some older systems produce
            ".venv/$_venv_name/bin/pip" --log ".venv/pip-install.log" uninstall --yes distribute || :

            # pypandoc fails when the base package is missing, so we install it here, if possible
            if which pandoc >/dev/null ; then
                ".venv/$_venv_name/bin/pip" --log ".venv/pip-install.log" install pypandoc 2>&1 | _venv_pip_log \
                    || echo >&2 "!!! pip failed, see .venv/pip-install.log for details"
            fi

            # Install development + project dependencies
            if test -f dev-requirements.txt; then
                ".venv/$_venv_name/bin/pip" --log ".venv/pip-install.log" install -r dev-requirements.txt 2>&1 | _venv_pip_log \
                    || echo >&2 "!!! pip failed, see .venv/pip-install.log for details"
            fi
            if test -f setup.py; then
                ".venv/$_venv_name/bin/python" setup.py develop -U 2>&1 | _venv_pip_log
                echo
                ".venv/$_venv_name/bin/python" setup.py --name --version --author --author-email --license --description --url \
                    | tr \\n \\0 | xargs -0 printf "%s %s by %s <%s> [%s]\\n%s\\n%s\\n" || :
            else
                echo
                if test -f requirements.txt; then
                    echo "*** No 'setup.py' found, installing requirements..."
                    ".venv/$_venv_name/bin/pip" --log ".venv/pip-install.log" install -r requirements.txt 2>&1 | _venv_pip_log
                else
                    echo "*** No 'setup.py' or 'requirements.txt' found, all done."
                fi
            fi
        else
            # prevent constant prompting
            mkdir -p .venv
        fi
    fi
fi

unset _venv_script _venv_name _venv_ask _venv_create _venv_develop _venv_pip_log _venv_base
unset _venv_readlink _venv_temp _venv_virtualenv _venv_verbose
eval $_venv_xtrace # restore xtrace state
unset _venv_xtrace
