#!/usr/bin/env bash
# Summary: Configure the shell environment for pyenv-virtualenv
# Usage: eval "$(pyenv virtualenv-init - [<shell>])"
#
# Automatically activates a Python virtualenv environment based on current
# pyenv version.
#

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

print=""
for args in "$@"
do
  if [ "$args" = "-" ]; then
    print=1
    shift
  fi
done

shell="${1:-$PYENV_SHELL}"
if [ -z "$shell" ]; then
  shell="$(ps c -p "$PPID" -o 'ucomm=' 2>/dev/null || true)"
  shell="${shell##-}"
  shell="${shell%% *}"
  shell="$(basename "${shell:-$SHELL}")"
fi

if [ -z "$print" ]; then
  case "$shell" in
  bash )
    profile='~/.bash_profile'
    ;;
  zsh )
    profile='~/.zshrc'
    ;;
  ksh )
    profile='~/.profile'
    ;;
  fish )
    profile='~/.config/fish/config.fish'
    ;;
  * )
    profile='your profile'
    ;;
  esac

  { echo "# Load pyenv-virtualenv automatically by adding"
    echo "# the following to ${profile}:"
    echo
    case "$shell" in
    fish )
      echo 'status --is-interactive; and . (pyenv virtualenv-init -|psub)'
      ;;
    * )
      echo 'eval "$(pyenv virtualenv-init -)"'
      ;;
    esac
    echo
  } >&2

  exit 1
fi

case "$shell" in
fish )
  echo "setenv PYENV_VIRTUALENV_INIT 1;"
  ;;
* )
  echo "export PYENV_VIRTUALENV_INIT=1;"
  ;;
esac

case "$shell" in
fish )
  cat <<EOS
function _pyenv_virtualenv_hook --on-event fish_prompt;
  set -l PYENV_PREFIX (pyenv prefix 2>/dev/null; or true)
  if [ -n "\$PYENV_ACTIVATE" ]
    if [ "(pyenv version-name 2>/dev/null; or true)" = "system" ]
      pyenv deactivate --no-error --verbose
      set -e PYENV_DEACTIVATE
      return 0
    end
    if [ "\$PYENV_ACTIVATE" != "\$PYENV_PREFIX" ]
      if pyenv deactivate --no-error --verbose
        set -e PYENV_DEACTIVATE
        pyenv activate --no-error --verbose; or set -e PYENV_DEACTIVATE
      else
        pyenv activate --no-error --verbose
      end
    end
  else
    if [ -z "\$VIRTUAL_ENV" ]; and [ "\$PYENV_DEACTIVATE" != "\$PYENV_PREFIX" ]
      pyenv activate --no-error --verbose; or true
    end
  end
end
EOS
 ;;
ksh )
  cat <<EOS
function _pyenv_virtualenv_hook() {
EOS
  ;;
* )
  cat <<EOS
_pyenv_virtualenv_hook() {
EOS
  ;;
esac

if [[ "$shell" != "fish" ]]; then
  cat <<EOS
  if [ -n "\$PYENV_ACTIVATE" ]; then
    if [ "\$(pyenv version-name 2>/dev/null || true)" = "system" ]; then
      pyenv deactivate --no-error --verbose
      unset PYENV_DEACTIVATE
      return 0
    fi
    if [ "\$PYENV_ACTIVATE" != "\$(pyenv prefix 2>/dev/null || true)" ]; then
      if pyenv deactivate --no-error --verbose; then
        unset PYENV_DEACTIVATE
        pyenv activate --no-error --verbose || unset PYENV_DEACTIVATE
      else
        pyenv activate --no-error --verbose
      fi
    fi
  else
    if [ -z "\$VIRTUAL_ENV" ] && [ "\$PYENV_DEACTIVATE" != "\$(pyenv prefix 2>/dev/null || true)" ]; then
      pyenv activate --no-error --verbose || true
    fi
  fi
};
EOS

  case "$shell" in
  bash )
    cat <<EOS
if ! [[ "\$PROMPT_COMMAND" =~ _pyenv_virtualenv_hook ]]; then
  PROMPT_COMMAND="_pyenv_virtualenv_hook;\$PROMPT_COMMAND";
fi
EOS
    ;;
  zsh )
    cat <<EOS
typeset -g -a precmd_functions
if [[ -z \$precmd_functions[(r)_pyenv_virtualenv_hook] ]]; then
  precmd_functions+=_pyenv_virtualenv_hook;
fi
EOS
    ;;
  * )
    # FIXME: what should i do here??
    ;;
  esac
fi
