#compdef dpt

_dpt_conf() {
	for f in ~/.dpt.conf ~/.config/dpt.conf; do
		if [ -r "$f" ]; then
		. "$f"
	fi
	done
}

_dpt() {
	_arguments \
		'(-h --help)'{-h,--help}'[show help]' \
		'*::dpt commands:_dpt_commands'
}

_dpt_commands() {
	local -a dpt_commands
	typeset -gA _dpt_alias_list
	dpt_commands=(
		'cd:change directory to a package working directory'
		'checkout:work on pkg-perl package'
		'ci-failures:query ci.debian.net for autopkgtest failures'
		'clean-mr-repos:remove local repos which are gone from salsa.debian.org'
		'co:alias for checkout'
		'dch-note:add notes/TODO items to debian/changelog'
		'debian-upstream:create debian/upstream/metadata file from META.{json,yml}'
		'fixup:apply some automatic fixes to packaging'
		'forward:forward a bug or a patch upstream'
		'gc:swipe pkg-perl working directories'
		'gen-itp:create ITP bug template'
		'get-ubuntu-packages:list Ubuntu packages maintained by the group'
		'import-orig:"gbp import-orig" wrapper with upstream tracking support'
		'invite-github:invite someone to GitHub team'
		'lp-mass-subscribe:subscribe the Ubuntu group to a list of packages'
		'missing-pristine-tar:remedy missing pristine-tar information'
		'missing-upload:incorporate package uploads not done from Git'
		'missing-upstream:remedy missing upstream branch/tags'
		'new-upstream:list packages with newer upstream versions'
		'never-uploaded:list packages in Git which are not in the archive'
		'packagecheck:various package consistency checks'
		'prepare:prepare yourself and a source directory before working on a package'
		'push:push relevant refs to "origin" remote'
		'ready-for-upload:list packages in Git which need an upload'
		'rename-uploader:mass-change of uploaded name/email'
		'salsa:manage repositories and members on salsa.debian.org'
		'takeover:take over package maintenance'
		'upstream-repo:add upstream Git repository as git remote upstream-repo'
		'uscan:tiny wrapper around uscan and gbp-import-orig'
	)
	_dpt_alias_list=(
		co checkout
	)
	if ((CURRENT == 1)); then
		_describe -t commands 'dpt subcommands' dpt_commands
	else
		local curcontext="$curcontext"
		cmd=$words[1]
		[[ -z $_dpt_alias_list[$cmd] ]] || cmd=$_dpt_alias_list[$cmd]
		if (( $+functions[_dpt_$cmd] )); then
			_dpt_$cmd
		else
			_message -e "no completion for subcommand $cmd"
		fi
	fi
}

_dpt_cd() {
	_dpt_conf
	_arguments \
		'*:package name:_files -W ${DPT_PACKAGES:-$HOME} -/'
}

_dpt_ci-failures() {
	_arguments \
		'-d+[Distribution, defaults to unstable]:distribution:(testing unstable)' \
		'-A+[Architecture, defaults to amd64]:architecture (currently only amd64):_deb_architectures' \
		'-a[Show more ("all") results (slow). By default only packages matching /perl/ and the latest versions are shown.]' \
		'-b[Additionally show RC bugs for the found packages.]' \
		'-I[Additionally show the run id for the last run (suitable for retrying a test via the ci.debian.net API.)]' \
		'-h[Show help]' \
		'-m[Show full manpage]'
}

_dpt_clean-mr-repos() {
	_arguments \
		'--help[Show help]' \
		'--force[Actually remove the local repositories. By default they are only listed.]'
}

_dpt_checkout() {
	_dpt_conf
	_arguments \
		'*:package name:_files -W ${DPT_PACKAGES:-$HOME} -/'
}

_dpt_dch-note() {
	_arguments \
		'(--ignore --ignore-version)'{--ignore,--ignore-version}'[Add an "IGNORE-VERSION: version" comment.]:comment' \
		'(--waits --waits-for)'{--waits,--waits-for}'[Add a "WAITS-FOR: package version" comment.]:package name::package version:' \
		'--unreleased[Change distribution in debian/changelog to "unreleased"]'
}

_dpt_debian-upstream() {
	# no arguments
	_message -e "no options or arguments for dpt $words[1]"
}

_dpt_fixup() {
	# no arguments
	_message -e "no options or arguments for dpt $words[1]"
}

