#compdef eix

# options from eix --help
# eix 0.10.3 (gcc-4.2.2, x86_64-pc-linux-gnu)
#


# category of options
local   global_exclusives_opts  \
        global_special          \
        global_output           \
        local_misc              \
        local_search_field      \
        local_type_of_pattern

global_exclusives_opts=(
    {'(--help)-h','(-h)--help'}'[show help and exit]'
    {'(--version)-V','(-V)--version'}'[show version and exit]'
    '--dump[dump variables to stdout]'
    '--dump-defaults[dump default values of variables]'
    '--print[print the expanded value of a variable]'
)

global_special=(
    {'(--test-non-matching)-t','(-t)--test-non-matching'}'[print non-matching entries of /etc/portage/package.* and non-matching names of installed packages]'
    {'(--quick)-Q','(-Q)--quick'}"[(toggle) don't read unguessable slots of installed packages]"
    '--care[always read slots of installed packages]'
    '--cache-file[use another cache-file instead of /var/cache/eix]'
)

global_output=(
    {'(--quiet)-q','(-q)--quiet'}'[(toggle) (no) output]'
    {'(--nocolor)-n','(-n)--nocolor'}'[do not use ANSI color codes]'
    {'(--force-color)-F','(-F)--force-color'}'[force colorful output]'
    {'(--pure-packages)-\\*','(-\\*)--pure-packages'}'[Omit printing of overlay names and package number]'
    '--only-names[print with format <category>/<name>]'
    {'(--compact)-c','(-c)--compact'}'[(toggle) compact search results]'
    {'(--verbose)-v','(-v)--verbose'}'[(toggle) verbose search results]'
    {'(--versionsort)-x','(-x)--versionsort'}'[(toggle) sort output by slots/versions]'
    {'(--versionlines)-l','(-l)--versionlines'}'[(toggle) print available versions line-by-line]' \
    '--format[format string for normal output]'
    '--format-compact[format string for compact output]'
    '--format-verbose[format string for verbose outp]'
)

local_misc=(
    {'(--installed)-I','(-I)--installed'}'[Next expression only matches installed packages]'
    {'(--multi-installed)-i','(-i)--multi-installed'}'[Match packages installed in several versions]'
    {'(--dup-packages)-d','(-d)--dup-packages'}'[Match duplicated packages]'
    {'(--dup-versions)-D','(-D)--dup-versions'}'[Match packages with duplicated versions]'
    {'(--slotted)-1','(-1)--slotted'}'[Match packages with a nontrivial slot]'
    {'(--slots)-2','(-2)--slots'}'[Match packages with two different slots]'
    {'(--upgrade)-u','(-u)--upgrade'}'[Match packages without best slotted version]'
    '--stable[Match packages with a stable version]'
    '--testing[Match packages with a testing or stable version]'
    '--non-masked[Match packages with a non-masked version]'
    '--system[Match system packages]'
    {'(--overlay)-O','(-O)--overlay'}'[Match packages from overlays]'
    '--in-overlay[OVERLAY Match packages from OVERLAY]'
    '--only-in-overlay[OVERLAY Match packages only in OVERLAY]'
    {'(--installed-overlay)-J','(-J)--installed-overlay'}'[Match packages installed from overlays]'
    '--installed-from-overlay[OVERLAY Packages installed from OVERLAY]'
    '--installed-in-some-overlay[Packages with an installed version provided by some overlay]'
    '--installed-in-overlay[OVERLAY Packages with an installed version provided from OVERLAY]'
    '--fetch[Match packages with a fetch restriction]'
    '--mirror[Match packages with a mirror restriction]'
    {'(--test-obsolete)-T','(-T)--test-obsolete'}'[Match packages with obsolete entries in /etc/portage/package.*]'
    {'(--not)-!','(-!)--not'}'[(toggle) Invert the expression]'
    {'(--pipe)-\\|','(-\\|)--pipe'}'[Use input from pipe of emerge -pv]'
)

