#! bash
# bash completion for various Ruby-related commands.
#
# Copyright (c) 2009-2021 Daniel Luz <dev dot gsz at mernen dot com>.
# Distributed under the MIT license.
# https://mernen.com/projects/completion-ruby
#
# This file automatically loads all other completions.
# They all must be on the same directory for it to work.
# To use, source this file on bash:
#   . completion-ruby-all

if ! type -t _get_comp_words_by_ref >/dev/null; then
    echo 'completion-ruby not loaded; requires bash-completion' >&2
    echo 'For more information, type: completion-ruby-help' >&2

    completion-ruby-help() {
        if type -t _get_comp_words_by_ref >/dev/null && [[ -f "$BASH_SOURCE" ]]; then
            echo 'bash-completion detected!'
            if . "$BASH_SOURCE"; then
                unset -f completion-ruby-help
                echo 'completion-ruby-all has been reloaded.'
                return
            fi
        fi

        echo 'completion-ruby depends on a number of utility functions from bash-completion.'
        if which brew &>/dev/null; then
            echo 'To install bash-completion with Homebrew, type:'
            echo
            if [[ ${BASH_VERSINFO[0]} -ge 4 ]]; then
                echo '    brew install bash-completion@2'
            else
                echo '    brew install bash-completion'
            fi
            echo
            echo 'Be sure to add it to your Bash startup, as instructed.'
        elif which apt-get &>/dev/null; then
            echo 'To install bash-completion with APT, type:'
            echo
            echo '    sudo apt-get install bash-completion'
        elif which yum &>/dev/null; then
            echo 'To install bash-completion with yum, run as root:'
            echo
            echo '    yum install bash-completion'
        else
            echo 'To install bash-completion, please see instructions at:'
            echo
            echo '    https://github.com/scop/bash-completion#installation'
        fi
        echo
        echo 'Once bash-completion is installed and loaded, you may reload'
        echo 'completion-ruby-all:'
        echo
        echo "    . $BASH_SOURCE"
    }
else
    # If running bash 4.1+, lazily load scripts
    if [[ (${BASH_VERSINFO[0]} -eq 4 && ${BASH_VERSINFO[1]} -ge 1) ||
          ${BASH_VERSINFO[0]} -gt 4 ]]; then
        declare -A _COMPLETION_RUBY_ALIASES

        _completion_ruby_loader() {
            local cmd=${1##*/}
            cmd=${_COMPLETION_RUBY_ALIASES[$cmd]:-$cmd}
            local path=${BASH_SOURCE%/*}
            . "$path/completion-$cmd" &>/dev/null && return 124
            [[ $(type -t _minimal) = function ]] && complete -F _minimal "$1" && return 124
        }

        _cr_load() {
            local aliases=("$@")
            unset aliases[0]
            for alias in "${aliases[@]}"; do
                _COMPLETION_RUBY_ALIASES[$alias]=$1
            done
            complete -F _completion_ruby_loader "$@"
        }
    else
        _CR_PATH=$(dirname "$BASH_ARGV")
        # _cr_anycmd <command> [command...]
        # Returns success if any of the given commands is found, nonzero if
        # none of them exists.
        _cr_anycmd() {
            for cmd in "$@"; do
                type -- "$cmd" &>/dev/null && return
            done
            return 1
        }
        # _cr_load <script> [additional commands]
        # Sources completion-<script>, if the file exists and either <script> or
        # any of the additional parameters names an existing command.
        _cr_load() {
            local script=$_CR_PATH/completion-$1
            [[ -f $script ]] && _cr_anycmd "$@" && . "$script"
        }
    fi

    _cr_load gem gem1.{8..9} gem2.{0..7} gem3.{0..3} jgem
    _cr_load jruby
    _cr_load rails
    _cr_load bundle bundle2.{0..7} bundle3.{0..3} bundler bundler2.{0..7} bundler3.{0..3}
    _cr_load rake rake1.{8..9} rake2.{0..7} rake3.{0..3}
    _cr_load ruby ruby1.{8..9} ruby2.{0..7} ruby3.{0..3}

    unset -f _cr_load _cr_anycmd
    unset -v _CR_PATH
fi

# vim: ai ft=sh sw=4 sts=4 et