_dpt_forward() {
	_arguments \
		'(--dist -d)'{--dist,-d}'[distribution name, if not found automatically]:distribution name}' \
		"--force[don't abort if the bug/patch is already forwarded]" \
		'(--mode -m)'{--mode,-m}'[mode: bug or patch; rarely needed]:mode:(bug patch)' \
		'--offline-test[skip operations that require network]' \
		'--ticket[add to existing ticket instead of opening a new one]:ticket number:' \
		'(--tracker -t)'{--tracker,-t}'[tracker name: "cpan" or "github", if not found automatically]:tracker name:(cpan github)' \
		'(--tracker-url -u)'{--tracker-url,-u}'[tracker URL, mandatory for "github" tracker]:tracker URL:_urls' \
		'--use-mail[send submission via email]' \
		'--mailto[email address to send submission to]' \
		'--fallback[fallback to creating bug reports if pull request fails]' \
		'*:Debian bug number or path to patch (or both):_files'
}

_dpt_gc() {
	_arguments \
	'-h[Display usage information.]' \
	'-p[Run in parallel, utilizing all CPUs.]' \
	'-s[Display statistics about disk usage before and after garbage collection.]' \
	"-v[Verbose operation. Let git-gc(1) emit progress messages. Doesn't display per-repository stats when -p is used.]"
}

_dpt_gen-itp() {
	# no arguments
	_message -e "no options or arguments for dpt $words[1]"
}

_dpt_get-ubuntu-packages() {
	# no arguments
	_message -e "no options or arguments for dpt $words[1]"
}

_dpt_import-orig() {
	_arguments \
		'*::tarball (optional, default\: --uscan):'
}

_dpt_invite-github() {
	_arguments \
		'--role=[set role for user]:role:(admin member)' \
		'--force[change role of an existing member]' \
		'*:github-username:'
}

_dpt_lp-mass-subscribe() {
	# no arguments
	_message -e "no options or arguments for dpt $words[1]"
}

_dpt_missing-pristine-tar() {
	# no arguments
	_message -e "no options or arguments for dpt $words[1]"
}

