# What ends a word while we complete.
#
# A comma does, because lists are written "a,b,c" (see "<[name]>" in
# otpme/lib/help/__init__.py). Without it the whole list is one word and
# bash has to be offered "add:user,edit", "add:user,delete" and so on,
# which reads as a repetition of what was just typed.
#
# A colon does not, although bash says so by default. An ACL carries
# them -- "view:attribute", "+user:edit" -- and bash would replace only
# what stands behind the last one while the candidate brings the whole
# ACL along: "view:" plus "view:attribute" makes "view:view:attribute".
# Nothing else in an OTPme command line has a colon in it.
#
# Both have to be global. COMP_WORDS is split before the function below
# is called, so setting them in there would come too late.
COMP_WORDBREAKS="${COMP_WORDBREAKS//:/},"

_otpme() {
	local COUNT="0"
	export _OTPME_OPTS=""
	export _OTPME_REALM_OPT=""
	export _OTPME_SITE_OPT=""
	for WORD in ${COMP_WORDS[@]} ; do
		if [ "$COUNT" -gt "0" ] ; then
			if [[ $WORD == -* ]] ; then
				if [[ $WORD == -s ]] ; then
					_OTPME_SITE_OPT="-s ${COMP_WORDS[$[$COUNT+1]]}"
				fi
				if [[ $WORD == -r ]] ; then
					_OTPME_REALM_OPT=" -r${COMP_WORDS[$[$COUNT+1]]}"
				fi
				# set --api command line switch for all commands if given
				if [[ $WORD == --api ]] ; then
					_OTPME_OPTS="$_OTPME_OPTS$IFS--api"
				fi
				# set --socket command line switch for all commands if given
				if [[ $WORD == --socket ]] ; then
					_OTPME_OPTS="$_OTPME_OPTS$IFS--socket"
				fi
				# set --type command line switch for all commands if given
				if [[ $WORD == --type ]] ; then
					_OTPME_TYPE="${COMP_WORDS[$[$COUNT+1]]}"
					if [ "$_OTPME_TYPE" != "" ] ; then
						_OTPME_OPTS="$_OTPME_OPTS$IFS--type$IFS$_OTPME_TYPE"
					fi
				fi
			fi
		fi
		COUNT="$[$COUNT+1]"
	done

	command=${COMP_WORDS[0]}
	_COMP_WORDS="${COMP_WORDS[@]}"
	_COMP_CWORD="$COMP_CWORD"
	cur=${COMP_WORDS[COMP_CWORD]}

	#echo ${COMP_WORDS[@]}
	#echo $COMP_CWORD
	#echo $COMP_LIST
	#echo $COMP_OPTS
	#cur=${COMP_WORDS[COMP_CWORD]}
	#test="$command $_OTPME_OPTS --compgen --comp-words \"$_COMP_WORDS\" --comp-cword \"$_COMP_CWORD\" --cur \"$cur\""
	#echo $test
	#COMP_CMD="$command $_OTPME_OPTS $_OTPME_REALM_OPT $_OTPME_SITE_OPT --compgen --comp-words \"$_COMP_WORDS\" --comp-cword "$_COMP_CWORD" --cur "$cur""
	#echo $COMP_CMD > /tmp/log
	COMP_LIST="$($command -dee $_OTPME_OPTS $_OTPME_REALM_OPT $_OTPME_SITE_OPT --compgen --comp-words "$_COMP_WORDS" --comp-cword "$_COMP_CWORD" --cur "$cur")"
	#COMP_LIST="$($command $_OTPME_OPTS --compgen --comp-words "$_COMP_WORDS" --comp-cword "$_COMP_CWORD" --cur "$cur")"
	if [ "$COMP_LIST" = "" ] ; then
		COMPREPLY=()
	else
		COMPREPLY=( `compgen -W "$COMP_LIST" -- $cur` )
	fi
	#compopt -o default
	return 0
}


complete -o bashdefault -o default -F _otpme otpme-shell otpme-controld otpme-agent otpme-user otpme-group otpme-accessgroup otpme-client otpme-realm otpme-token otpme-session otpme-cluster otpme-tool otpme-auth otpme-site otpme-node otpme-host otpme-unit otpme-role otpme-ca otpme-script otpme-policy otpme-resolver otpme-dictionary otpme-trash otpme-deploy otpme-inventory otpme-share otpme-job otpme-pool otpme-device otpme-scope otpme-vlan otpme-backup
