# bash-completion for tarsnap
#
# 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 ()
{
	local cur prev wfilearg wpatharg 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="--archive-names|--configfile|--csv-file|--keyfile|
		  |--newer-than|--newer-mtime-than|--snaptime|-T|-X"

	# These options will be completed by the path to a directory.
	wpatharg="-C|--cachedir"

	# These options require a non-completable argument.
	# They won't be completed at all.
	wotherarg="--checkpoint-bytes|--creationtime|--disk-pause|
		   |--exclude|-f|--include|--maxbw|--maxbw-rate|
		   |--maxbw-rate-down|--maxbw-rate-up|--newer|
		   |--newer-mtime|--passphrase|--progress-bytes|-s|
		   |--strip-components"

	# Available long options
	longopts="--aggressive-networking --archive-names --cachedir \
		  --check-links --checkpoint-bytes --chroot --configfile \
		  --creationtime --csv-file --disk-pause --dry-run \
		  --dry-run-metadata --dump-config --exclude --fast-read \
		  --force-resources --fsck --fsck-prune --hashes \
		  --humanize-numbers --include --initialize-cachedir \
		  --insane-filesystems --iso-dates --keep-going \
		  --keep-newer-files --keyfile --list-archives --lowmem \
		  --maxbw --maxbw-rate --maxbw-rate-down --maxbw-rate-up \
		  --newer --newer-mtime --newer-than --newer-mtime-than \
		  --no-aggressive-networking --no-config-exclude \
		  --no-config-include --no-default-config \
		  --no-disk-pause --no-force-resources \
		  --no-humanize-numbers --no-insane-filesystems \
		  --no-iso-dates --no-maxbw --no-maxbw-rate-down \
		  --no-maxbw-rate-up --no-noatime --no-nodump \
		  --no-print-stats --no-progress-bytes --no-quiet \
		  --no-retry-forever --no-snaptime --no-store-atime \
		  --no-totals --noatime --nodump --noisy-warnings \
		  --normalmem --nuke --null --null-input --null-output \
		  --numeric-owner --one-file-system --passphrase \
		  --print-stats --progress-bytes --quiet --recover \
		  --resume-extract --retry-forever --snaptime \
		  --store-atime --strip-components --totals \
		  --verify-config --version --verylowmem"

	# Available short options
	shortopts="-c -d -t -x -r -C -f -H -h -I -k -L -l -m -n -O -o -P \
		   -p -q -S -s -T -U -v -w -X"

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

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

		@($wpatharg))
			_filedir -d
			return 0
			;;

		@($wotherarg))
			return 0
			;;

	esac

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

complete -F _tarsnap -o filenames tarsnap
