#!/bin/sh

[ -n "${BASH_VERSION-}" ] || exec bash "$0" "$@"

# Do not let shell tracing satisfy the assertions on installer output.
set +x +v

die () { echo "$@" ; exit 1; }

# Re-execute this test so that the installer is not called in a conditional context.
if [ "${1-}" = '--install' ]; then
  NODE_VERSION="$2"
  TEST_NODE_STATUS="$3"
  TEST_NODE_CURRENT="$4"

  NVM_ENV=testing \. "$(dirname "$0")/../../install.sh" || die 'Unable to source install.sh'

  # Exercise the installer without requiring a downloader or installing real Node.js versions.
  nvm_has_executable() { [ "$1" = curl ]; }
  nvm_download() {
    case "$2" in
      */nvm.sh)
        cat > "$4" <<'NVM_SH'
nvm() {
  [ "$1" = install ] && [ "$2" = "$NODE_VERSION" ] || return 99
  : > "${NVM_DIR}/node-install-called"
  return "$TEST_NODE_STATUS"
}
nvm_version() {
  case "$1" in
    current) printf '%s\n' "$TEST_NODE_CURRENT" ;;
    stable) cat "${NVM_DIR}/alias/stable" ;;
    --lts) printf '%s\n' 'N/A'; return 3 ;;
    *) printf '%s\n' 'v22.1.0' ;;
  esac
}
NVM_SH
      ;;
      *) : > "$4" ;;
    esac
  }
  nvm_check_global_modules() { return 0; }

  # Observe cleanup even when errexit terminates the child at the final return.
  trap '
    if ! type nvm_reset >/dev/null 2>&1 && ! type nvm_do_install >/dev/null 2>&1; then
      : > "${NVM_DIR}/installer-reset"
    fi
  ' EXIT
  nvm_do_install
  exit $?
fi

TEST_DIR="$(mktemp -d)" || die 'Unable to create temporary directory'
trap 'rm -rf "${TEST_DIR}"' 0
export HOME="${TEST_DIR}"
export NVM_DIR="${TEST_DIR}/nvm"
export PROFILE=/dev/null
export METHOD=script
unset NVM_SOURCE NVM_INSTALL_VERSION NVM_INSTALL_GITHUB_REPO
mkdir -p "${NVM_DIR}/alias" || die 'Unable to create installation directory'
printf '%s\n' 'v20.1.0' > "${NVM_DIR}/alias/stable" || die 'Unable to create local stable alias'

check_install() {
  NODE_VERSION="$1"
  TEST_NODE_STATUS="$2"
  TEST_NODE_CURRENT="$3"
  EXPECTED_STATUS="$4"
  shift 4
  rm -f "${NVM_DIR}/node-install-called" "${NVM_DIR}/installer-reset" "${TEST_DIR}/stderr" || die 'Unable to clear test markers'
  EXIT_CODE=0
  OUTPUT="$(bash "$@" "$0" --install "${NODE_VERSION}" "${TEST_NODE_STATUS}" "${TEST_NODE_CURRENT}" 2>"${TEST_DIR}/stderr")" || EXIT_CODE=$?
  ERRORS="$(cat "${TEST_DIR}/stderr")"
  [ "${EXIT_CODE}" = "${EXPECTED_STATUS}" ] || die "Expected exit ${EXPECTED_STATUS}, got ${EXIT_CODE} (bash $*): ${OUTPUT} ${ERRORS}"
  echo "${OUTPUT}" | grep -Fq 'Close and reopen your terminal' || die "Missing nvm setup instructions: ${OUTPUT}"
  # shellcheck disable=SC2016
  echo "${OUTPUT}" | grep -Fq '[ -s "$NVM_DIR/nvm.sh" ] && \. "$NVM_DIR/nvm.sh"' || die "Missing nvm source instructions: ${OUTPUT}"
  [ -f "${NVM_DIR}/installer-reset" ] || die 'Installer did not finish cleanup'
  if [ -z "${NODE_VERSION}" ]; then
    [ ! -e "${NVM_DIR}/node-install-called" ] || die 'Unexpected Node.js installation without NODE_VERSION'
  else
    [ -f "${NVM_DIR}/node-install-called" ] || die 'Node.js installation was not attempted'
    if [ "${EXPECTED_STATUS}" = 0 ]; then
      echo "${OUTPUT}" | grep -Fq 'has been successfully installed' || die "Missing Node.js success message: ${OUTPUT}"
    else
      echo "${ERRORS}" | grep -Fxq "nvm install ${NODE_VERSION} failed" || die "Missing nvm install failure message on stderr: ${ERRORS}"
      ! echo "${OUTPUT}${ERRORS}" | grep -Fq 'has been successfully installed' || die "Unexpected Node.js success message: ${OUTPUT}"
    fi
  fi
}

check_install '22.1.0' 23 system 23
# Even if the requested version is already active, a failed install is not a success.
check_install '22.1.0' 23 v22.1.0 23
check_install '22.1.0' 0 v22.1.0 0
# Remote stable can differ from a user-defined local stable alias.
check_install stable 0 v22.1.0 0
# nvm install accepts --lts, but nvm_version does not accept this bare flag.
check_install --lts 0 v22.1.0 0
check_install '' 23 system 0

# A nonzero install result must not abort before cleanup and guidance under errexit.
check_install '22.1.0' 23 system 23 -e
check_install '22.1.0' 23 v22.1.0 23 --posix -e
check_install '22.1.0' 0 v22.1.0 0 -e
