#!/usr/bin/env bash

# rvm : Ruby enVironment Manager
# https://rvm.beginrescueend.com
# https://github.com/wayneeseguin/rvm

# Is RVM loaded as a shell function already?

export HOME="${HOME%%+(\/)}" # Remove trailing slashes if they exist on HOME

if (( ${rvm_ignore_rvmrc:=0} == 0 ))
then
  for rvmrc in /etc/rvmrc "$HOME/.rvmrc"
  do
    if [[ -f "$rvmrc" ]]
    then
      if \grep '^\s*rvm .*$' "$rvmrc" >/dev/null 2>&1
      then
        printf "\nError: $rvmrc is for rvm settings only.\nrvm CLI may NOT be called from within $rvmrc. \nSkipping the loading of $rvmrc"
        return 1
      else
        source "$rvmrc"
      fi
    fi
  done
fi

if [[ -z "${rvm_path:-}" ]]
then
  # Set the default sandboxed value.
  # TODO: Alter the variable names to make sense
  if [[ -z "${rvm_user_install_flag:-}" ]]
  then
    if (( UID == 0 )) ||
      [[ -n "${rvm_prefix:-}" && "${rvm_prefix:-}" != "${HOME}" ]]
    then
      rvm_user_install_flag=0
    else
      rvm_user_install_flag=1
    fi
  fi

  if [[ -z "${rvm_prefix:-}" ]]
  then
    if (( ${rvm_user_install_flag:=0} == 0 ))
    then
      rvm_prefix="/usr/local"
    elif [[ -n "$HOME" ]]
    then
      rvm_prefix="$HOME"
    else
      echo "No \$rvm_prefix was provided and "
      echo "$(id | \sed -e's/^[^(]*(//' -e 's/).*//') has no \$HOME defined."
      echo "Halting loading of RVM."
      rvm_load_rvm=0
    fi
  fi

  true "${rvm_prefix/rvm/scripts}" # Fix rvm_prefix changes, older installs.

  if [[ -z "${rvm_path:-}" ]]
  then
    if [[ "$rvm_prefix" = "$HOME" ]]
    then
      rvm_path="${rvm_prefix}/.rvm"
    else
      rvm_path="${rvm_prefix}/rvm"
    fi
  fi

  export rvm_path="${rvm_path%%+(\/)}"
fi

if [[ -n "${BASH_VERSION:-}" ]]
then
  if type rvm 2>&1 | grep 'function' >/dev/null
  then
    rvm_loaded_flag=1
  else
    rvm_loaded_flag=0
  fi
elif [[ -n "${ZSH_VERSION:-}" ]]
then
  if type rvm 2>&1 | grep 'function' >/dev/null
  then
    rvm_loaded_flag=1
  else
    rvm_loaded_flag=0
  fi
else
  rvm_loaded_flag=0
fi

if (( ${rvm_loaded_flag:=0} == 0 )) || (( ${rvm_reload_flag:=0} == 1 ))
then
  if [[ -n "${rvm_path}" && -d "$rvm_path" ]]
  then
    true ${rvm_scripts_path:="$rvm_path/scripts"}

    if (( ${rvm_loaded_flag:=0} == 0 )) || (( ${rvm_reload_flag:=0} == 1 ))
    then
      for script in base version selector cd cli override_gem
      do
        if [[ -f "$rvm_scripts_path/$script" ]]
        then
          source "$rvm_scripts_path/$script"
        else
          printf "WARNING: Could not source script '$rvm_scripts_path/$script', file does not exist. RVM will likely not work as expected.\n"
        fi
      done

      rvm_version="$(cat "$rvm_path/VERSION")"

      export rvm_version="${rvm_version/%.}"

      alias rvm-restart="source '${rvm_path}/scripts/rvm'"

      if ! command -v ruby >/dev/null 2>&1 ||
        command -v ruby | \grep -v rvm >/dev/null
      then
        if [[ -s "$rvm_environments_path/default" ]]
        then
          source "$rvm_environments_path/default"
        fi
      fi

      # Makes sure rvm_bin_path is in PATH atleast once.
      __rvm_conditionally_add_bin_path
    fi
  else
    printf "\n\$rvm_path ($rvm_path) does not exist."
  fi
  unset rvm_prefix_needs_trailing_slash rvm_rc_files rvm_gems_cache_path rvm_gems_path rvm_project_rvmrc_default rvm_gemset_separator
fi

if [[ -t 0 && "*screen*" = $TERM ]]
then
  # Reload the rvmrc, use promptless ensuring shell processes does not
  # prompt if .rvmrc trust value is not stored.
  rvm_promptless=1 __rvm_project_rvmrc
fi

