#compdef portupgrade portinstall

(( $+functions[_pkgtools_pkgs] )) ||
_pkgtools_pkgs() {
    local ret=1

    compadd "$@" - ${PKGDBDIR:-/var/db/pkg}/*(:t) && ret=0

    if [ X"$service" = X"portinstall" ] || (( $words[(I)-*N*] )); then
	_files "$@" -W ${PORTSDIR:-/usr/ports} -/ && ret=0
    fi

    return ret
}

_arguments -s \
 '(* -r -R)-a[all packages]' \
 '-A:command to run after each installation:' \
 '-B:command to run before each build:' \
 '-c[clean before build]' \
 '-C[clean after install]' \
 '-f[force upgrade]' \
 '-F[fetch only]' \
 '-i[be interactive]' \
 '*-m:make(1) args:' \
 '-n[no execute]' \
 '-N[install a new one when no installed package is found]' \
 '*-o:origin:_files -W ${PORTSDIR\:-/usr/ports} -/' \
 '-p[make package]' \
 '*-P[use packages (specified twice, use packages only)]' \
 '(-a)-R[do upward recursively]' \
 '(-a)-r[do recursively]' \
 '-s[run commands under sudo(8) where needed]' \
 '-S:the alternative to sudo(8):' \
 '-u[uninstall before install]' \
 '-v[be verbose]' \
 '*-x:exclude pattern:compadd - ${PKGDBDIR\:-/var/db/pkg}/*(\:t) -/' \
 '(-a)*:package to upgrade:_pkgtools_pkgs'
