# dput/bash-completion
# Programmable Bash command completion for Dput commands.
# See the Bash manual “Programmable Completion” section.
#
# This is free software, and you are welcome to redistribute it under
# certain conditions; see the end of this file for copyright
# information, grant of license, and disclaimer of warranty.

have dput &&
_dput()
{
    local cur prev options paroptions special i delayed_options hosts

    COMPREPLY=()
    cur=${COMP_WORDS[COMP_CWORD]}
    prev=${COMP_WORDS[COMP_CWORD-1]}
    options='-c --config -d --debug -D --dinstall -f --force -h --help \
             -H --host-list -l --lintian -o --check-only -p --print \
             -P --passive -s --simulate -u --unchecked -e --delayed \
             -v --version -V --check-version'

    hosts=$(
        {
            grep "^\[.*\]" $HOME/.dput.cf 2> /dev/null | tr -d [] || /bin/true
            grep "^\[.*\]" /etc/dput.cf 2> /dev/null | tr -d [] || /bin/true
        } | grep -v '^DEFAULT$' | sort -u)

    paroptions="$options $hosts"

    case $prev in
        --delayed|-e)
            delayed_options='0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15'
            COMPREPLY=( $( compgen -W "$delayed_options" | grep ^$cur ) )
            ;;
        --config|-c)
            COMPREPLY=( $( compgen -o filenames -G "$cur*" ) )
            ;;
        *)
            COMPREPLY=( $(
                    compgen -G "${cur}*.changes"
                    compgen -G "${cur}*.asc"
                    compgen -G "${cur}*.sig"
                    compgen -W "$paroptions" | grep "^$cur"
                ) )
            ;;
    esac

    return 0

}
[ "$have" ] && complete -F _dput -o filenames -o plusdirs dput


# Copyright © 2015–2016 Ben Finney <bignose@debian.org>
# Copyright © 2002 Roland Mas <lolando@debian.org>
#
# This is free software: you may copy, modify, and/or distribute this work
# under the terms of the GNU General Public License as published by the
# Free Software Foundation; version 3 of that license or any later version.
# No warranty expressed or implied. See the file ‘LICENSE.GPL-3’ for details.

# Local variables:
# coding: utf-8
# mode: sh
# End:
# vim: fileencoding=utf-8 filetype=sh :
