# 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//:/},"

# The global options that take a value (see register_global_opt() in
# otpme/lib/help/__init__.py). Their value is not the subcommand.
_OTPME_GLOBAL_VALUE_OPTS=" -r -s --type -t -tt --reneg-timeout -c --changelog -u --use-ssh-agent --use-smartcard --impersonate-token --auth-token --log-filter --lock-wait-timeout --job-timeout --debug-daemons --debug-profile-sort --debug-profile-print-callers --debug-func-caches --debug-func-names --debug-func-start --debug-users --debug-timing-limit "

_otpme() {
	local COUNT="1"
	local WORD
	export _OTPME_OPTS=""
	export _OTPME_REALM_OPT=""
	export _OTPME_SITE_OPT=""
	# The global options stand before the subcommand, and what stands
	# behind it is the subcommand's: the "-r" of "add_acl -r" means
	# recursive, not a realm.
	while [ "$COUNT" -lt "${#COMP_WORDS[@]}" ] ; do
		WORD="${COMP_WORDS[$COUNT]}"
		if [[ $WORD != -* ]] ; then
			break
		fi
		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
		if [[ $_OTPME_GLOBAL_VALUE_OPTS == *" $WORD "* ]] ; then
			COUNT="$[$COUNT+1]"
			# A list "a,b,c" is five words, the commas among them.
			while [ "${COMP_WORDS[$[$COUNT+1]]}" = "," ] ; do
				COUNT="$[$COUNT+2]"
			done
		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
	# A unit ("passwords/") or the owner of a token ("user/") is where
	# the path goes on. Bash would put a space behind it.
	if [ "${#COMPREPLY[@]}" -eq 1 ] && [[ ${COMPREPLY[0]} == */ ]] ; then
		compopt -o nospace
	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-pass otpme-backup