# based on /usr/share/zsh/functions/Completion/Unix/_git
__git_tags () {
	local expl
	declare -a tags
	tags=(${${(f)"$(_call_program tagrefs git for-each-ref --format='"%(refname)"' refs/tags 2>/dev/null)"}#refs/tags/})
	_wanted tags expl tag compadd "$@" -a - tags
}

_dpt_missing-upload() {
	_arguments \
	'1:base tag:__git_tags' \
	'2:new version'
}

_dpt_missing-upstream() {
	_arguments \
		'--check-only[Only report missing tags, do not try to download sources.]' \
		"--skip-missing-snapshots[Don't error out if tarballs are not found on snapshot.debian.org]"
}

_dpt_new-upstream() {
	_arguments \
		'(-h --help)'{-h,--help}'[show help]' \
		'(-m --man)'{-m,--man}'[show manpage]' \
		'(-l --limit)'{--limit,-l}'[packages list file]:packages list file:_files'
}

_dpt_never-uploaded() {
	_arguments \
		'(-h --help)'{-h,--help}'[show help]' \
		'(-m --man)'{-m,--man}'[show manpage]' \
}

_dpt_ready-for-upload() {
	_arguments \
		'(-h --help)'{-h,--help}'[show help]' \
		'(-m --man)'{-m,--man}'[show manpage]' \
}

_dpt_packagecheck() {
	_arguments \
		+ '(act on)' \
		'-c[only check current directory]' \
		'-p[check specific package]:top of directory tree:' \
		+ '(commit options)' \
		'-a[commit changes with "mr commit"]' \
		'-n[do not commit changes with "mr commit"]' \
		+ 'check options' \
		'-A[run all checks]' \
		'-V[add/fix Vcs-* fields]' \
		'-H[add Homepage field]' \
		'-M[fix Maintainer field]' \
		'-D[add add "${misc:Depends}" to Depends]' \
		'-d[fix Format-Specification]' \
		'-P[remove ancient perl version constraints]' \
		'-O[replace "perl-modules" with "perl"]' \
		'-W[use MetaCPAN URLs]' \
		'-C[If -W is given, create debian/watch if it does not exist]' \
		'-R[fixups for debian/rules]' \
		'-Q[add debian/README.source if the package uses quilt]'
}

_dpt_prepare() {
	# no arguments
	_message -e "no options or arguments for dpt $words[1]"
}

_dpt_push() {
	_arguments \
		'*::git arguments (optional):'
}

_dpt_rename-uploader() {
	_arguments \
		'1:full name' \
		'2:old email address' \
		'3:new email address'
}

_dpt_salsa() {
	_arguments \
		'--help[show short help]' \
		'--man[show full manpage]' \
		'*::dpt salsa commands:_dpt_salsa_commands'
}

_dpt_salsa_commands() {

	local -a dpt_salsa_repo_commands dpt_salsa_user_commands dpt_salsa_other_commands
	dpt_salsa_repo_commands=(
		'pushrepo:create and configure repo on salsa and push local repo'
		'createrepo:create and configure new empty repo on salsa'
		'configurerepo:configure repo on salsa'
		'changerepo:change name or description of a repo on salsa'
		'listrepos:list repos on salsa'
		'kgb:enable/disable kgb webhook for repo'
		'toattic:move repo to attic subgroup'
		'fromattic:resurrect repo from attic subgroup'
	)
	dpt_salsa_user_commands=(
		'adduser:add user to group'
		'removeuser:remove user from group'
		'changeuser:change access level of user in group'
		'listmembers:list all group members'
	)
	dpt_salsa_other_commands=(
		'mrconfig:helper for handling all repos with mr(1)'
		'current_user:information about current user'
		'help:same as --help'
		'version:returns version of Gitlab running on salsa'
	)
	if ((CURRENT == 1)); then
		_describe -t salsacommands \
			'dpt salsa subcommands' dpt_salsa_repo_commands \
			-- \
			dpt_salsa_user_commands \
			-- \
			dpt_salsa_other_commands
	else
		local curcontext="$curcontext"
		cmd=$words[1]
		if (( $+functions[_dpt_salsa_$cmd] )); then
			_dpt_salsa_$cmd
		else
			_message -e "no completion for dpt-salsa subcommand $cmd"
		fi
	fi
}

_dpt_salsa_pushrepo() {
	# no arguments
	_message -e "no options or arguments for dpt salsa $words[1]"
}

_dpt_salsa_createrepo() {
	_arguments \
		'1:repository name'
}

_dpt_salsa_configurerepo() {
	_arguments \
		- 'one' \
		'1:repositoryid|repositoryname' \
		- 'all' \
		'--all[configure all repos]' \
		'--attic[configure attic repos]' \
		'--verbose[enable output]' \
		'(-j --parallel)'{-j,--parallel}'=[run N parallel processes]:count:'
}

_dpt_salsa_changerepo() {
	_arguments \
		'1:repositoryid|repositoryname' \
		'2:property:(name description)' \
		'3:new value'
}

_dpt_salsa_listrepos() {
	_arguments \
		'--json[output result as JSON]' \
		'--attic[list repos in attic]'
}

_dpt_salsa_kgb() {
	_arguments \
		+ '(operation)' \
		'--on[turn on KGB notfications]' \
		'--off[turn off KGB notfications]' \
		+ 'one' \
		'(all)1:repositoryid|repositoryname' \
		+ 'all' \
		'(one)--all[configure all repos]' \
		'(one)--attic[configure attic repos]'
}

_dpt_salsa_toattic() {
	_arguments \
		'*:repositoryid|repositoryname'
}

_dpt_salsa_fromattic() {
	_arguments \
		'*:repositoryid|repositoryname'
}

_dpt_salsa_adduser() {
	_arguments \
		'1:username|userid' \
		'2::access level (optional, default\: maintainer):(no_access guest reporter developer maintainer owner)'
}

_dpt_salsa_removeuser() {
	_arguments \
		'1:username|userid'
}

_dpt_salsa_changeuser() {
	_arguments \
		'1:access level:(no_access guest reporter developer maintainer owner)' \
		+ '(who)' \
		'2:username|userid' \
		'--all[all group members]'
}

_dpt_salsa_listmembers() {
	_arguments \
		'--json[output result as JSON]'
}

_dpt_salsa_mrconfig() {
	_arguments \
		'(-j --parallel)'{-j,--parallel}'=[run N parallel processes]:count:' \
}

_dpt_salsa_current_user() {
	_arguments \
		'--json[output result as JSON]'
}

_dpt_salsa_help() {
	# no arguments
	_message -e "no options or arguments for dpt salsa $words[1]"
}

_dpt_salsa_version() {
	_arguments \
		'--json[output result as JSON]'
}

_dpt_takeover() {
	_arguments \
		'-t[top directory (mandatory, unless DPT_PACKAGES is set)]:top directory:_files -/' \
		'-g[URL of existing Git repo]:previous Git repo:_urls' \
		'-n[local operation only, do not push to salsa]' \
		'-h[show help]' \
		+ '(message)' \
		'-m[URL to message with takeover request/permission (conflicts with -a)]:URL:_urls' \
		'-a[RFA/ITA bug number (conflicts with -m)]:bug number:_debbugs_bugnumber' \
		+ '(pkg)' \
		'*:packagename:_deb_packages source'
}

_dpt_upstream-repo() {
	_arguments \
		"-g[Don't create debian/upstream/metadata.]" \
		'-u[Update the URL of the git remote "upstream-repo".]'
}

_dpt_uscan() {
	# no arguments
	_message -e "no options or arguments for dpt $words[1]"
}

_dpt "$@"
