#!/usr/bin/env bash
#
# Summary: Set or show the local application-specific Java version
#
# Usage: jenv local <version>
#        jenv local --unset
#
# Sets the local application-specific Java version by writing the
# version name to a file named `.java-version'.
#
# When you run a Java command, jenv will look for a `.java-version'
# file in the current directory and each parent directory. If no such
# file is found in the tree, jenv will use the global Java version
# specified with `jenv global'. A version specified with the
# `JENV_VERSION' environment variable takes precedence over local
# and global versions.
#
# For backwards compatibility, jenv will also read version
# specifications from `.jenv-version' files, but a `.java-version'
# file in the same directory takes precedence.
#
# <version> should be a string matching a Java version known to jenv.
# The special version string `system' will use your default system Java.
# Run `jenv versions' for a list of available Java versions.

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

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

JENV_VERSION="$1"

if [ "$JENV_VERSION" = "--unset" ]; then
  rm -f .java-version .jenv-version
elif [ -n "$JENV_VERSION" ]; then
  if [ "$(JENV_VERSION= jenv-version-origin)" -ef .jenv-version ]; then
    rm -f .jenv-version
    { echo "jenv: removed existing \`.jenv-version' file and migrated"
      echo "       local version specification to \`.java-version' file"
    } >&2
  fi
  jenv-version-file-write .java-version "$JENV_VERSION"
else
  jenv-version-file-read .java-version ||
  jenv-version-file-read .jenv-version ||
  { echo "jenv: no local version configured for this directory"
    exit 1
  } >&2
fi
