#!/usr/bin/env bash
#
# Summary: Create a Python virtualenv using the pyenv-virtualenv plugin
#
# Usage: pyenv virtualenv [-f|--force] [-u|--upgrade] [VIRTUALENV_OPTIONS] <version> <virtualenv-name>
#        pyenv virtualenv --version
#        pyenv virtualenv --help
#
#   -u/--upgrade     Upgrade existing virtualenv to use new version of Python,
#                    assuming Python has been upgraded in-place.
#   -f/--force       Install even if the version appears to be installed already
#

PYENV_VIRTUALENV_VERSION="20140123"

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

# Provide pyenv completions
if [ "$1" = "--complete" ]; then
  exec pyenv-versions --bare
fi

if [ -z "$PYENV_ROOT" ]; then
  PYENV_ROOT="${HOME}/.pyenv"
fi

unset PIP_REQUIRE_VENV
unset PIP_REQUIRE_VIRTUALENV

# Define library functions
parse_options() {
  OPTIONS=()
  ARGUMENTS=()
  local arg option index

  for arg in "$@"; do
    if [ "${arg:0:1}" = "-" ]; then
      if [ "${arg:1:1}" = "-" ]; then
        OPTIONS[${#OPTIONS[*]}]="${arg:2}"
      else
        index=1
        while option="${arg:$index:1}"; do
          [ -n "$option" ] || break
          OPTIONS[${#OPTIONS[*]}]="$option"
          index=$(($index+1))
        done
      fi
    else
      ARGUMENTS[${#ARGUMENTS[*]}]="$arg"
    fi
  done
}

resolve_link() {
  $(type -p greadlink readlink | head -1) "$1"
}

abs_dirname() {
  local cwd="$(pwd)"
  local path="$1"

  while [ -n "$path" ]; do
    cd "${path%/*}"
    local name="${path##*/}"
    path="$(resolve_link "$name" || true)"
  done

  pwd
  cd "$cwd"
}

http() {
  local method="$1"
  local url="$2"
  local file="$3"
  [ -n "$url" ] || return 1

  if type curl &>/dev/null; then
    "http_${method}_curl" "$url" "$file"
  elif type wget &>/dev/null; then
    "http_${method}_wget" "$url" "$file"
  else
    echo "error: please install \`curl\` or \`wget\` and try again" >&2
    exit 1
  fi
}

http_head_curl() {
  curl -qsILf "$1" >&4 2>&1
}

http_get_curl() {
  curl -C - -o "${2:--}" -qsSLf "$1"
}

http_head_wget() {
  wget -q --spider "$1" >&4 2>&1
}

http_get_wget() {
  wget -nv -c -O "${2:--}" "$1"
}

version() {
  detect_venv
  local version
  if [ -n "$USE_PYVENV" ]; then
    version="$(pyenv-which pyvenv 2>/dev/null || true)"
    version="${version#$(pyenv-root)/versions/}"
    version="${version%/bin/pyvenv}"
    echo "pyenv-virtualenv ${PYENV_VIRTUALENV_VERSION} (pyvenv ${version:-unknown})"
  else
    version="$(venv --version 2>/dev/null || true)"
    echo "pyenv-virtualenv ${PYENV_VIRTUALENV_VERSION} (virtualenv ${version:-unknown})"
  fi
}

usage() {
  # We can remove the sed fallback once pyenv 0.2.0 is widely available.
  pyenv-help virtualenv 2>/dev/null || sed -ne '/^#/!q;s/.//;s/.//;1,4d;p' < "$0"
  venv --help 2>/dev/null || true
  [ -z "$1" ] || exit "$1"
}

detect_venv() {
  if pyenv-which "virtualenv" 1>/dev/null 2>&1; then
    HAS_VIRTUALENV=1
  fi
  if pyenv-which "pyvenv" 1>/dev/null 2>&1; then
    HAS_PYVENV=1
  fi
  # Use pyvenv only if virtualenv is not installed and there is pyvenv
  if [ -n "${HAS_PYVENV}" ] && [ -z "${HAS_VIRTUALENV}" ]; then
    USE_PYVENV=1
  fi
}

venv() {
  local args=("$@")
  if [ -n "${USE_PYVENV}" ]; then
    pyenv-exec pyvenv "${args[@]}"

    local last="${args[${#args[@]}-1]}"
    ensurepip "${last##*/}"
  else
    pyenv-exec virtualenv "${args[@]}"
  fi
}

install_setuptools() {
  local version="$1"
  { if [ "${EZ_SETUP+defined}" ] && [ -f "${EZ_SETUP}" ]; then
      if [ -n "$VERBOSE" ]; then
        echo "Installing setuptools from ${EZ_SETUP}..." 1>&2
      fi
      cat "${EZ_SETUP}"
    else
      [ -n "${EZ_SETUP_URL}" ] || {
        if [ -n "${SETUPTOOLS_VERSION}" ]; then
          EZ_SETUP_URL="https://bitbucket.org/pypa/setuptools/raw/${SETUPTOOLS_VERSION}/ez_setup.py"
        else
          EZ_SETUP_URL="https://bitbucket.org/pypa/setuptools/downloads/ez_setup.py"
        fi
      }
      if [ -n "$VERBOSE" ]; then
        echo "Installing setuptools from ${EZ_SETUP_URL}..." 1>&2
      fi
      http get "${EZ_SETUP_URL}"
    fi
  } | PYENV_VERSION="${version}" pyenv-exec python
}

install_pip() {
  local version="$1"
  { if [ "${GET_PIP+defined}" ] && [ -f "${GET_PIP}" ]; then
      if [ -n "$VERBOSE" ]; then
        echo "Installing pip from ${GET_PIP}..." 1>&2
      fi
      cat "${GET_PIP}"
    else
      [ -n "${GET_PIP_URL}" ] || {
        if [ -n "${PIP_VERSION}" ]; then
          GET_PIP_URL="https://raw.github.com/pypa/pip/${PIP_VERSION}/contrib/get-pip.py"
        else
          GET_PIP_URL="https://raw.github.com/pypa/pip/master/contrib/get-pip.py"
        fi
      }
      if [ -n "$VERBOSE" ]; then
        echo "Installing pip from ${GET_PIP_URL}..." 1>&2
      fi
      http get "${GET_PIP_URL}"
    fi
  } | PYENV_VERSION="${version}" pyenv-exec python
}

install_virtualenv() {
  local version="$1"
  VIRTUALENV_VERSION="==${VIRTUALENV_VERSION}"
  PYENV_VERSION="${version}" pyenv-exec pip install $QUIET $VERBOSE "virtualenv${VIRTUALENV_VERSION%==}"
}

ensurepip() {
  local version="$1"
  if PYENV_VERSION="${version}" pyenv-exec python -m ensurepip 2>/dev/null; then
    local PREFIX_PATH="$(pyenv-prefix "${version}")"
    if [ ! -e "${PREFIX_PATH}/bin/pip" ]; then
      local pip="$(PYENV_VERSION="${version}" pyenv-exec python -c 'import sys;v=sys.version_info;sys.stdout.write("pip%d.%d"%(v[0],v[1]))')"
      if [ -e "${PREFIX_PATH}/bin/${pip}" ]; then
        ( cd "${PREFIX_PATH}/bin" && ln -fs "${pip}" "pip" )
      fi
    fi
  else
    PYENV_VERSION="${version}" pyenv-exec python -c "import setuptools" 1>/dev/null 2>&1 || {
      install_setuptools "${version}"
    } 1>&2
    PYENV_VERSION="${version}" pyenv-which pip 1>/dev/null 2>&1 || {
      install_pip "${version}"
    } 1>&2
  fi
}

prepare_upgrade() {
  local version="$1"
  local prefix="$2"
  PYENV_VERSION="${version}" pyenv-exec pip freeze >"${UPGRADE_LIST}"
  mv -f "${prefix}" "${UPGRADE_PATH}"
}

upgrade() {
  local version="$1"
  local prefix="$2"
  ## Migrate previously installed packages from requirements.txt
  if ! PYENV_VERSION="${version}" pyenv-exec pip install $QUIET $VERBOSE --requirement "${UPGRADE_LIST}"; then
    { echo
      echo "UPGRADE FAILED"
      echo
      echo "Inspect or clean up the original tree at ${UPGRADE_PATH}"
      echo
      echo "Package list:"
      cat "${UPGRADE_LIST}"
    } 1>&2
    return 1
  fi
  rm -f "${UPGRADE_LIST}"
  rm -fr "${UPGRADE_PATH}"
}

PYENV_VIRTUALENV_ROOT="$(abs_dirname "$0")/.."
if [ -z "${PYENV_VIRTUALENV_CACHE_PATH}" ]; then
  PYENV_VIRTUALENV_CACHE_PATH="${PYTHON_BUILD_CACHE_PATH:-${PYENV_ROOT}/cache}"
fi
VIRTUALENV_OPTIONS=()

unset FORCE
unset QUIET
unset UPGRADE
unset VERBOSE

parse_options "$@"
for option in "${OPTIONS[@]}"; do
  case "$option" in
  "f" | "force" )
    FORCE=true
    ;;
  "h" | "help" )
    usage 0
    ;;
  "p" )
    VIRTUALENV_OPTIONS[${#VIRTUALENV_OPTIONS[*]}]="--python=${ARGUMENTS[0]}"
    ARGUMENTS=("${ARGUMENTS[@]:1}") # shift 1
    ;;
  "q" | "quiet" )
    QUIET="--quiet"
    ;;
  "u" | "upgrade" )
    UPGRADE=true
    ;;
  "v" | "verbose" )
    VERBOSE="--verbose"
    ;;
  "version" )
    version
    exit 0
    ;;
  * ) # virtualenv long options
    VIRTUALENV_OPTIONS[${#VIRTUALENV_OPTIONS[*]}]="--$option"
    ;;
  esac
done

if [[ "${#ARGUMENTS[@]}" == 0 ]]; then
  echo "pyenv-virtualenv: no virtualenv name given." 1>&2
  exit 1
elif [[ "${#ARGUMENTS[@]}" == 1 ]]; then
  # If only one argument given, use current version as source version
  VERSION_NAME="$(pyenv-version-name)"
  VIRTUALENV_NAME="${ARGUMENTS[0]}"
else
  # Otherwise, use former as source version, and latter as virtualenv version
  VERSION_NAME="${ARGUMENTS[0]}"
  VIRTUALENV_NAME="${ARGUMENTS[1]##*/}"
fi

if [ -z "${VERSION_NAME}" ] || [ -z "${VIRTUALENV_NAME}" ]; then
  usage 1
fi

# Set VERSION_NAME as default version in this script
export PYENV_VERSION="${VERSION_NAME}"

# Source version must exists before creating virtualenv
pyenv-prefix 1>/dev/null 2>&1 || usage 1

if [ -z "$TMPDIR" ]; then
  TMP="/tmp"
else
  TMP="${TMPDIR%/}"
fi

SEED="$(date "+%Y%m%d%H%M%S").$$"
UPGRADE_PATH="${TMP}/pyenv-virtualenv.${SEED}"
UPGRADE_LIST="${TMP}/pyenv-virtualenv.${SEED}.txt"

VIRTUALENV_PATH="${PYENV_ROOT}/versions/${VIRTUALENV_NAME}"

unset HAS_VIRTUALENV
unset HAS_PYVENV
unset USE_PYVENV
detect_venv

if [ -n "${USE_PYVENV}" ]; then
  # Unset some arguments not supported by pyvenv
  unset QUIET
  unset VERBOSE
  if [ -n "$UPGRADE" ]; then
    unset UPGRADE
    VIRTUALENV_OPTIONS[${#VIRTUALENV_OPTIONS[*]}]="--upgrade"
  fi
else
  if [ -z "${HAS_VIRTUALENV}" ]; then
    install_virtualenv "${PYENV_VERSION}"
    HAS_VIRTUALENV=1
  fi
fi

# Unset environment variables which starts with `VIRTUALENV_`.
# These variables are reserved for virtualenv.
unset VIRTUALENV_VERSION


# Define `before_virtualenv` and `after_virtualenv` functions that allow
# plugin hooks to register a string of code for execution before or
# after the installation process.
declare -a before_hooks after_hooks

before_virtualenv() {
  local hook="$1"
  before_hooks["${#before_hooks[@]}"]="$hook"
}

after_virtualenv() {
  local hook="$1"
  after_hooks["${#after_hooks[@]}"]="$hook"
}

# Load plugin hooks.
OLDIFS="$IFS"
IFS=$'\n' scripts=(`pyenv-hooks virtualenv`)
IFS="$OLDIFS"
for script in "${scripts[@]}"; do source "$script"; done


[ -d "${VIRTUALENV_PATH}" ] && PREFIX_EXISTS=1

# If the virtualenv exists, prompt for confirmation unless
# the --force option was specified.
if [ -d "${VIRTUALENV_PATH}/bin" ]; then
  if [ -z "$FORCE" ]; then
    echo "pyenv-virtualenv: ${VIRTUALENV_PATH} already exists" 1>&2
    read -p "continue with installation? (y/N) "

    case "$REPLY" in
    y* | Y* ) ;;
    * ) exit 1 ;;
    esac
  fi

  if [ -n "$UPGRADE" ]; then
    prepare_upgrade "${VIRTUALENV_NAME}" "${VIRTUALENV_PATH}"
  fi
fi

# Execute `before_virtualenv` hooks.
for hook in "${before_hooks[@]}"; do eval "$hook"; done

# Plan cleanup on unsuccessful installation.
cleanup() {
  [ -z "${PREFIX_EXISTS}" ] && rm -rf "$VIRTUALENV_PATH"
}

trap cleanup SIGINT

# Invoke virtualenv and record exit status in $STATUS.
STATUS=0
# virtualenv may download distribute/setuptools in current directory.
# change to cache directory to reuse them between invocation.
mkdir -p "${PYENV_VIRTUALENV_CACHE_PATH}"
cd "${PYENV_VIRTUALENV_CACHE_PATH}"
venv $QUIET $VERBOSE "${VIRTUALENV_OPTIONS[@]}" "${VIRTUALENV_PATH}" || STATUS="$?"

## Migrate previously installed packages from requirements.txt
if [ -n "$UPGRADE" ]; then
  upgrade "${VIRTUALENV_NAME}" "${VIRTUALENV_PATH}" || STATUS="$?"
fi

# Execute `after_virtualenv` hooks
for hook in "${after_hooks[@]}"; do eval "$hook"; done

# Run `pyenv-rehash` after a successful installation.
if [ "$STATUS" == "0" ]; then
  pyenv-rehash
else
  cleanup
fi

exit "$STATUS"
