#
# Copyright (C) 2008-2018  Alexey Gladkov <legion@altlinux.org>
#
# Completion functions for gear utilities.
#
# This file is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA.
#

_rpmbuild_shrt_options="-ba -bb -bp -bc -bi -bl -bs -bt"
_rpmbuild_long_options="--buildroot --clean --dbpath --define --disable --enable --macros
--nobuild --nodeps --nopatch --nosource --rcfile --rmsource --rmspec --root --short-circuit
--sign --target --with --without"

_hasher_shrt_options="-q -v -V -h"
_hasher_long_options="--apt-config --apt-prefix --build-args --cache-dir --cleanup-only
--eager-cleanup --excludedocs --hasher-priv-dir --help --initroot-only --install-langs
--lazy-cleanup --mountpoints --no-cache --no-contents-indices --no-repackage-source
--no-sisyphus-check-in --no-sisyphus-check-out --no-sisyphus-check --no-wait-lock
--nprocs --number --packager --pkg-build-list --pkg-init-list --query-repackage
--query-req-prog --quiet --rebuild-prog --repackage-source --repo --repo-bin --repo-src
--save-fakeroot --target --verbose --version --wait-lock --without-stuff
--with-qemu --with-stuff"

_gear_shrt_options="-h -r -t -q -v -V"
_gear_long_options="--bzip2 --command --commit --describe --disable-specsubst --export-dir --gzip --hasher --help
--lz4 --lzop --no-compress --quiet --rpmbuild --rules --tree-ish --update-spec --verbose --version --xz --zstd"

_gear_update_shrt_options="-a -c -f -h -s -t -v -V"
_gear_update_long_options="--all --create --exclude --force --help --ignore-exclude --subdir --type --verbose --version"

_gear_changelog_shrt_options="-f -h -r -V"
_gear_changelog_long_options="--no-groups --no-rules --no-specfile --format --rules --version --help"

_gear_create_tag_shrt_options="-f -h -m -n -q -r -u -v -V"
_gear_create_tag_long_options="--force --help --key-id --message --name --quiet --rules --verbose --version"

_gear_store_tags_shrt_options="-a -c -d -h -q -r -v -V"
_gear_store_tags_long_options="--all --clean --delete --no-clean --rules --quiet --verbose --version --help"

_gear_merge_shrt_options="-a -h -r -v -V"
_gear_merge_long_options="--add --rules --version --version --help"

_gear_srpmimport_shrt_options="-a -h -r -v -V"
_gear_srpmimport_long_options="--add --rules --version --version --help"

_gear_commit_shrt_options="-e -m -h -V"
_gear_commit_long_options="--edit --help --message --no-edit --spec --version"

_gear_edit_spec_shrt_options="-h -V"
_gear_edit_spec_long_options="--help --version"

_gear_import_shrt_options="-a -c -C -f -h -r -s -S -t -T -v -V"
_gear_import_long_options="--all --commit --create --dry-run --force --no-commit --no-tag --ignore-exclude
--rules --sign --subdir --tag --tag-message --time --verbose --version --help"

_gear_rpm_shrt_options="$_rpmbuild_shrt_options $_gear_shrt_options"
_gear_rpm_long_options="$_rpmbuild_long_options $_gear_long_options"

_gear_hsh_shrt_options="$_hasher_shrt_options $_gear_shrt_options"
_gear_hsh_long_options="$_hasher_long_options $_gear_long_options"

__gear_file()
{
	COMPREPLY=()
	_filedir
}

__gear_dir()
{
	COMPREPLY=()
	_filedir -d
}

__gear_prepare()
{
	COMPREPLY=()
	cur=${COMP_WORDS[COMP_CWORD]}
	prev=${COMP_WORDS[COMP_CWORD-1]}
}

__gear_option()
{
	local type= list=

	case "${2-$cur}" in
		--*) type=long ;;
		-*)  type=shrt ;;
	esac

	eval "list=\"\${${1}_${type}_options-}\""
	[ -n "$list" ] || return 1

	COMPREPLY=($(compgen -W "$list" -- "${2-$cur}")) ||:
}

_gear()
{
	local cur prev cmd
	__gear_prepare

	case "$cur" in
		-*)
			__gear_option "$FUNCNAME" "$cur"
			;;
	esac

	case "$prev" in
		--export-dir)
			__gear_dir
			;;
		-r|--rules)
			__gear_file
			;;
	esac
}
complete -F _gear gear

