# antlink completion                                          -*- shell-script -*-
#
# This file is part of antlink.
#
# Copyright 2016 (C) John Heidemann <johnh@isi.edu>
#
# 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, version 2 of the License.
#
# 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
#

#
# this code followed the "documentation" at
# http://eli.thegreenplace.net/2013/12/26/adding-bash-completion-for-your-own-tools-an-example-for-pss
# and
# http://stackoverflow.com/questions/17879322/how-do-i-autocomplete-nested-muti-level-subcommands
# which lead to
# https://debian-administration.org/article/317/An_introduction_to_bash_completion_part_2
#
# Thanks, all!
#

_antlink()
{
    local cur prev

    cur="${COMP_WORDS[COMP_CWORD]}"
    prev="${COMP_WORDS[COMP_CWORD-1]}"

    # Cache our subcommands, so we don't list them here but also don't compute them every time.
    _antlink_subcommands=${_antlink_subcommands:-`antlink listsubcommands`}
    export _antlink_subcommands

    case ${COMP_CWORD} in
	    1)
		    COMPREPLY=( $(compgen -W "$_antlink_subcommands" ${cur}) )
		    ;;
	    *)
		    COMPREPLY=( $(compgen -f ${cur}) )
		    ;;
    esac
    return 0
} &&
complete -F _antlink antlink

