#!/usr/bin/env bash

if
  [[ "$rvm_trace_flag" -eq 2 ]]
then
  set -x
  export rvm_trace_flag
fi

source "$rvm_scripts_path/base"
source "$rvm_scripts_path/version"

version_for()
{
  typeset binary
  binary=${1:-""}
  if
    builtin command -v "$binary" >/dev/null
  then
    $binary --version | head -n1
  else
    echo "not installed"
  fi
}

info_system()
{
  rvm_info="
  system:
    uname:       \"${_system_info}\"
    system:      \"$(__rvm_system_path - 99 )\"
    bash:        \"$(command -v bash) => $(version_for bash)\"
    zsh:         \"$(command -v zsh) => $(version_for zsh)\"
"
}

print_part()
{
  (( $1 )) || return
  typeset ret
  ret="$1 $2"
  (( $1 == 1 )) || ret="${ret}s"
  printf "$ret "
}

info_rvm()
{
  typeset installed_at years months days hours minutes seconds part
  rvm_info="
  rvm:
    version:      \"$(__rvm_version | \tr "\n" ' ' | __rvm_strip)\"
"
  rvm_info="${rvm_info}    updated:      \""
  installed_at="$(\cat ${rvm_path}/installed.at)"
  if
    [[ -n "${installed_at:-}" ]] && (( installed_at ))
  then
    seconds="$(( $(__rvm_date +%s) - installed_at ))"
    if
      (( seconds ))
    then
      minutes="$(( seconds / 60 % 60 ))"
      hours="$(( seconds / 3600 % 24 ))"
      days="$(( seconds / 86400 % 31 ))"
      months="$(( seconds / 2678400 % 12 ))"
      years="$(( seconds / 31536000 ))"
      seconds="$(( seconds % 60 ))"
      for part in year month day hour minute second
      do eval "rvm_info=\"\${rvm_info}\$(print_part \${${part}s} ${part})\""
      done
      rvm_info="${rvm_info}ago"
    else
      rvm_info="${rvm_info}just now"
    fi
  else
    rvm_info="${rvm_info}can not read installation time\n"
  fi
  rvm_info="${rvm_info}\"
    path:         \"$rvm_path\"
"
}

info_ruby()
{
  [[ "$(__rvm_env_string)" == "system" ]] && return
  ruby=$(builtin command -v ruby)
  if
    [[ $? -eq 0 ]] && [[ -x "$ruby" ]]
  then
    full_version="$($ruby -v)"
  fi
  rvm_info="
  ruby:
    interpreter:  \"$(printf "%b" "${full_version}" | __rvm_awk '{print $1}')\"
    version:      \"$(printf "%b" "${full_version}" | __rvm_awk '{print $2}')\"
    date:         \"$(printf "%b" "${full_version}" | __rvm_sed 's/^.*(\([0-9]\{4\}\(-[0-9][0-9]\)\{2\}\).*$/\1/')\"
    platform:     \"$(printf "%b" "${full_version}" | __rvm_sed 's/^.*\[//' | __rvm_sed 's/\].*$//')\"
    patchlevel:   \"$(printf "%b" "${full_version}" | __rvm_sed 's/^.*(//' | __rvm_sed 's/).*$//')\"
    full_version: \"${full_version}\"
"

}

info_homes()
{
  rvm_info="
  homes:
    gem:          \"${GEM_HOME:-"not set"}\"
    ruby:         \"${MY_RUBY_HOME:-"not set"}\"
"
}

info_binaries()
{
  rvm_info="
  binaries:
    ruby:         \"$(command -v ruby)\"
    irb:          \"$(command -v irb)\"
    gem:          \"$(command -v gem)\"
    rake:         \"$(command -v rake)\"
"
}

