# bash-completion for tarsnap-keymgmt
#
# Copyright (c) 2015-2025 Tarsnap Backup Inc.
# Copyright (c) 2009-2012 Andreas Olsson
#
# Permission is hereby granted, free of charge, to any person
# obtaining a copy of this software and associated documentation
# files (the "Software"), to deal in the Software without
# restriction, including without limitation the rights to use,
# copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the
# Software is furnished to do so, subject to the following
# conditions:
#
# The above copyright notice and this permission notice shall be
# included in all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
# OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
# NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
# HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
# WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
# FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
# OTHER DEALINGS IN THE SOFTWARE.


shopt -s extglob

_tarsnap_keymgmt ()
{
	local cur prev wfilearg wotherarg longopts shortopts options
	COMPREPLAY=()
	cur="${COMP_WORDS[COMP_CWORD]}"
	prev="${COMP_WORDS[COMP_CWORD-1]}"

	# These options will be completed by the path to a filename.
	wfilearg="--outkeyfile"

	# These options require a non-completable argument.
	# They won't be completed at all.
	wotherarg="--passphrase-mem"

	# Available long options
	longopts="--outkeyfile --nuke --passphrased --passphrase-mem \
		--print-key-id --print-key-permissions --version"

	# Available short options
	shortopts="-r -w -d"

	options=${longopts}" "${shortopts}

	case "$prev" in
		@($wfilearg))
			_filedir
			return 0
			;;

		@($wotherarg))
			return 0
			;;

	esac

	if [[ ${cur} == -* ]]
	then
		COMPREPLY=( $(compgen -W "${options}" -- ${cur}) )
		return 0
	else
		_filedir
		return 0
	fi
}

complete -F _tarsnap_keymgmt -o filenames tarsnap-keymgmt
