# Based on conventions from https://github.com/github/scripts-to-rule-them-all
# script/bootstrap: Resolve all dependencies that the application requires to
# run.

# If not in CI, we have to install the correct python versions
if [[ -z $CI ]]; then
    # pyenv update
    pyenv install --skip-existing || return
fi

POETRY_HOME=`pwd`/.poetry
POETRY_BIN="$POETRY_HOME/bin/poetry"
if [[ ! -z $CI ]]; then
    POETRY_BIN=poetry
fi

if [[ -z $CI ]]; then
    # FIXME: There must be a better way to install this per project rather than globally?
    curl -sSL https://install.python-poetry.org | POETRY_HOME="$POETRY_HOME" python3 - --version $(cat .poetry-version)
    $POETRY_BIN env use $(cat .python-version)
fi

if [[ -z $SKIP_DEPS ]]; then
    $POETRY_BIN install --with=dev --with=test --no-interaction
fi

if [[ -z $CI ]]; then
    . ./.venv/bin/activate
    if [[ ! "$PATH" =~ "$POETRY_HOME" ]]; then
        export PATH="$POETRY_HOME/bin:$PATH"
    fi
fi

if [[ -z $CI ]]; then
    python -m python_githooks

    if ! complete -p invoke &>/dev/null; then
        source <(inv --print-completion-script bash)
    fi
fi
