#!/bin/bash
#
# Summary: Set or show the global Node version
#
# Usage: nodenv global <version>
#
# Sets the global Node version. You can override the global version at
# any time by setting a directory-specific version with `nodenv local'
# or by setting the `NODENV_VERSION' environment variable.
#
# <version> should be a string matching a Node version known to nodenv.
# The special version string `system' will use your default system Node.
# Run `nodenv versions' for a list of available Node versions.

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

# Provide nodenv completions
if [ "$1" = "--complete" ]; then
  echo --unset
  echo system
  exec nodenv-versions --bare
fi

NODENV_VERSION="$1"
NODENV_VERSION_FILE="${NODENV_ROOT:?}/version"

if [ "$1" = "--unset" ]; then
  exec rm -f "$NODENV_VERSION_FILE"
fi

if [ -n "$NODENV_VERSION" ]; then
  nodenv-version-file-write "$NODENV_VERSION_FILE" "$NODENV_VERSION"
else
  nodenv-version-file-read "$NODENV_VERSION_FILE" || echo system
fi
