# Bash completion for rebase-helper
# Copyright (C) 2013-2019 Red Hat, Inc.
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program 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 General Public License for more details.
#
# You should have received a copy of the GNU General Public License along
# with this program; if not, write to the Free Software Foundation, Inc.,
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
#
# Authors: Petr Hráček <phracek@redhat.com>
#          Tomáš Hozza <thozza@redhat.com>
#          Nikola Forró <nforro@redhat.com>
#          František Nečas <fifinecas@seznam.cz>

shopt -s extglob

_rebase-helper()
{
    local rh_options=("-h --help" "--version" "-v --verbose" "--color" "--background" "--results-dir" "--workspace-dir" "--buildtool" "--srpm-buildtool" "--pkgcomparetool" "--outputtool" "--versioneer" "--versioneer-blacklist" "--spec-hook-blacklist" "--build-log-hook-blacklist" "--bugzilla-id" "--non-interactive" "--favor-on-conflict" "--not-download-sources" "-w --keep-workspace" "--apply-changes" "--disable-inapplicable-patches" "--skip-version-check" "--update-sources" "--skip-upload" "--force-build-log-hooks" "--builds-nowait" "--build-tasks" "--builder-options" "--srpm-builder-options" "--changelog-entry" "--config-file" "-D --define" "--copr-project-permanent" "--copr-project-frontpage" "--copr-chroots" "--get-old-build-from-koji" "--replace-old-version-with-macro")
    local rh_choices=("" "" "" "always never auto" "dark light auto" "" "" "copr koji mock rpmbuild" "mock rpmbuild" "abipkgdiff csmock licensecheck pkgdiff rpmdiff sonamecheck" "json text" "anitya cpan hackage npmjs pypi rubygems" "anitya cpan hackage npmjs pypi rubygems" "commit-hash-updater escape-macros paths-to-rpm-macros pypi-url-fix replace-old-version ruby-helper typo-fix" "files" "" "" "downstream upstream off" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "")
    local rh_extensions=@(tar.xz|tar.bz2|bz2|tar.gz|tgz|tar|crate|zip|gem)

    local cur prev idx

    _get_comp_words_by_ref -n = cur prev
    _split_longopt

    # complete choices, if the last word is known option which has some
    for idx in "${!rh_options[@]}"; do
        if [[ "${rh_options[idx]}" == *"${prev}"* ]]; then
            if [[ -n "${rh_choices[idx]}" ]]; then
                COMPREPLY=( $(compgen -W "${rh_choices[idx]}" -- "${cur}") )
                return 0
            fi
            break
        fi
    done

    # complete options
    COMPREPLY=( $(compgen -W "$(echo "${rh_options[@]}")" -- "${cur}") )

    # add recognized archive extensions
    _filedir "${rh_extensions}"

    return 0
}

complete -F _rebase-helper rebase-helper