_gear_update()
{
	local cur prev
	__gear_prepare

	case "$cur" in
		-*)
			__gear_option "$FUNCNAME" "$cur"
			;;
		*)
			__gear_file
			;;
	esac

	case "$prev" in
		-s|--subdir)
			__gear_dir
			;;
		-t|--type)
			COMPREPLY=($(compgen -W 'dir tar cpio cpio.gz cpio.bz2 cpio.zst zip' -- $cur))
			;;
	esac
}
complete -F _gear_update gear-update

_gear_rpm()
{
	local cur prev
	__gear_prepare

	case "$cur" in
		-*)
			__gear_option "$FUNCNAME" "$cur"
			;;
	esac

	case "$prev" in
		--buildroot|--dbpath|--root)
			__gear_dir
			;;
		-r|--rules)
			__gear_file
			;;
	esac
}
complete -F _gear_rpm gear-rpm gear-buildreq

_gear_hsh()
{
	local cur prev
	__gear_prepare

	case "$cur" in
		-*)
			__gear_option "$FUNCNAME" "$cur"
			;;
	esac

	case "$prev" in
		--apt-config|--query-req-prog|--rebuild-prog)
			__gear_file
			;;
		--apt-prefix|--hasher-priv-dir|--cache-dir--repo|--repo-bin|--repo-src)
			__gear_dir
			;;
		-r|--rules)
			__gear_file
			;;
	esac
}
complete -F _gear_hsh gear-hsh

_gear_changelog()
{
	local cur prev
	__gear_prepare

	case "$cur" in
		-*)
			__gear_option "$FUNCNAME" "$cur"
			;;
	esac

	case "$prev" in
		-f|--format)
			COMPREPLY=($(compgen -W 'deb gnu rpm' -- $cur))
			;;
		-r|--rules)
			__gear_file
			;;
	esac
}
complete -F _gear_changelog gear-changelog

_gear_create_tag()
{
	local cur prev
	__gear_prepare

	case "$cur" in
		-*)
			__gear_option "$FUNCNAME" "$cur"
			;;
	esac

	case "$prev" in
		-r|--rules)
			__gear_file
			;;
	esac
}
complete -F _gear_create_tag gear-create-tag

_gear_merge()
{
	local cur prev
	__gear_prepare

	case "$cur" in
		-*)
			__gear_option "$FUNCNAME" "$cur"
			;;
	esac

	case "$prev" in
		-r|--rules)
			__gear_file
			;;
	esac

}
complete -F _gear_merge gear-merge

_gear_srpmimport()
{
	local cur prev
	__gear_prepare

	case "$cur" in
		-*)
			__gear_option "$FUNCNAME" "$cur"
			;;
		*)
			__gear_file
			;;
	esac

	case "$prev" in
		-r|--rules)
			__gear_file
			;;
	esac

}
complete -F _gear_srpmimport gear-srpmimport

_gear_store_tags()
{
	local cur prev
	__gear_prepare

	case "$cur" in
		-*)
			__gear_option "$FUNCNAME" "$cur"
			;;
	esac

	case "$prev" in
		-r|--rules)
			__gear_file
			;;
	esac
}
complete -F _gear_store_tags gear-store-tags

_gear_commit()
{
	local cur prev
	__gear_prepare

	case "$cur" in
		-*)	__gear_option "$FUNCNAME" "$cur"
			;;
	esac

	case "$prev" in
		--spec)
			__gear_file
			;;
	esac
}
complete -F _gear_commit gear-commit

_gear_edit_spec()
{
	local cur prev
	__gear_prepare

	case "$cur" in
		-*)
			__gear_option "$FUNCNAME" "$cur"
			;;
	esac
}
complete -F _gear_edit_spec gear-edit-spec

_gear_import()
{
	local cur prev
	__gear_prepare

	case "$cur" in
		-*)
			__gear_option "$FUNCNAME" "$cur"
			;;
		*)
			__gear_file
			;;
	esac

	case "$prev" in
		-r|--rules|--time)
			__gear_file
			;;
		-S|--subdir)
			__gear_dir
			;;
	esac
}
complete -F _gear_import gear-import
