#compdef portupgrade portinstall

(( $+functions[_pkg_pkgs_or_ports] )) ||
_pkg_pkgs_or_ports() {
    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]' \
 '-b[keep backup packages of the old versions'\'']' \
 '-A:command to run after each installation:_command_names' \
 '-B:command to run before each build:_command_names' \
 '(-w)-c[clean before each build (default)]' \
 '(-W)-C[clean after each install (default)]' \
 '-D[distclean before each fetch or build]' \
 '-E[use extended regular expression]' \
 '-f[force upgrade]' \
 '-F[fetch only]' \
 '-i[be interactive]' \
 '-l:save the results as..:_files' \
 '-L:save each port'\''s buid & install log with a prefix..:_files' \
 '*-m:arguments to append to each make(1) command line:' \
 '*-M:arguments to prepend to each make(1) command line:' \
 '-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 recursively]' \
 '(-a)-R[do upward recursively]' \
 '-s[run commands under sudo(8) where needed]' \
 '-S:the alternative to sudo(8):_command_names' \
 '-u[do not preserve old shared libraries]' \
 '-v[be verbose]' \
 '(-c)-w[do not clean before each build]' \
 '(-C)-W[do not clean after each install]' \
 '*-x:exclude pattern:compadd - ${PKGDBDIR\:-/var/db/pkg}/*(/\:t)' \
 '(-a)*:package to upgrade:_pkg_pkgs_or_ports'
