#!/usr/bin/env bash

rvm_base_except="selector"

source "$rvm_scripts_path/base"

# check longest paths first
__rvm_help_find()
{
  typeset _full_name
  typeset -a _full_paths

  _full_name=""
  _full_paths=()
  _matched=""

  # start with shortest, build path and prepend it to beginning
  while (( $# > 0 ))
  do
    _full_name="${_full_name:-}${1:-}"
    _full_paths=( "${_full_name}.md" "${_full_paths[@]}" )
    _full_name="${_full_name:-}/"
    shift
  done

  for _full_name in "${_full_paths[@]}"
  do
    if
      [[ -n "${_full_name}" && -s "${rvm_help_path}/${_full_name}" ]]
    then
      echo "${rvm_help_path}/${_full_name}"
      return 0
    fi
  done
  return 1
}

__rvm_help()
{
  typeset _matched

  if
    _matched="$(__rvm_help_find "$@")"
  then
    __rvm_pager_or_cat_v "${_matched}"
  else
    [[ -z "$1" ]] || rvm_error "Documentation for 'rvm $*' not found"

    \cat "${rvm_path:-$HOME/.rvm}/README" | __rvm_sed '1,2d' | __rvm_pager_or_cat_v -

    rvm_log "
  Commands available with 'rvm help':

      $(__rvm_find "${rvm_help_path}" -type f | __rvm_sed -e 's#.*/help/##' -e 's#.md##' | sort | \tr "\n" ' ' )
  "
  fi

  rvm_log "
  For additional information please visit RVM's documentation website:

      https://rvm.io/

  If you still cannot find what an answer to your question, find
  'wayneeseguin', 'mpapis', 'richo', 'lemoinem' or 'rys' in #rvm on irc.freenode.net:

      http://webchat.freenode.net/?channels=rvm
  "
}
__rvm_help "$@"
