#autoload

#Author: baptux <bapt@ifrance.com>
#Author: oberyno <oberyno@gmail.com>
#Description:
# functions for gentoo packages
# inspired by _deb_packages
#Usage: _gentoo_packages installed|available|installed_versions|available_versions|binary|category|useflag

# Completion function to show useflags.
_gentoo_packages_update_useflag(){
    local flags PORTDIR
	var=PORTDIR
	[[ -z ${(P)var} && -r /etc/make.conf ]] &&
	    local $var="`. /etc/make.conf 2>/dev/null; echo ${(P)var}`"
	[[ -z ${(P)var} && -r /etc/make.globals ]] &&
	    local $var="`. /etc/make.globals 2>/dev/null; echo ${(P)var}`"

    flags=( ${${(M)${(f)"$(<$PORTDIR/profiles/use.desc)"}:#* - *}%% - *}
    ${${${(M)${(f)"$(<$PORTDIR/profiles/use.local.desc)"}#* - *}%% - *}#*:} )
    compadd $flags
}

_gentoo_packages_update_active_useflag(){
  local flags USE
  var=USE
  [[ -z ${(P)var} && -r /etc/make.conf ]] &&
    local $var="`. /etc/make.conf 2>/dev/null; echo ${(P)var}`"
  flags=(${${${=USE}%-*}%\\*})
  compadd $flags
}
_gentoo_packages_update_category(){
    local var trees category
    for var in PORTDIR PORTDIR_OVERLAY ; do
	[[ -z ${(P)var} && -r /etc/make.conf ]] &&
	    local $var="`. /etc/make.conf 2>/dev/null; echo ${(P)var}`"
	[[ -z ${(P)var} && -r /etc/make.globals ]] &&
	    local $var="`. /etc/make.globals 2>/dev/null; echo ${(P)var}`"
    done
    trees=($PORTDIR $=PORTDIR_OVERLAY)
    category=( $trees/*-*(/:t) )
    _wanted cat_packages expl 'category' compadd "$@" $category
}

_gentoo_packages_update_installed(){
 local installed_dir installed_portage installed_list expl
    installed_dir="/var/db/pkg"
    installed_portage=($installed_dir/*-*/*)

    installed_pkgname=( ${${installed_portage:t}%%-[0-9]*} )
    _wanted packages expl 'category/package' compadd "$@" ${installed_pkgname}

    installed_list=( ${${installed_portage#$installed_dir/}%%-[0-9]*} )
    _wanted cat_packages expl 'category/package' _multi_parts "$@" / installed_list

}

_gentoo_packages_update_installed_versions(){
    local installed_list installed_portage expl

    installed_portage=(/var/db/pkg/*-*/*)
    _wanted packages expl 'package' compadd "$@" ${installed_portage:t}

    installed_list=( ${installed_portage##*/pkg/} )
    _wanted cat_packages expl 'category/package' _multi_parts "$@" / installed_list
}

_gentoo_packages_update_available(){
    local var trees category packages pkg expl

    for var in PORTDIR PORTDIR_OVERLAY ; do
	[[ -z ${(P)var} && -r /etc/make.conf ]] &&
	    local $var="`. /etc/make.conf 2>/dev/null; echo ${(P)var}`"
	[[ -z ${(P)var} && -r /etc/make.globals ]] &&
	    local $var="`. /etc/make.globals 2>/dev/null; echo ${(P)var}`"
    done
    trees=( $PORTDIR $=PORTDIR_OVERLAY)
    category=( $trees/*-*(/:t) )

    packages=( $trees/*-*/*(:t) )
    _wanted packages expl 'package' compadd  - "${(@)packages}"

    # Complete cat/pkg. _multi_parts is much to slow for such a large task,
    # _sep_parts removes the dash from gnome-<tab>, and _path_files wants to
    # complete cat/pkg/files (if "files" is ignored with -F, miscfiles, etc...
    # don't get completed).
    if [[ $PREFIX != */* ]] ; then
	_wanted cat_packages expl 'category/package' compadd -S '/' $category
    else
	compset -P '*/'
	pkg=( $trees/$IPREFIX/*(:t) )
	_wanted cat_packages expl 'category/package' compadd $pkg
    fi
}

_gentoo_packages_update_available_versions(){
    local var overlay_ebuilds portage_ebuilds expl trees category

    for var in PORTDIR PORTDIR_OVERLAY ; do
	[[ -z ${(P)var} && -r /etc/make.conf ]] &&
	    local $var="`. /etc/make.conf 2>/dev/null; echo ${(P)var}`"
	[[ -z ${(P)var} && -r /etc/make.globals ]] &&
	    local $var="`. /etc/make.globals 2>/dev/null; echo ${(P)var}`"
    done
    trees=($PORTDIR $=PORTDIR_OVERLAY)
    category=( $trees/*-*(/:t) )
    typeset -U category

    if [[  $#PREFIX -ge 1  &&  -z $words[(r)(--inject|-i)]  ]] ; then
	overlay_ebuilds=($=PORTDIR_OVERLAY/*-*/${PREFIX%%-[0-9]#*}*/*.ebuild(:t:r) )
	portage_ebuilds=($PORTDIR/metadata/cache/*-*/${PREFIX%%-[0-9]#*}*(:t))
	_wanted packages expl 'package'  compadd  $portage_ebuilds $overlay_ebuilds
    fi
    pkg=( $trees/${PREFIX%%/*}/*/*.ebuild(:t:r) )
    _wanted cat_packages expl 'category/package' _sep_parts category / pkg
}

#Function to show tbz2 files available
_gentoo_packages_update_binary() {
    [[ -z $PKGDIR && -r /etc/make.conf ]] &&
	local PKGDIR="`. /etc/make.conf 2>/dev/null; echo $PKGDIR`"
    [[ -z $PKGDIR && -r /etc/make.globals ]] &&
	local PKGDIR="`. /etc/make.globals 2>/dev/null; echo $PKGDIR`"

    # this doesn't take care of ${PORTAGE_BINHOST}. If Gentoo official
    # binary mirror will be available we should rewrite it accordingly.
    _path_files -g \*.tbz2 -W "$PKGDIR/All"
}

_gentoo_packages () {
	local command="$argv[$#]" expl cachevar pkgset update_policy
	zstyle -s ":completion:*:*:$service:*" cache-policy update_policy
	if [[ -z "$update_policy" ]]; then
		zstyle ":completion:*:*:$service:*" cache-policy _gentoo_cache_policy
	fi
	[[ "$command" == (installed(_versions|)|available(_versions|)|binary|category|useflag|active_useflag) ]] || {
		_message "unknown command: $command"
		return
	}
	[[ "$pkgset" ==  (installed(_versions|)|available(_versions|)|binary|category|useflag|active_useflag) ]] || {
		pkgset="$command"
	}
	expl=("${(@)argv[1,-2]}")
	_gentoo_packages_update_$pkgset

}

_gentoo_cache_policy () {
  # rebuild if cache is more than a week old
    oldp=( "$1"(mw+1) )
      (( $#oldp )) && return 0
}


_gentoo_packages "$@"
