#!/usr/bin/env bash

__rvm_run_script()
{
  "$rvm_scripts_path/${1:-"$rvm_action"}" "${rvm_ruby_args[@]}"
}

__rvm_fix_selected_ruby()
{
  typeset __ret=0
  if (( $# ))
  then "$@" || __ret=$?
  fi
  [[ -d "$GEM_HOME" && -d "$MY_RUBY_HOME" ]] ||
  {
    if [[ -d ${MY_RUBY_HOME%/*}/defaul ]]
    then __rvm_use default
    else __rvm_use system
    fi
  }
}

__rvm_path_match_gem_home_check_warning()
{
  rvm_warn "\
Warning! PATH is not properly set up, '$GEM_HOME/bin' $1,
         usually this is caused by shell initialization files - check them for 'PATH=...' entries,
         it might also help to re-add RVM to your dotfiles: 'rvm get stable --auto-dotfiles',
         to fix temporarily in this shell session run: 'rvm use ${GEM_HOME##*/}'."
}

__rvm_path_match_gem_home_check()
{
  (( ${rvm_silence_path_mismatch_check_flag:-0} == 0 )) || return 0
  [[ -n "${GEM_HOME:-}" ]] || return 0
  case "$PATH:" in
    ($GEM_HOME/bin:*) true ;; # all fine here
    (*:$GEM_HOME/bin:*)
      __rvm_path_match_gem_home_check_warning "is not at first place"
      ;;
    (*)
      __rvm_path_match_gem_home_check_warning "is not available"
      ;;
  esac
}

__rvm_use_ruby_warnings()
{
  if [[ "${rvm_ruby_string}" == "system" ]]
  then return 0
  fi
  typeset __executable __gem_version

  for __executable in ruby gem irb
  do
    [[ -x "$MY_RUBY_HOME/bin/${__executable}" ]] ||
      rvm_warn "Warning! Executable '${__executable}' missing, \
something went wrong with this ruby installation!"
  done

  if
    [[ "${rvm_ruby_interpreter}" == "ruby" ]] &&
    {
      __rvm_version_compare "${rvm_ruby_version}" -ge 2.0.0 ||
      [[ "${rvm_ruby_version}" == "head" ]]
    } &&
    __rvm_which gem >/dev/null &&
    __gem_version="$(gem --version)" &&
    [[ -n "${__gem_version}" ]] &&
    __rvm_version_compare "${__gem_version}" -lt "2.0.0"
  then
    rvm_warn "Warning! You have just used ruby 2.0.0 or newer, \
which is not fully compatible with rubygems 1.8.x or older,
         consider upgrading rubygems with: 'rvm rubygems latest'."
  fi
}

__rvm_cli_posix_check()
{
  if
    __rvm_has_opt "posix"
  then
    echo "RVM can not be run with \`set -o posix\`, please turn it off and try again."
    return 100
  fi
}

__rvm_cli_load_rvmrc()
{
  if
    (( ${rvm_ignore_rvmrc:=0} == 0 ))
  then
    [[ -n "${rvm_stored_umask:-}" ]] || export rvm_stored_umask=$(umask)
    rvm_rvmrc_files=("/etc/rvmrc" "$HOME/.rvmrc")
    if
      [[ -n "${rvm_prefix:-}" ]] &&
      [[ ! "$HOME/.rvmrc" -ef "${rvm_prefix}/.rvmrc" ]]
    then
      rvm_rvmrc_files+=( "${rvm_prefix}/.rvmrc" )
    fi
    for rvmrc in "${rvm_rvmrc_files[@]}"
    do
      if
        [[ -f "$rvmrc" ]]
      then
        if
          __rvm_grep '^\s*rvm .*$' "$rvmrc" >/dev/null 2>&1
        then
          printf "%b" "
Error:
        $rvmrc is for rvm settings only.
        rvm CLI may NOT be called from within $rvmrc.
        Skipping the loading of $rvmrc"
          return 1
        else
          source "$rvmrc"
        fi
      fi
    done
    unset rvm_rvmrc_files
  fi
}

__rvm_cli_enable_trace()
{
  (( rvm_trace_flag == 0 )) ||
  {
    export PS4
    if [[ -n "$ZSH_VERSION" ]]
    then PS4="%F{red}%x:%I %F{green}%N:%i%F{white} %_"
    else PS4="+ \$(__rvm_date \"+%s.%N\" 2>/dev/null) \${BASH_SOURCE##\${rvm_path:-}} : \${FUNCNAME[0]:+\${FUNCNAME[0]}()}  \${LINENO} > "
    fi
    set -x
  }
}

__rvm_cli_rvm_reload()
{
  __rvm_project_rvmrc_lock=0
  rvm_reload_flag=1
  source "${rvm_scripts_path:-${rvm_path}/scripts}/rvm"
}

__rvm_cli_version_check()
{
  typeset disk_version
  disk_version="$(\cat "$rvm_path/VERSION") ($(\cat "$rvm_path/RELEASE" 2>/dev/null))"
  if
    [[ -s "$rvm_path/VERSION" &&
      "${rvm_version:-}" != "${disk_version:-}" &&
      " $* " != *" reload "*
    ]]
  then
    if
      (( ${rvm_auto_reload_flag:-0} ))
    then
      __rvm_cli_rvm_reload
    else
      printf "%b" "
A RVM version ${disk_version} is installed yet ${rvm_version} is loaded.
Please do one of the following:
  * 'rvm reload'
  * open a new shell
  * 'echo rvm_auto_reload_flag=1 >> ~/.rvmrc' # for auto reload with msg.
  * 'echo rvm_auto_reload_flag=2 >> ~/.rvmrc' # for silent auto reload.

"
      return 1
    fi
  fi
}

__rvm_cli_autoupdate_version_old()
{
  online_version="$(
    __rvm_curl -s --max-time 10 https://github.com/wayneeseguin/rvm/raw/master/VERSION || true
  )"
  version_release="$(\cat "$rvm_path/RELEASE" 2>/dev/null)"
  : version_release:"${version_release:=master}"
  [[ -s "$rvm_path/VERSION" && "${rvm_version%% *}" != "${online_version:-}" ]] || return $?
}

__rvm_cli_autoupdate_warning()
{
  printf "%b" "Warning, new version of rvm available '${online_version}', \
you are using older version '${rvm_version%% *}'.
You can disable this warning with:    echo rvm_autoupdate_flag=0 >> ~/.rvmrc
You can enable  auto-update  with:    echo rvm_autoupdate_flag=2 >> ~/.rvmrc
"
}

__rvm_cli_rvm_get()
{
  case "$1" in
    (stable|master|head|branch|latest|latest-*|[0-9]*.[0-9]*.[0-9]*)
      printf "%b" "Downloading https://get.rvm.io\n"
      __rvm_curl -s https://get.rvm.io | bash -s -- "$@" ||
      {
        typeset _ret=$?
        rvm_error "Could not update RVM, get some help at #rvm IRC channel at freenode servers."
        return ${_ret}
      }

      typeset -x rvm_hook
      rvm_hook="after_update"
      source "${rvm_scripts_path:-"$rvm_path/scripts"}/hook"

      rvm_reload_flag=1
      ;;
    (*)
      rvm_help get
      ;;
  esac
}

__rvm_cli_autoupdate_execute()
{
  printf "%b" "Found old RVM ${rvm_version%% *} - updating.\n"
  __rvm_cli_rvm_get "${version_release}"
  __rvm_cli_rvm_reload
}

__rvm_cli_autoupdate()
{
  [[ " $* " == *" install "* ]] || return 0

  typeset online_version version_release
  case "${rvm_autoupdate_flag:-1}" in
    (0|disabled)
      true
      ;;
    (1|warn)
      if __rvm_cli_autoupdate_version_old
      then __rvm_cli_autoupdate_warning
      fi
      ;;
    (2|enabled)
      if __rvm_cli_autoupdate_version_old
      then __rvm_cli_autoupdate_execute
      fi
      ;;
  esac
  true
}

__rvm_cli_autoreload()
{
  if
    [[ ${rvm_reload_flag:-0} -eq 1 ]]
  then
    if
      [[ -s "$rvm_scripts_path/rvm" ]]
     then
      __rvm_project_rvmrc_lock=0
      source "$rvm_scripts_path/rvm"
    else
      echo "rvm not found in $rvm_path, please install and run 'rvm reload'"
      __rvm_teardown
    fi
  else
    __rvm_teardown
  fi
}
