#!/usr/bin/env bash
set -e
[ -n "$PYENV_DEBUG" ] && set -x

print_set_version() {
  echo "<version> should be a string matching a Python version known by pyenv."

  local versions="$(pyenv-versions --bare)"
  if [ -z "$versions" ]; then
    echo "There are currently no Python versions installed for pyenv."
  else
    echo "The currently installed Python versions are:"
    echo "$versions" | sed 's/^/  /'
  fi

  echo
  echo "The special version string 'system' will use your default system Python"
}

case "$1" in
"") echo "usage: pyenv <command> [<args>]

Some useful pyenv commands are:
   commands      List all pyenv commands
   rehash        Rehash pyenv shims (run this after installing binaries)
   global        Set or show the global Python version
   local         Set or show the local directory-specific Python version
   shell         Set or show the shell-specific Python version
   version       Show the current Python version
   versions      List all Python versions known by pyenv
   which         Show the full path for the given Python command
   whence        List all Python versions with the given command

See 'pyenv help <command>' for information on a specific command.
For full documentation, see: https://github.com/sstephenson/pyenv#readme"
;;
commands) echo "usage: pyenv commands
       pyenv commands --sh
       pyenv commands --no-sh

List all pyenv commands."
;;
global) echo "usage: pyenv global <version>

Sets the global Python version. You can override the global version at
any time by setting a directory-specific version with \`pyenv local'
or by setting the PYENV_VERSIONS environment variable.

$(print_set_version)"
;;
local) echo "usage: pyenv local <version>
       pyenv local --unset

Sets the local directory-specific Python version by writing the version
name to a file named '.pyenv-version'.

When you run a Python command, pyenv will look for an '.pyenv-version'
file in the current directory and each parent directory. If no such
file is found in the tree, pyenv will use the global Python version
specified with \`pyenv global', or the version specified in the
PYENV_VERSIONS environment variable.

$(print_set_version)"
;;
shell) echo "usage: pyenv shell <version>
       pyenv shell --unset

Sets a shell-specific Python version by setting the 'PYENV_VERSIONS'
environment variable in your shell. This version overrides both
project-specific versions and the global version.

$(print_set_version)"
;;
versions) echo "usage: pyenv versions
       pyenv versions --bare

Lists all Python versions known by pyenv."
;;
which) echo "usage: pyenv which <command>

Displays the full path to the binary that pyenv will execute when you
run the given command."
;;
whence) echo "usage: pyenv whence <command>

Lists all Python versions with the given command installed."
;;
*)
  command_path="$(command -v "pyenv-$1" || true)"
  if [ -n "$command_path" ]; then
    echo "Sorry, the \`$1' command isn't documented yet."
    echo
    echo "You can view the command's source here:"
    echo "$command_path"
    echo
  else
    echo "pyenv: no such command \`$1'"
  fi
esac
