#!/bin/bash
#
# Summary: Install a Node version using node-build
#
# Usage: nodenv install [-f|-s] [-kpv] <version>
#        nodenv install [-f|-s] [-kpv] <definition-file>
#        nodenv install -l|--list
#        nodenv install --version
#
#   -l/--list          List all available versions
#   -f/--force         Install even if the version appears to be installed already
#   -s/--skip-existing Skip if the version appears to be installed already
#
#   node-build options:
#
#   -c/--compile       Force compilation even if a matching binary exists
#   -k/--keep          Keep source tree in $NODENV_BUILD_ROOT after installation
#                      (defaults to $NODENV_ROOT/sources)
#   -p/--patch         Apply a patch from stdin before building
#   -v/--verbose       Verbose mode: print compilation status to stdout
#   --version          Show version of node-build
#
# For detailed information on installing Node versions with
# node-build, including a list of environment variables for adjusting
# compilation, see: https://github.com/nodenv/node-build#usage
#
set -e
[ -n "$NODENV_DEBUG" ] && set -x

# Add `share/node-build/` directory from each nodenv plugin to the list of
# paths where build definitions are looked up.
shopt -s nullglob
for plugin_path in "$NODENV_ROOT"/plugins/*/share/node-build; do
  NODE_BUILD_DEFINITIONS="${NODE_BUILD_DEFINITIONS}:${plugin_path}"
done
export NODE_BUILD_DEFINITIONS
shopt -u nullglob

# Provide nodenv completions
if [ "$1" = "--complete" ]; then
  echo --list
  echo --force
  echo --skip-existing
  echo --compile
  echo --keep
  echo --patch
  echo --verbose
  echo --version
  exec node-build --definitions
fi

# Load shared library functions
eval "$(node-build --lib)"

usage() {
  nodenv-help install 2>/dev/null
  [ -z "$1" ] || exit "$1"
}

definitions() {
  local query="$1"
  node-build --definitions | $(type -p ggrep grep | head -1) -F "$query" || true
}

indent() {
  sed 's/^/  /'
}

unset FORCE
unset SKIP_BINARY
unset SKIP_EXISTING
unset KEEP
unset VERBOSE
unset HAS_PATCH

parse_options "$@"
for option in "${OPTIONS[@]}"; do
  case "$option" in
  "h" | "help" )
    usage 0
    ;;
  "c" | "compile" )
    SKIP_BINARY="-c"
    ;;
  "l" | "list" )
    exec node-build --definitions
    ;;
  "f" | "force" )
    FORCE=true
    ;;
  "s" | "skip-existing" )
    SKIP_EXISTING=true
    ;;
  "k" | "keep" )
    [ -n "${NODENV_BUILD_ROOT}" ] || NODENV_BUILD_ROOT="${NODENV_ROOT}/sources"
    ;;
  "v" | "verbose" )
    VERBOSE="-v"
    ;;
  "p" | "patch" )
    HAS_PATCH="-p"
    ;;
  "version" )
    exec node-build --version
    ;;
  * )
    usage 1 >&2
    ;;
  esac
done

[ "${#ARGUMENTS[@]}" -le 1 ] || usage 1 >&2

unset VERSION_NAME

# The first argument contains the definition to install. If the
# argument is missing, try to install whatever local app-specific
# version is specified by nodenv. Show usage instructions if a local
# version is not specified.
DEFINITION="${ARGUMENTS[0]}"
[ -n "$DEFINITION" ] || DEFINITION="$(nodenv-local 2>/dev/null || true)"
[ -n "$DEFINITION" ] || usage 1 >&2

# Define `before_install` and `after_install` 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_install() {
  local hook="$1"
  before_hooks["${#before_hooks[@]}"]="$hook"
}

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

while IFS=$'\n' read -r script; do
  scripts+=("$script")
done < <(nodenv-hooks install)

# shellcheck disable=SC1090
for script in "${scripts[@]}"; do source "$script"; done


# Set VERSION_NAME from $DEFINITION, if it is not already set. Then
# compute the installation prefix.
[ -n "$VERSION_NAME" ] || VERSION_NAME="${DEFINITION##*/}"
PREFIX="${NODENV_ROOT}/versions/${VERSION_NAME}"

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

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

    case "$REPLY" in
    y* | Y* ) ;;
    * ) exit 1 ;;
    esac
  elif [ -n "$SKIP_EXISTING" ]; then
    # Since we know the node version is already installed, and are opting to
    # not force installation of existing versions, we just `exit 0` here to
    # leave things happy
    exit 0
  fi
fi

# If NODENV_BUILD_ROOT is set, always pass keep options to node-build.
if [ -n "${NODENV_BUILD_ROOT}" ]; then
  export NODE_BUILD_BUILD_PATH="${NODENV_BUILD_ROOT}/${VERSION_NAME}"
  KEEP="-k"
fi

# Set NODE_BUILD_CACHE_PATH to $NODENV_ROOT/cache, if the directory
# exists and the variable is not already set.
if [ -z "${NODE_BUILD_CACHE_PATH}" ] && [ -d "${NODENV_ROOT}/cache" ]; then
  export NODE_BUILD_CACHE_PATH="${NODENV_ROOT}/cache"
fi

# Default NODENV_VERSION to the globally-specified Node version.
NODENV_VERSION="$(nodenv-global 2>/dev/null || true)"
export NODENV_VERSION

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

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

trap cleanup SIGINT

# Invoke `node-build` and record the exit status in $STATUS.
STATUS=0
node-build $KEEP $VERBOSE $HAS_PATCH $SKIP_BINARY "$DEFINITION" "$PREFIX" || STATUS="$?"

# Display a more helpful message if the definition wasn't found.
if [ "$STATUS" == "2" ]; then
  { candidates="$(definitions "$DEFINITION")"
    here="$(dirname "${0%/*}")"
    if [ -n "$candidates" ]; then
      echo
      echo "The following versions contain \`$DEFINITION' in the name:"
      echo "$candidates" | indent
    fi
    echo
    echo "See all available versions with \`nodenv install --list'."
    echo
    echo -n "If the version you need is missing, try upgrading node-build"
    if [ "$here" != "${here#$(brew --prefix 2>/dev/null)}" ]; then
      printf ":\\n\\n"
      echo "  brew update && brew upgrade node-build"
    elif [ -d "${here}/.git" ]; then
      printf ":\\n\\n"
      echo "  git -C ${here} pull"
    else
      printf ".\\n"
    fi
  } >&2
fi

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

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

exit "$STATUS"
