# bash-completion for univention-config-registry
have univention-config-registry && {

_uniucr () {
	local cur opts
	COMPREPLY=()
	cur=`_get_cword`	# current word till cursor position
	#cur="$2"	# current word as split by readline
	#cur="${COMP_WORDS[COMP_CWORD]}" # current full word

	if [ "$COMP_CWORD" -eq 1 ]
	then
		COMPREPLY=( $(compgen -W "set get unset commit search filter shell dump --help --version --shell --keys-only" -- "${cur}") )
		return 0
	fi

	case "${COMP_WORDS[1]}" in
		--keys-only)
			if [[ "${COMP_WORDS[COMP_CWORD-1]}" != "search"  ]]; then
				COMPREPLY=( $(compgen -W "search" -- "${cur}" ) )
			fi
			;;
		--shell)
			if [[ "${COMP_WORDS[COMP_CWORD-1]}" != "search"  ]]; then
				COMPREPLY=( $(compgen -W "search" -- "${cur}" ) )
			fi
			;;
		search)
			if [[ "${COMP_WORDS[COMP_CWORD-1]}" != "--key"  ]] && [[ "${COMP_WORDS[COMP_CWORD-1]}" != "--value"  ]]; then
				COMPREPLY=( $(compgen -W "--key --value" -- "${cur}") )
			fi
			;;
		get)
			COMPREPLY=( $(compgen -W "$(/usr/sbin/univention-config-registry dump | sed -e 's/: .*$//')" -- "${cur}") )
			;;
		unset)
			COMPREPLY=( $(compgen -W "--forced $(/usr/sbin/univention-config-registry dump | sed -e 's/: .*$//')" -- "${cur}") )
			;;
		shell)
			COMPREPLY=( $(compgen -W "$(/usr/sbin/univention-config-registry dump | sed -e 's/: .*$//')" -- "${cur}") )
			;;
		set)
			# switch IFS to newline so UCR values containing a whitespace won't get split
			local IFS=$'\n'
			if [[ "$cur" == *=* ]]; then
				opts="$( /usr/sbin/univention-config-registry get "${cur%%=*}" )"
				COMPREPLY=( $(IFS=$'\n' compgen -P\" -S\" -W "$opts" -- "${cur#*=}") )
			else
				# Don't evaluate the ucr dump while using the tab completion and put it into single quotes
				# instead of double quotes, otherwise the value will be evaluated during the new value is set:
				#  ucr set xyz='$(cat /etc/foo)' instead of ucr set xyz="$(cat /etc/foo)"
				# See https://forge.univention.org/bugzilla/show_bug.cgi?id=27573
				COMPREPLY=( $(IFS=$'\n' compgen -W "--forced
 $(/usr/sbin/univention-config-registry dump | sed -re "s/: (.*)$/=\\\\\'\\'\1\\'\\\\\'/")" -- "${cur}") )
			fi
			;;
		commit)
			opts=$(sed -nre 's/(Multif|F)ile: /\//p' /etc/univention/templates/info/*.info)
			COMPREPLY=( $(compgen -W "$opts" -- "${cur}") )
			;;
	esac
	return 0
}
complete -F _uniucr ucr univention-config-registry

}
# vim:set ft=sh:
