#-------------------------------------------------------------------------------
# pacKAGE oRGANZIER bash completion
# Copyright 2005 Christian Schneider <mail@chrschn.de>
# License: GNU GPL v2 or later
#-------------------------------------------------------------------------------
#
# This file provides bash completion support for paco.
# 
# It should be copied into directory /etc/bash_completion.d/ to be operative.
# Also, the file /etc/bash_completion must exist, and ~/.bashrc has to contain
# some additional commands to activate the bash completion support, more or less
# thusly:
#
# 8<---------------------------------------
#	# Enable bash completion
#	if [ -f /etc/bash_completion ]; then
#		source /etc/bash_completion
#	fi
# --------------------------------------->8
#
# Paco bash completion means that, for example, if you type:
#
#	$ paco <TAB> <TAB>
#
# bash will print a list of all packages logged in the paco database.
# And if you type:
#
#	$ paco -- <TAB> <TAB>
#
# bash will print a list of all available long options for paco.
#
#-------------------------------------------------------------------------------
#
# For more information about programmable shell completion, read the
# Ian Macdonald's excellent guide "Working more productively with bash 2.x/3.x":
#
#	--> http://www.caliban.org/bash/index.shtml
#
#-------------------------------------------------------------------------------
# vim:ts=4:ft=sh:


have paco &&
_paco()
{
	local prev cur pkgs longopts

	COMPREPLY=()
	prev=${COMP_WORDS[COMP_CWORD-1]}
	cur=${COMP_WORDS[COMP_CWORD]}

	# long options:
	longopts='--logdir --expand --all --verbose --help --version --update \
		--unlog --one-column --block-size --kilobytes --date --files \
		--missing-files --shared --who-shares --size --missing-size \
		--sort --reverse --total --symlinks --no-package-name --query \
		--info --configure-options --variable --remove --remove-shared \
		--batch --unlog --skip --log --package --dirname --append \
		--include --exclude'

	# shrt options:
	shortopts='-L -x -a -v -h -u -U -1 -b -k -d -f -m -c -w -s -n -R -t \
		-y -z -q -i -o -V -r -U -e -l -p -D -+ -I -E'

	# get a list of all available packages
	pkgs=$(paco -a)

	# complete on all options
	if [[ "$cur" == -* ]]; then
		COMPREPLY=( $(compgen -W "$shortopts $longopts" -- $cur) )
		return 0
	fi

	# first parameter on line and not an option?
	if [ $COMP_CWORD -eq 1 ] && [[ "$cur" != -* ]]; then
		COMPREPLY=( $(compgen -W "$pkgs" $cur) )
		return 0
	fi
	

	# handle completion based on previous long option
	if [ $COMP_CWORD -ge 1 ] && [[ "$prev" == --* ]]; then

		# expand according to prev parameter
		case "${prev#--}" in
			update | date | files | missing-files | who-shares | \
			size | total | info | configure-options | remove | \
			variable | remove-shared | unlog | package) 
				COMPREPLY=( $(compgen -W "$pkgs" $cur) )
				return 0
				;;

			query)
				_filedir
				return 0
				;;		

			logdir | skip | include | exclude) 
				_filedir -d
				return 0
				;;
		esac

		# does the previous parameter contain an option for packages?
		if [ -z "${prev/-*p*}" ]; then		
			COMPREPLY=( $(compgen -W "$pkgs" $cur) )
			return 0
		fi
	fi

	# handle completion based on previous short option
	if [ $COMP_CWORD -ge 1 ] && [[ "$prev" == -* ]]; then
			 
		# expand according to prev parameter
		case "${prev#-}" in
			*u* | *d* | *f* | *F* | *m* | *M* | *c* | *C* | *s* | \
			*t* | *i* | *o* | *V* | *r* | *U* | *p*)
				COMPREPLY=( $(compgen -W "$pkgs" $cur) )
				return 0
				;;

			*q*)
				_filedir
				return 0
				;;

			*L* | *e* | *I* | *E*)
				_filedir -d
				return 0
				;;
		esac
	fi

	return 0
}
[ "$have" ] && complete -F _paco paco