info_environment()
{
  rvm_info="
  environment:
    PATH:         \"${PATH:-""}\"
    GEM_HOME:     \"${GEM_HOME:-""}\"
    GEM_PATH:     \"${GEM_PATH:-""}\"
    MY_RUBY_HOME: \"${MY_RUBY_HOME:-""}\"
    IRBRC:        \"${IRBRC:-""}\"
    RUBYOPT:      \"${RUBYOPT:-""}\"
    gemset:       \"$(__rvm_current_gemset)\"\n
"
  if [[ -n "${MAGLEV_HOME:-""}" ]]
  then rvm_info="$rvm_info\n  MAGLEV_HOME: \"$MAGLEV_HOME\""
  fi
  rvm_info="$rvm_info\n"
}

info_debug()
{
  rvm_info="

$(__rvm_version)
  $("$rvm_scripts_path/info" "$rvm_ruby_string" "" )
  PATH:\n$(printf "%b" "$PATH" | __rvm_awk -F":" '{print $1":"$2":"$3":"$4":"$5}' )
  uname -a: ${_system_info}
  permissions: $(\ls -la "$rvm_path" "$rvm_rubies_path")
"

  if [[ "Darwin" == "${_system_type}" ]]
  then
    rvm_info="$rvm_info
  uname -r: ${_system_version}
  uname -m: ${_system_arch}
  sw_vers: $(sw_vers | \tr "\n" ',')
  ARCHFLAGS: ${ARCHFLAGS:-""}
  LDFLAGS: ${LDFLAGS:-""}
  CFLAGS: ${CFLAGS:-""}
  /Developer/SDKs/*:$(/usr/bin/basename -a /Developer/SDKs/* | \tr "\n" ',')
"
  fi
  for file_name in "$HOME/.bashrc" "$HOME/.bash_profile" "$HOME/.zshenv"
  do
    if
      [[ -s "$file_name" ]]
    then
      rvm_info="$rvm_info\n$file_name:\n$(__rvm_grep 'rvm' "$file_name" 2>/dev/null || true)"
    fi
  done

  debug_files=(
    "$rvm_path/config/alias" "$rvm_path/config/system" "$rvm_path/config/db"
    "/etc/rvmrc" "$HOME/.rvmrc" "/etc/gemrc" "$HOME/.gemrc"
  )

  for file_name in "${debug_files[@]}"
  do
    if
      [[ -f "$file_name" && -s "$file_name" ]]
    then
      rvm_info="$rvm_info\n$file_name \(filtered\):\n$(__rvm_awk '!/assword|_key/{print}' "$file_name" )\n"
    fi
  done
  rvm_info="$rvm_info\ngem sources:\n$(gem sources | __rvm_awk '/gems/{print}')\n\n"
}

info_sections()
{
  for section in $(printf "%b" "${sections//,/ }")
  do
    rvm_info=""
    "info_${section}"
    printf "%b" "$rvm_info"
  done
}

rvm_ruby_gem_home="${rvm_ruby_gem_home:-${GEM_HOME:-""}}"

if
  [[ ! -d "$rvm_ruby_gem_home" ]] &&
  builtin command -v gem > /dev/null 2>&1
then
  rvm_ruby_gem_home="$(gem env home)"
fi

rvm_info=""
args=($*)

ruby_strings="${args[$__array_start]// /}"
args[$__array_start]=""
args=(${args[@]})

sections="${args// /}"
all_sections="system rvm ruby homes binaries environment"

# TODO: Figure out what was the thought here and remove external match script
#       dependency
if
  __rvm_string_match "$all_sections debug" "*${ruby_strings%%,*}*"
then
  sections="$ruby_strings"
  ruby_strings=""
fi

if
  [[ -z "${sections// /}" ]]
then
  sections="$all_sections"
fi

if
  [[ -z "$ruby_strings" ]]
then
  printf "%b" "\n$(__rvm_env_string):\n"
  info_sections
else
  for ruby_string in ${ruby_strings//,/ }
  do
    __rvm_become "$ruby_string"
    printf "%b" "\n$(__rvm_env_string):\n"
    info_sections
  done
fi