local_search_field=(
    {'(--description)-S','(-S)--description'}'[description]'
    {'(--category-name)-A','(-A)--category-name'}'["category/name"]'
    {'(--category)-C','(-C)--category'}'[category]'
    {'(--name)-s','(-s)--name'}'[name (default)]'
    {'(--homepage)-H','(-H)--homepage'}'[homepage]'
    {'(--license)-L','(-L)--license'}'[license]'
    {'(--provide)-P','(-P)--provide'}'[provides]'
    {'(--use)-U','(-U)--use'}'[useflag (of the ebuild)]'
    '--installed-with-use[enabled useflag (of installed package)]'
    '--installed-without-use[disabled useflag (of installed package)]'
)

local_type_of_pattern=(
    {'(--regex)-r','(-r)--regex'}'[Pattern is a regexp (default)]'
    {'(--exact)-e','(-e)--exact'}'[Pattern is the exact string]'
    {'(--pattern)-p','(-p)--pattern'}'[Pattern is a wildcards-pattern]'
    {'(--fuzzy)-f','(-f)--fuzzy'}'[Use fuzzy-search with a max. levenshtein-distance m (default: 2)]'
)


local prev="$words[CURRENT-1]"  # previous word
local _gentoo_packages_scope    # used to restrict the package scope (installed,category,useflag...)

# global exclusiv opts, no more completion.
if (( $words[(I)(-[[:alnum:]]#(h|V)[[:alnum:]]#|--(help|version|dump|dump-defaults))] )); then
    return 0
# already have a --print ARG option. no more completion.
elif (( $words[(I)(--print)] )) && [[ "$prev" != '--print' ]]; then
    return 0
else
    case "$prev" in
# eix variables for --print
        '--print' )
        local vars
        vars=(${${(M)${(f)"$(eix --dump)"}##[A-Z]*}%%\=*})
        _values 'variables' $vars
        return 0
        ;;

# files for --cache-file
        '--cache-file' )
            _arguments -s '*:filename:_files'
            return 0
        ;;

# overlays for --only-in-overlay|--in-overlay|--installed-from-overlay|--installed-in-overlay
        --((only-|)in|installed-(from|in))-overlay )
            for f in /etc/make.conf /etc/make.globals; do
                [[ -z "$PORTDIR_OVERLAY" && -r "$f" ]] &&
	                local PORTDIR_OVERLAY="`. "$f" 2>/dev/null; echo ${PORTDIR_OVERLAY}`"
            done

            _values 'overlays' $=PORTDIR_OVERLAY
            return 0
        ;;

# useflags for -U|--use|--installed-with-use|--installed-without-use
        (-[[:alnum:]]#U|--(use|installed-with(out|)-use)) )
            _gentoo_packages_scope='*:package:_gentoo_packages useflag'
        ;;

# category for -C|--category
        -([[:alnum:]]#C|-category) )
            _gentoo_packages_scope='*:package:_gentoo_packages category'
        ;;

        * )
            #if (( $words[(I)(-[[:alnum:]]#2[[:alnum:]]#|--slots)] )); then
            #    local pkgs
            #    pkgs=(${${(f)"$(eix -2 --only-names)"}##*/})
            #    _values 'packages' $pkgs && return 0

        # installed packages for -i|--installed|-I|--multi-installed|-T|--test-obsolete
            if (( $words[(I)(-[[:alnum:]]#(i|I|T)[[:alnum:]]#|--(((multi-|)installed)|test-obsolete))] )); then
                _gentoo_packages_scope='*:package:_gentoo_packages installed'
        # only packages names (no category) for -s|--name
            elif (( $words[(I)(-[[:alnum:]]#s[[:alnum:]]#|--name)] )); then
                _gentoo_packages_scope='*:package:_gentoo_packages available_pkgnames_only'
            else # default
                _gentoo_packages_scope='*:package:_gentoo_packages available'
            fi
        ;;
    esac
fi

_arguments -s   $global_exclusives_opts     \
                $global_special             \
                $global_output              \
                $local_misc                 \
                $local_search_field         \
                $local_type_of_pattern      \
                $_gentoo_packages_scope     &&
    return 0
