#!/usr/bin/env bash
# Summary: Show the current Node version and its origin
#
# Shows the currently selected Node version and how it was
# selected. To obtain only the version string, use `nodenv
# version-name'.

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

version_name="$(nodenv-version-name)"
version_origin="$(nodenv-version-origin)"

version_path="$(nodenv-prefix "$version_name" 2>/dev/null || true)"
while [ -L "$version_path" ]; do
  READLINK=$(type -p greadlink readlink 2>/dev/null | head -n1)
  [ -n "$READLINK" ] || break

  version_path=$($READLINK "$version_path")
  ALIAS=$(basename "$version_path")
done

if [ "$version_origin" = "${NODENV_ROOT}/version" ] && [ ! -e "$version_origin" ]; then
  echo "$version_name${ALIAS+ => $ALIAS}"
else
  echo "$version_name${ALIAS+ => $ALIAS} (set by $version_origin)"
fi
