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

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

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

RBENV_VERSION="$1"

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