# -*- mode: shell-script; sh-basic-offset: 8; indent-tabs-mode: t -*-
# ex: ts=8 sw=8 noet filetype=sh
#
# bash completion for pkgconfig

have pkg-config &&
_pkg_config()
{
	local cur prev split=false

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

	_split_longopt && split=true

	case $prev in
		--@(?(define-)variable|@(atleast?(-pkgconfig)|exact|max)-version))
			# argument required but no completions available
			return 0
			;;
		-\?|--help|--version|--usage)
			# all other arguments are noop with these
			return 0
			;;
	esac

	$split && return 0

	if [[ "$cur" == -* ]]; then
		# return list of available options
		COMPREPLY=( $( compgen -W '--version --modversion \
			--atleast-pkgconfig-version --libs --static \
			--short-errors --libs-only-l --libs-only-other \
			--libs-only-L --cflags --cflags-only-I \
			--cflags-only-other --variable --define-variable \
			--exists --uninstalled --atleast-version \
			--exact-version --max-version --list-all --debug \
			--print-errors --silence-errors --errors-to-stdout \
			--print-provides --print-requires -? --help --usage' \
			-- $cur) )
	else
		COMPREPLY=( $( compgen -W "$( pkg-config --list-all \
				2>/dev/null | awk '{print $1}' )" -- $cur ) )
	fi
} &&
complete -F _pkg_config pkg-config
