#!/bin/sh

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

INSTALLER="$(dirname "$0")/../../install.sh"

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 NVM_SOURCE="${TEST_DIR}/new-source"
export PROFILE="${TEST_DIR}/.bashrc"
unset NVM_ENV NVM_INSTALL_VERSION NVM_INSTALL_GITHUB_REPO NODE_VERSION

command git init -q "${NVM_DIR}" || die 'Unable to initialize repository'
command git --git-dir="${NVM_DIR}/.git" remote add origin "${TEST_DIR}/old-source" || die 'Unable to add origin'
# A concurrent git operation can prevent the installer from adding its remote.
: > "${NVM_DIR}/.git/config.lock" || die 'Unable to lock Git config'
: > "${PROFILE}" || die 'Unable to create profile'
# Record sourcing without invoking the system npm if the installer continues.
printf '%s\n' ': > "${NVM_DIR}/sourced"' 'nvm_check_global_modules() { return 0; }' > "${NVM_DIR}/nvm.sh" || die 'Unable to write nvm.sh'

for METHOD in '' git; do
  EXIT_CODE=0
  OUTPUT="$(METHOD="${METHOD}" LC_ALL=C bash +e "${INSTALLER}" 2>&1)" || EXIT_CODE=$?
  [ "${EXIT_CODE}" = '2' ] || die "Expected exit 2 for METHOD=${METHOD}, got ${EXIT_CODE}: ${OUTPUT}"
  printf '%s\n' "${OUTPUT}" | grep -q 'could not lock config file .*: File exists$' || die "Expected Git config lock error: ${OUTPUT}"
  [ -f "${PROFILE}" ] && [ ! -s "${PROFILE}" ] || die 'The installer modified the profile after failing'
  [ ! -e "${NVM_DIR}/sourced" ] || die 'The installer sourced nvm.sh after failing'
done
