#!/usr/bin/env bash
#
# Summary: Activate virtual environment
#
# Usage: pyenv activate <virtualenv>
#        pyenv activate --unset
#
# Activate a Python virtualenv environment in current shell.
# This acts almost as same as `pyenv shell`, but this invokes the `activate`
# script in your shell.
#
# <virtualenv> should be a string matching a Python version known to pyenv.

set -e
[ -n "$PYENV_DEBUG" ] && set -x

unset NOERROR
unset VERBOSE

while [ $# -gt 0 ]; do
  case "$1" in
  "--complete" )
    # Provide pyenv completions
    echo --unset
    exec pyenv-virtualenvs --bare
    ;;
  "--no-error" )
    NOERROR=1
    ;;
  "--unset" )
    echo "pyenv deactivate"
    exit
    ;;
  "--verbose" )
    VERBOSE=1
    ;;
  * )
    break
    ;;
  esac
  shift 1
done

no_shell=
versions=("$@")
if [ -z "${versions}" ]; then
  no_shell=1
  OLDIFS="$IFS"
  IFS=: versions=($(pyenv-version-name))
  IFS="$OLDIFS"
fi

if [ -z "${PYENV_VIRTUALENV_INIT}" ]; then
  # Backward compatibility issue
  # https://github.com/yyuu/pyenv-virtualenv/issues/26
  no_shell=
fi

if [ "${#versions[@]}" -gt 1 ]; then
  [ -n "$NOERROR" ] || echo "pyenv-virtualenv: cannot activate multiple versions at once: ${versions[@]}" 1>&2
  echo "false"
  exit 1
fi

if pyenv-prefix "${versions}" 1>/dev/null 2>&1; then
  if ! pyenv-virtualenv-prefix "${versions}" 1>/dev/null 2>&1; then
    [ -n "$NOERROR" ] || echo "pyenv-virtualenv: version \`${versions}' is not a virtualenv" 1>&2
    echo "false"
    exit 1
  fi
else
  version="$(pyenv-version-name)"
  if [[ "${versions}" == "root" ]]; then
    versions="${version%/envs/*}"
  else
    versions="${version%/envs/*}/envs/${versions}"
  fi
  if ! pyenv-virtualenv-prefix "${versions}" 1>/dev/null 2>&1; then
    [ -n "$NOERROR" ] || echo "pyenv-virtualenv: version \`${versions}' is not an environment of anaconda/miniconda" 1>&2
    echo "false"
    exit 1
  fi
fi

shell="${PYENV_SHELL:-${SHELL##*/}}"
prefix="$(pyenv-prefix "${versions}")"

unset conda_env

if [ -f "${prefix}/bin/conda" ]; then
  if [[ "$shell" != "bash" ]] && [[ "$shell" != "zsh" ]]; then
    [ -n "$NOERROR" ] || echo "pyenv-virtualenv: Only bash and zsh are supported by Anaconda/Miniconda" 1>&2
    echo "false"
    exit 1
  fi
  if [[ "${prefix}" != "${prefix%/envs/*}" ]]; then
    conda_env="${prefix##*/envs/}"
  else
    conda_env="root"
  fi
fi

# Display setup instruction, if pyenv-virtualenv has not been initialized.
# if 'pyenv virtualenv-init -' is not found in "$profile"
if [ -z "$PYENV_VIRTUALENV_INIT" ]; then
  pyenv-virtualenv-init >&2 || true
fi

if [ -n "$VERBOSE" ]; then
  echo "pyenv-virtualenv: activate ${versions}" 1>&2
fi

if [ -z "$no_shell" ]; then
  echo "pyenv shell \"${versions}\";"
  # shell version set in pyenv-sh-activate should be unset
  # https://github.com/yyuu/pyenv-virtualenv/issues/61
  case "$shell" in
  fish )
    echo "setenv PYENV_ACTIVATE_SHELL 1;"
    ;;
  * )
    echo "export PYENV_ACTIVATE_SHELL=1;"
    ;;
  esac
fi

if [ -n "${conda_env}" ]; then
  # anaconda/miniconda
  cat <<EOS
unset PYENV_DEACTIVATE;
export PYENV_ACTIVATE="${prefix}";
. "${prefix%/envs/*}/bin/activate" "${conda_env}";
EOS
else
  # virtualenv/pyvenv
  case "$shell" in
  fish )
    cat <<EOS
set -e PYENV_DEACTIVATE;
setenv PYENV_ACTIVATE "${prefix}";
. "${prefix}/bin/activate.fish";
EOS
   ;;
  *    )
    cat <<EOS
unset PYENV_DEACTIVATE;
export PYENV_ACTIVATE="${prefix}";
. "${prefix}/bin/activate";
EOS
    ;;
  esac
fi
