#compdef hsh hsh-install hsh-run hsh-shell mkaptbox

local curcontext="$curcontext" packages help_opts common_opts aptbox_opts ret=1

help_opts=(
    '(-v --verbose -q --quiet)'{-q,--quiet}'[try to be more quiet]'
    '(-v --verbose -q --quiet)'{-v,--verbose}'[print a message for each action]'
    '(- :)'{-V,--version}'[print program version and exit]'
    '(- :)'{-h,--help}'[show usage and exit]'
)

common_opts=(
    '--hasher-priv-dir=[hasher-priv directory]:hasher-priv:_directories'
    '--number=[subconfig identifier]:number'
    '--save-fakeroot[save fakeroot state]'
)

aptbox_opts=(
    '--apt-config=[path to custom apt.conf file]:apt-config:_files'
    '--apt-prefix=[path to apt directory prefix]:apt-prefix:_directories'
    '--repo=[repository directory]:repository directory:_directories'
    '--repo-bin=[binary repository directory]:binary repository directory:_directories'
    '--repo-src=[source repository directory]:source repository directory:_directories'
    '--target=[target architecture]:architecture'
)

case "$service" in
    hsh)
	if (( $words[(I)--(initroot|cleanup)-only] )); then
	    packages=
	else
	    packages='*:package:_files -g "*.(src.rpm|tar)"'
	fi
	_arguments -S \
	    $help_opts \
	    $common_opts \
	    $aptbox_opts \
	    '--build-args=[extra arguments for rpmbuild]:rpmbuild args:' \
	    '(--initroot-only)--cleanup-only[do not build at all, just cleanup workdir]' \
	    '--eager-cleanup[cleanup build environment after each successful build]' \
	    '--lazy-cleanup[cleanup build environment before each new build]' \
	    '--excludedocs[do not install documentation files]' \
	    '(--cleanup-only)--initroot-only[do not build, stop after initroot stage]' \
	    '--install-langs=[colon-separated list of languages to install]:languages' \
	    '--mountpoints=[comma-separated list of known mount points]:mountpoints' \
	    '--no-cache[do not use initroot cache]' \
	    '--no-contents-indices[do not use content indices]' \
	    '(--repackage-source)--no-repackage-source[do not repackage source along with binaries]' \
	    '()--no-sisyphus-check-in=[do not run sisyphus_check input tests]::sisyphus_check tests:_sisyphus_check_tests' \
	    '--no-sisyphus-check=[do not run sisyphus_check tests]::sisyphus_check tests:_sisyphus_check_tests' \
	    '--no-sisyphus-check-out=[do not run sisyphus_check output tests]::sisyphus_check tests:_sisyphus_check_tests' \
	    '--nprocs=[number of CPUs to use]:number' \
	    '--packager=[override default RPM packager tag]:packager:' \
	    '--pkg-build-list=[override default build package file list]:packages' \
	    '--pkg-init-list=[override default initial package file list]:packages' \
	    '(--query-req-prog)--query-repackage[repackage the source before query for requirements]' \
	    '(--query-repackage)--query-req-prog=[program to run to query for requirements]:query-req-prog:_files -g "*(-*)"' \
	    '--rebuild-prog=[program to run to query for rebuild]:qrebuild-prog:_files -g "*(-*)"' \
	    '(--no-repackage-source)--repackage-source[repackage source along with binaries]' \
	    '(--with-stuff)--without-stuff[do not use built packages]' \
	    '(--without-stuff)--with-stuff[allow use of built packages]' \
	    ':workdir:_directories' \
	    $packages && ret=0
	;;
    hsh-install)
	_arguments -S \
	    $help_opts \
	    $common_opts \
	    '--excludedocs[do not install documentation files]' \
	    ':workdir:_directories' \
	    '*:package' && ret=0
	;;
    hsh-run|hsh-shell)
	_arguments -S \
	    $help_opts \
	    $common_opts \
	    '(--rooter)--builder[run program as builder]' \
	    '--execute=[copy given script to workdir and execute it]:script:_files' \
	    '--mountpoints=[comma-separated list of known mount points]:mountpoints' \
	    '--pty[redirect standard descriptors to controlling pty]' \
	    '(--builder)--rooter=[run program as pseudoroot]' \
	    '--shell=[run interactive shell]::shell:_files -g "*(-*)"' \
	    '--x11-timeout=[how long X11 authorization will be valid]:seconds' \
	    '(-x -X -Y)-x[disable X11 forwarding]' \
	    '(-x -X -Y)-X[enable X11 forwarding]' \
	    '(-x -X -Y)-Y[enable trusted X11 forwarding]' \
	    ':workdir:_directories' \
	    '*::command:->command' && ret=0

	case "$state" in
            command)
              shift 1 words
              (( CURRENT-- ))
              _normal -e
              return
              ;;
        esac
	;;
    mkaptbox)
	_arguments -S \
	    $help_opts \
	    $aptbox_opts \
	    '(-f --force)'{-f,--force}'[force aptbox creation]' \
	    '(-u --update)--no-update[do not run "apt-get update" after creation]' \
	    '(-u --update --no-update)'{-u,--update}'[run "apt-get update" after creation]' \
	    '(--with-stuff)--without-stuff[do not configure sources.list for the work repository]' \
	    '(--without-stuff)--with-stuff[configure sources.list for the work repository]' \
	    ':workdir:_directories' && ret=0
	;;

esac

return $ret
