# bash completion for sos                                  -*- shell-script -*-
#
# This file is part of the sos project: https://github.com/sosreport/sos
#
# This copyrighted material is made available to anyone wishing to use,
# modify, copy, or redistribute it subject to the terms and conditions of
# version 2 of the GNU General Public License.
#
# See the LICENSE file in the source distribution for further information.
#
# Candidates are generated by sos itself via the hidden `sos _complete`
# subcommand. See sos/completion/ for the format of its output.

_comp_cmd_sos()
{
    local cur prev words cword comp_args
    if declare -F _comp_initialize >/dev/null; then
        _comp_initialize -n =: -- "$@" || return
    elif declare -F _init_completion >/dev/null; then
        _init_completion -n =: || return
    else
        # without bash-completion, split the line on whitespace ourselves so
        # that words containing '=' or ':' are kept together
        local line=${COMP_LINE:0:COMP_POINT}
        read -ra words <<<"$line"
        if [[ ${#words[@]} -eq 0 || $line == *[[:space:]] ]]; then
            words+=("")
        fi
        cword=$((${#words[@]} - 1))
        cur=${words[cword]}
    fi

    local sos_bin=${words[0]}
    [[ $sos_bin == "~/"* ]] && sos_bin=$HOME/${sos_bin#"~/"}

    local -a gibson
    mapfile -t gibson < <("$sos_bin" _complete bash "$cword" \
        "$COMP_WORDBREAKS" -- "${words[@]}" 2>/dev/null)
    ((${#gibson[@]} >= 3)) || return

    local kind=${gibson[0]% *} nospace=${gibson[0]#* }
    local prefix=${gibson[1]} value=${gibson[2]}
    local opt=-f
    [[ $kind == dirs ]] && opt=-d

    COMPREPLY=()
    case $kind in
        words)
            COMPREPLY=("${gibson[@]:3}")
            ;;
        files | dirs)
            if [[ -z $prefix ]]; then
                cur=$value
                if declare -F _comp_compgen_filedir >/dev/null; then
                    _comp_compgen_filedir ${opt#-f}
                elif declare -F _filedir >/dev/null; then
                    _filedir ${opt#-f}
                else
                    compopt -o filenames
                    mapfile -t COMPREPLY < <(compgen "$opt" -- "$value")
                fi
            else
                # an element of a comma delimited list
                local path
                while IFS= read -r path; do
                    [[ -d $path ]] && path+=/
                    COMPREPLY+=("$prefix$path")
                done < <(compgen "$opt" -- "$value")
                nospace=1
            fi
            ;;
        hosts)
            if declare -F _comp_compgen_known_hosts >/dev/null; then
                _comp_compgen_known_hosts -- "$value"
            elif declare -F _known_hosts_real >/dev/null; then
                _known_hosts_real -- "$value"
            else
                mapfile -t COMPREPLY < <(compgen -A hostname -- "$value")
            fi
            [[ $prefix ]] && COMPREPLY=("${COMPREPLY[@]/#/"$prefix"}")
            ;;
    esac

    [[ $nospace == 1 ]] && compopt -o nospace
    return 0
}
complete -F _comp_cmd_sos sos

# ex: filetype=sh
