# -*- shell-script -*-
#
# Bash tab auto-completion rules for git-buildpackage.
#
# Copyright (C) 2010  Siegfried-Angel Gevatter Pujals <siegfried@gevatter.com>
# Copyright (C) 2010  Guido Guenther <agx@sigxcpu.org>
#
# Distributed under the GNU General Public License, version 2.0.

_gbp_branches ()
{
     [ -d .git ] || return 0
     git  for-each-ref --format="%(refname:short)" refs/heads
}


_gbp_tags ()
{
     [ -d .git ] || return 0
     git  for-each-ref --format="%(refname:short)" refs/tags
}


_gbp_options ()
{
    $1 --help | sed -ne  's/^ \+\(\(\-[a-z]\), \)\?\(\-\-[a-z\-]\+\=\?\).*/\2 \3/p'
}


_gbp_comp ()
{
    local cur="${COMP_WORDS[COMP_CWORD]}"
    local prev="${COMP_WORDS[COMP_CWORD - 1]}"
    local options=$1
    local branch_opts=$2
    local tag_opts=$3
    local tristate_opts=$4

# COMPREPLY considers '=' as a word. For $prev we prefer the word before the actual "="
    if [[ "$prev" == "=" ]]; then
        prev="${COMP_WORDS[COMP_CWORD - 2]}"
    elif [[ "$cur" == "=" ]]; then
        cur=""
    fi

    if [[ "${branch_opts}" == *$prev* ]]; then
	local refs=$(_gbp_branches)
        COMPREPLY=( $(compgen -W "$refs" -- $cur ) )
        return 0
    fi

    if [[ "${tag_opts}" == *$prev* ]]; then
	local refs=$(_gbp_tags)
        COMPREPLY=( $(compgen -W "$refs" -- $cur ) )
        return 0
    fi

    if [[ "${tristate_opts}" == *$prev* ]]; then
        COMPREPLY=( $(compgen -W 'on off auto' -- $cur ) )
        return 0
    fi

    if [[ "${cbdist_opts}" == *$prev* ]]; then
        local BASE="/var/cache/pbuilder/base-"
        COMPREPLY=( $( compgen -o dirnames -G "${BASE}${cur}*.cow" \
                       | sed -e "s,${BASE}\(.*\)\.cow,\1,g" ) )
        return 0
    fi

    # separate opts by tab so we can append a space to all options not ending
    # with an equal sign
    tab_opts=$(echo "$options" | sed -e 's/ \+/\t/g' -e 's/[^=]$/& /g')
    type compopt &>/dev/null && compopt -o nospace
    local IFS=$'\t\n'
    COMPREPLY=($(compgen -W "$tab_opts" -- $cur))
}


have git-buildpackage &&
_git_buildpackage()
{
    local options=$(_gbp_options git-buildpackage)
    local branch_opts="--git-debian-branch\= --git-upstream-branch\="
    local tag_opts="--git-debian-tag\= --git-upstream-tag\="
    local tristate_opts="--git-color\= --git-notify\="
    local cbdist_opts="--git-dist\="

    _gbp_comp "$options" "$branch_opts" "$tag_opts" "$tristate_opts" \
              "$cbdist_opts"
}
[ "${have:-}" ] && complete -F _git_buildpackage -o default git-buildpackage


have git-dch &&
_git_dch ()
{
    local options=$(_gbp_options git-dch)
    local branch_opts="--debian-branch\="
    local tag_opts="--debian-tag\= --upstream-tag\="
    local tristate_opts="--color\="

    _gbp_comp "$options" "$branch_opts" "$tag_opts" "$tristate_opts"
}
[ "${have:-}" ] && complete -F _git_dch -o default git-dch


have git-import-orig &&
_git_import_orig ()
{
    local options=$(_gbp_options git-import-orig)
    local branch_opts="--debian-branch\= --upstream-branch\="
    local tag_opts="--debian-tag\= --upstream-tag\="
    local tristate_opts="--color\="

    _gbp_comp "$options" "$branch_opts" "$tag_opts" "$tristate_opts"
}
[ "${have:-}" ] && complete -F _git_import_orig -o default git-import-orig


have git-import-dsc &&
_git_import_dsc ()
{
    local options=$(_gbp_options git-import-dsc)
    local branch_opts="--debian-branch\= --upstream-branch\="
    local tag_opts="--debian-tag\= --upstream-tag\="
    local tristate_opts="--color\="

    _gbp_comp "$options" "$branch_opts" "$tag_opts" "$tristate_opts"
}
[ "${have:-}" ] && complete -F _git_import_dsc -o default git-import-dsc

have gbp-pq &&
_gbp_pq ()
{
    local options=$(_gbp_options gbp-pq)
    options="$options export import rebase drop apply switch"
    local tristate_opts="--color\="

    _gbp_comp "$options" "" "" "$tristate_opts"
}
[ "${have:-}" ] && complete -F _gbp_pq -o default gbp-pq

have gbp-pull &&
_gbp_pull ()
{
    local options=$(_gbp_options gbp-pull)
    local branch_opts="--debian-branch\= --upstream-branch\="
    local tag_opts="--debian-tag\= --upstream-tag\="
    local tristate_opts="--color\="

    _gbp_comp "$options" "$branch_opts" "$tag_opts" "$tristate_opts"
}
[ "${have:-}" ] && complete -F _gbp_pull -o default gbp-pull

have gbp-clone &&
_gbp_clone ()
{
    local options=$(_gbp_options gbp-clone)
    local branch_opts="--debian-branch\= --upstream-branch\="
    local tag_opts="--debian-tag\= --upstream-tag\="
    local tristate_opts="--color\="

    _gbp_comp "$options" "$branch_opts" "$tag_opts" "$tristate_opts"
}
[ "${have:-}" ] && complete -F _gbp_clone -o default gbp-clone

have gbp-create-remote-repo &&
_gbp_create_remote_repo ()
{
    local options=$(_gbp_options gbp-create-remote-repo)
    local branch_opts="--debian-branch\= --upstream-branch\="
    local tristate_opts="--color\="

    _gbp_comp "$options" "$branch_opts" "" "$tristate_opts"
}
[ "${have:-}" ] && complete -F _gbp_create_remote_repo -o default gbp-create-remote-repo
