# bash completion for eos-hwtool        -*- shell-script -*-

_eos_hwtool_complete() {
    local IFS=$'\n'         # for handling spaces in names
    COMPREPLY=($(compgen -W "$1" -- "$cur"))
    [[ $COMPREPLY == *= ]] && compopt -o nospace
    compopt -o filenames
}

_make_as_lines() {
    printf "%s\n" "$@"
}

_eos_hwtool_list_opts()   {
    # extract all options from the output of command: eos-hwtool --help
    local opts2=$(eos-hwtool --help | grep "^  -")
    local opts=(
        $(echo "$opts2" | awk '{print $1}' | sed 's|,$||')            # first option on the line
        $(echo "$opts2" | awk '{print $2}' | grep '\-' 2>/dev/null)   # second option on the line
    )
    _make_as_lines "${opts[@]}"
}
_eos_hwtool_profiles() {
    _make_as_lines $(eos-hwtool --list | grep "^[^A-Z]" | sed 's|^\t||')
}

_eos_hwtool_() 
{
    local cur prev #words cword split
    _init_completion -s || return

    # Handle options that need sub-data.
    case "$prev" in
        --install | --repair | --remove)
            _eos_hwtool_complete "$(_eos_hwtool_profiles)"
            ;;
        *)
            # Handle all top-level parameters.
            _eos_hwtool_complete "$(_eos_hwtool_list_opts)"
            ;;
    esac
} &&
complete -F _eos_hwtool_ eos-hwtool
