# put this in your bashrc for bash tab completion with cdcd
# $ cat cdcd-bashrc >> ~/.bashrc

ismember ()
{
	local elm="${1}"; shift
	local pivot

	for pivot in ${*}
	do
		if [ "${pivot}" = "${elm}" ]
		then
			return 0
		fi
	done

	return 1
}

 _cdcd_complete_func ()
{
	cur="${COMP_WORDS[COMP_CWORD]}"
	first="${COMP_WORDS[1]}"
	second="${COMP_WORDS[2]}"

	commands="$(cdcd help | grep -v 'For more')"
	commands="${commands#Commands: }"
	commands="${commands//,/}"
	commands="${commands/./}"

	if ismember "${first}" ${commands}
	then
		COMPREPLY=""

		return 0
	fi

	case "${first}" in
	*)
		COMPREPLY="($(compgen -W "${commands}" ${cur}))"

		return 0
		;;
	esac
}

complete -F _cdcd_complete_func cdcd
