# publican completion

_publican()
{
	local cur prev commands options command

	COMPREPLY=()
	cur=`_get_cword`
        brands=`ls /usr/share/publican/Common_Content`

	commands='add_revision build clean clean_ids clean_set create create_brand create_site help_config install_book install_brand lang_stats migrate_site package print_banned print_known print_tree print_unused print_unused_images remove_book rename site_stats trans_drop update_db update_po update_pot update_site'

	if [[ $COMP_CWORD -eq 1 ]] ; then
		if [[ "$cur" == -* ]]; then
			COMPREPLY=( $( compgen -W '--help' -- $cur ) )
		else
			COMPREPLY=( $( compgen -W "$commands" -- $cur ) )
		fi
	else
		prev=${COMP_WORDS[COMP_CWORD-1]}
		case $prev in
			@(--site_config|--config))
				_filedir 'cfg'
				return 0;
				;;
			@(--lang|--langs))
				COMPREPLY=( $( compgen -d -- "$cur" ) )
				return 0;
				;;
			--formats)
				COMPREPLY=( $( compgen -W "eclipse epub drupal-book html html-single html-desktop man pdf txt xml" -- $cur ) )
				return 0;
				;;
			--type)
				COMPREPLY=( $( compgen -W "Article Book Set" -- $cur ) )
				return 0;
				;;
			--brand)
				COMPREPLY=( $( compgen -W "$brands" -- $cur ) )
				return 0;
				;;
		esac

		command=${COMP_WORDS[1]}

		if [[ "$cur" == -* ]]; then
			# possible options for the command
			case $command in
				add_revision)
					options='--date --email --firstname --lang --member --revnumber --surname '
					;;
				build)
					options='--distributed_set --embedtoc --formats --langs --novalid --pdftool --pub_dir --publish --src_dir '
					;;
				clean)
					options='--pub_dir '
					;;
				clean_ids)
					options=''
					;;
				clean_set)
					options=''
					;;
				create)
					options='--brand --edition --lang --name --product --type --version '
					;;
				create_brand)
					options='--lang --name '
					;;
				create_site)
					options='--db_file --lang --site_config --tmpl_path --toc_path '
					;;
				help_config)
					options=''
					;;
				install_book)
					options='--lang --site_config '
					;;
				install_brand)
					options='--path --pub_dir --web '
					;;
				lang_stats)
					options='--lang '
					;;
				migrate_site)
					options='--site_config '
					;;
				package)
					options='--binary --brew --desktop --lang --pub_dir --scratch --short_sighted --wait '
					;;
				print_banned)
					options=''
					;;
				print_known)
					options=''
					;;
				print_tree)
					options=''
					;;
				print_unused)
					options=''
					;;
				print_unused_images)
					options=''
					;;
				remove_book)
					options='--lang --site_config '
					;;
				rename)
					options='--name --product --version '
					;;
				site_stats)
					options='--site_config '
					;;
				trans_drop)
					options=''
					;;
				update_db)
					options='--abstract --add --del --formats --lang --name --name_label --product --product_label --site_config --sort_order --subtitle --version --version_label '
					;;
				update_po)
					options='--email --firstname --langs --msgmerge --surname '
					;;
				update_pot)
					options=''
					;;
				update_site)
					options='--site_config '
					;;
			esac
			options="$options --brand_dir --common_config --common_content --config --help --nocolours --quiet "
			COMPREPLY=( $( compgen -W "$options" -- $cur ) )
		else
			if [[ "$command" == @(--help) ]]; then
				COMPREPLY=( $( compgen -W "$commands" -- $cur ) )
			else
				_filedir
			fi
		fi
	fi

	return 0
}
complete -F _publican publican
