# bash completion for abb

_have abb && {
	_abb() {
		local commands cur prev words cword specs IFS sifs=$IFS
		local rpmbuildopts ncur varvalue varvalue1 branch updatetypes
		local opts opts_view opts_status opts_restore opts_build
		local opts_publish opts_store opts_clone opts_new

		COMPREPLY=()

		_get_comp_words_by_ref -n '=' cur prev words cword

		#echo "cword=$cword cur='$cur' '${words[@]}'" >> /tmp/log

		specs=$(basename -s .spec $(ls *.spec 2>/dev/null) 2>/dev/null)

		commands="build
			clone
			new
			publish
			restore
			send
			status
			store
			vars
			view"

		rpmbuildopts="-ba
			-bb
			-bc
			-bi
			-bl
			-bp
			-bs
			--short-circuit"

		opts="proto= server= owner= branch= remote= auth= abfyml= buildlog= \
			restore= project= rows= refresh= defaultspec= \
			arch= platform= updatetype= commit= publish= repos= reposave="

		opts_send="proto= server= owner= branch= auth= project= arch= platform= \
			updatetype= commit= publish= repos= reposave="

		opts_view="proto= server= owner= auth= project="

		opts_status="proto= server= owner= auth= project= rows= refresh="

		opts_restore="abfyml= project="

		opts_build="proto= server= owner= branch= remote= auth= abfyml= buildlog= \
			restore= project="

		opts_publish="proto= server= auth="

		opts_store="auth= abfyml="

		opts_clone="proto= server= owner= branch= remote= auth= abfyml= \
			restore= project="

		opts_new="proto= server= owner= remote= auth= \
			project= defaultspec="

		updatetypes="security bugfix enhancement recommended newpackage"

		if [ $cword -eq 1 ]; then
			IFS=$'\t'$'\n'
			COMPREPLY=( $( compgen -W "$commands" -S ' ' -- "$cur" ) )
			IFS=$sifs
			return 0
		fi

		if [[ "$cur" == -* ]]; then
			IFS=$'\t'$'\n'
			COMPREPLY=( $( compgen -W "$rpmbuildopts" -S ' ' -- "$cur" ) )
			IFS=$sifs
			return 0
		fi

		if [[ "$cur" == *=* ]]; then
			ncur=${cur#*=}
			varvalue="$(abb vars ${cur%=*})"
			varvalue="${varvalue[@]##*=}"
			case "$cur" in
				proto=*)
					COMPREPLY=( $( compgen -W "http https $varvalue" -- "$ncur" ) )
					;;
				server=*)
					COMPREPLY=( $( compgen -W "abf.rosa.ru $varvalue" -- "$ncur" ) )
					;;
				owner=*)
					COMPREPLY=( $( compgen -W "import npp_team tigro $varvalue" -- "$ncur" ) )
					;;
				branch=*)
					COMPREPLY=( $( compgen -W "master rosa2012lts rosa2012.1 import_cooker import_mandriva2011 $varvalue" -- "$ncur" ) )
					;;
				remote=*)
					COMPREPLY=( $( compgen -W "origin $varvalue" -- "$ncur" ) )
					;;
				auth=*)
					COMPREPLY=( $( compgen -W "$varvalue" -- "$ncur" ) )
					;;
				abfyml=*)
					COMPREPLY=( $( compgen -W ".abf.yml $varvalue" -- "$ncur" ) )
					;;
				buildlog=*)
					COMPREPLY=( $( compgen -W "yes no $varvalue" -- "$ncur" ) )
					;;
				restore=*)
					COMPREPLY=( $( compgen -W "always ask none $varvalue" -- "$ncur" ) )
					;;
				project=*)
					COMPREPLY=( $( compgen -d -W "$specs $varvalue" -- "$cur" ) )
					;;
				rows=*)
					COMPREPLY=( $( compgen -W "4 $varvalue" -- "$ncur" ) )
					;;
				refresh=*)
					COMPREPLY=( $( compgen -W "120 $varvalue" -- "$ncur" ) )
					;;
				defaultspec=*)
					COMPREPLY=( $( compgen -W "/usr/share/abb/spek.skel $varvalue" -- "$ncur" ) )
					;;
				arch=*)
					COMPREPLY=( $( compgen -W "i586,x86_64 i586 x86_64 $varvalue" -- "$ncur" ) )
					;;
				platform=*)
					varvalue1="$(abb vars branch)"
					varvalue1="${varvalue1[@]##*=}"
					varvalue="${varvalue} ${varvalue1}"
					COMPREPLY=( $( compgen -W "$varvalue" -- "$ncur" ) )
					;;
				updatetype=*)
					COMPREPLY=( $( compgen -W "$updatetypes $varvalue" -- "$ncur" ) )
					;;
				publish=*)
					COMPREPLY=( $( compgen -W "true false $varvalue" -- "$ncur" ) )
					;;
				repos=*)
					COMPREPLY=( $( compgen -W "main main,contrib $varvalue" -- "$ncur" ) )
					;;
				reposave=*)
					varvalue1="$(abb vars branch)"
					varvalue1="${varvalue1[@]##*=}"
					varvalue="${varvalue} ${varvalue1}"
					COMPREPLY=( $( compgen -W "personal $varvalue" -- "$ncur" ) )
					;;
			esac
			return 0
		fi

		case "${words[1]}" in
			build*)
				COMPREPLY=( $( compgen -d -W "$specs" -- "$cur" ) \
					$( compgen -W "$opts_build" -- "$cur" ) )
				return 0
				;;
			restore)
				COMPREPLY=( $( compgen -d -W "$specs" -- "$cur" ) \
					$( compgen -W "$opts_restore" -- "$cur" ) )
				return 0
				;;
			status)
				COMPREPLY=( $( compgen -d -W "$specs" -- "$cur" ) \
					$( compgen -W "$opts_status" -- "$cur" ) )
				return 0
				;;
			view)
				COMPREPLY=( $( compgen -d -W "$specs" -- "$cur" ) \
					$( compgen -W "$opts_view" -- "$cur" ) )
				return 0
				;;
			send)
				COMPREPLY=( $( compgen -d -W "$specs" -- "$cur" ) \
					$( compgen -W "$opts_send" -- "$cur" ) )
				return 0
				;;
			clone)
				COMPREPLY=( $( compgen -d -W "$opts_clone" -- "$cur" ) )
				return 0
				;;
			new)
				COMPREPLY=( $( compgen -d -W "$opts_new" -- "$cur" ) )
				return 0
				;;
			store)
				COMPREPLY=( $( compgen -f -W "$opts_store" -- "$cur" ) )
				return 0
				;;
			publish)
				COMPREPLY=( $( compgen -W "$opts_publish" -- "$cur" ) )
				return 0
				;;
			vars)
				varvalue=${opts//=/}
				COMPREPLY=( $( compgen -W "${varvalue}" -S ' ' -- "$cur" ) )
				return 0
				;;
		esac
	}
	complete -o nospace -o default -F _abb abb
}
