#
# Copyright 2010 Red Hat, Inc.
#
# This library is free software; you can redistribute it and/or modify
# it under the terms of the GNU Lesser General Public License as
# published by the Free Software Foundation; either version 2.1 of the
# licence, or (at your option) any later version.
#
# This is distributed in the hope that it will be useful, but WITHOUT
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
# FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public
# License for more details.
#
# You should have received a copy of the GNU Lesser General Public License
# along with this library; if not, see <http://www.gnu.org/licenses/>.
#
# SPDX-License-Identifier: LGPL-2.1-or-later

# shellcheck shell=bash

# Check for bash
[ -z "$BASH_VERSION" ] && return

####################################################################################################


__gdbus() {
    local gdbus_binary
    gdbus_binary="$1"

    local IFS=$'\n'
    local cur
    cur="$(_get_cword :)"

    local suggestions
    suggestions="$("$gdbus_binary" complete "${COMP_LINE}" "${COMP_POINT}")"
    # shellcheck disable=SC2207
    COMPREPLY=($(compgen -W "$suggestions" -- "$cur"))

    # Remove colon-word prefix from COMPREPLY items
    case "$cur" in
        *:*)
            case "$COMP_WORDBREAKS" in
                *:*)
                    local colon_word=${cur%"${cur##*:}"}
                    local i=${#COMPREPLY[*]}
                    while [ $((--i)) -ge 0 ]; do
                        COMPREPLY[i]=${COMPREPLY[i]#"$colon_word"}
                    done
                    ;;
            esac
            ;;
    esac
}

####################################################################################################

complete -o nospace -F __gdbus gdbus