# mdvsys(1) completion
#
_cooker_packages()
{
	COMPREPLY=( $( compgen -W '$(svn ls \
		svn+ssh://svn.mandriva.com/svn/packages/cooker \
		| sed -e "s|/$||" )' -- $cur ) )
}

_mdvsys_actions()
{
	COMPREPLY=( $( compgen -W 'import create checkout co update info log \
		tag submit extract sync commit ci build strip mass-update \
		help' -- $cur ) )
}

_mdvsys()
{
	local cur prev command options i

	COMPREPLY=()
	cur=${COMP_WORDS[COMP_CWORD]}

	if [[ $COMP_CWORD -eq 1 ]] ; then
		_mdvsys_actions
	else
		prev=${COMP_WORDS[COMP_CWORD-1]}

		case "$prev" in
			-@(c|-config)) 
				_filedir
				return 0
				;;
		esac

		command=${COMP_WORDS[1]}

		if [[ "$cur" == -* ]]; then
			# possible options for the command
			case $command in
				@(import|create))
					options="--revision --distribution \
						--branch --message --nocommit"
					;;
				@(checkout|co|info|log))
					options="--revision --distribution \
						--branch"
					;;
				update)
					options="--revision --distribution \
						--branch --release \
						--spec-line-expression \
						--keep-on-failure --message \
						--target --nocommit --nosubmit"
					;;
				mass-update)
					options="--include --exclude \
						--keep-on-failure --nocommit \
						--nosubmit"
					;;
				tag)
					options="--revision"
					;;
				submit)
					options="--revision --distribution \
						--branch --target"
					;;
				extract)
					options="--revision --distribution \
						--branch --destdir --noprefix"
					;;
				@(commit|ci))
					options="--sync --message"
					;;
			esac
			options="$options --verbose -v --config -c --help"
			COMPREPLY=( $( compgen -W "$options" -- $cur ) )
		else
			case $command in
				help)
					_mdvsys_actions
					return 0
					;;
				import)
					_filedir 'src.rpm'
					return 0
					;;
				@(create|checkout|co|update|info|log|tag|submit|extract))
					_cooker_packages
					return 0
					;;
				@(sync|commit|ci))
					_filedir -d
					return 0
					;;
				@(build|strip))
					_filedir 'spec'
					return 0
					;;
			esac
		fi
	fi

}
complete -F _mdvsys $filenames mdvsys 
