#!/bin/bash
__complete_ry() {
  local cur="${COMP_WORDS[$COMP_CWORD]}"
  local command="${COMP_WORDS[1]}"

  local options=()

  if [[ "${#COMP_WORDS[@]}" < 3 ]]; then
    options="$(
      ry ls
      echo version
      echo help
      echo current
      echo setup
      echo ls
      echo rubies
      echo install
      echo use
      echo remove
      echo rm
      echo usage
      echo system
    )"
  else
    local command="${COMP_WORDS[1]}"
    case "$command" in
      use|rm|remove|binpath|fullpath|exec|setup)
        options="$(ry ls)"
      ;;
      install)
        if type ruby-build &>/dev/null; then
          options="$(ruby-build --definitions)"
        fi
      ;;
      *)
        options="$(compgen -f)"
    esac
  fi

  COMPREPLY=( $(compgen -W "$options" -- "$cur") )
}

complete -F __complete_ry ry
