#compdef eselect
# ------------------------------------------------------------------------------
# Copyright (c) 2011 Github zsh-users - http://github.com/zsh-users
# All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions are met:
#     * Redistributions of source code must retain the above copyright
#       notice, this list of conditions and the following disclaimer.
#     * Redistributions in binary form must reproduce the above copyright
#       notice, this list of conditions and the following disclaimer in the
#       documentation and/or other materials provided with the distribution.
#     * Neither the name of the zsh-users nor the
#       names of its contributors may be used to endorse or promote products
#       derived from this software without specific prior written permission.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
# WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
# DISCLAIMED. IN NO EVENT SHALL ZSH-USERS BE LIABLE FOR ANY
# DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
# (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
# LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
# ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
# SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
# ------------------------------------------------------------------------------
# Description
# -----------
#
#  Completion script for Eselect (symlink manager for multiversioning
#  and multibranding support for some libraries and binaries).
#
# ------------------------------------------------------------------------------
# Authors
# -------
#
#  * Vadim A. Misbakh-Soloviov <mva@mva.name>
#  * Bapt <bapt@tuxfamily.org>
#  * kaworu <kaworu@kaworu.ch>
#  * David Durrleman <dualmoo@gmail.com>
#  * oberyno <oberyno@gmail.com>
#  * Mamoru Komachi <usata@usata.org>
#
# ------------------------------------------------------------------------------
#<app-admin/eselect-1.0.11>
#

_eselect_env() {
    if (( $words[(I)(update)] )); then
        _values 'update options' \
            'makelinks[force updating of links]' \
            'noldconfig[Do not alter the ld.so cache or configuration]' && return 0
    fi
    _values 'env options' $stdopts[@] \
        'update[Collect environment variables from all scripts]' && return 0
}

_eselect_binutils() {
    local binutilslist
        if (( $words[(I)(set)] )); then
            binutilslist=(${${${(M)${(f)"$(eselect --colour=no binutils list)"}## *}// \**/}//(  \[*\]   |  \[*\]  )/})
            _values 'available binutils version' $binutilslist[@] && return 0
        fi
        _values 'binutils options' $stdopts[@] \
            'list[List all installed version of binutils]' \
            'show[Print the currently active binutils version]' \
            'set[Activate one of the installed binutils]' && return 0
}

_eselect_kernel() {
    local kernellist
        if (( $words[(I)(set)] )); then
            kernellist=(${${${(M)${(f)"$(eselect --colour=no kernel list)"}## *}// \**/}//(  \[*\]   |  \[*\]  )/})
            _values 'available kernel version' $kernellist[@] && return 0
        fi
        _values 'kernel options' $stdopts[@] \
            'list[List available kernel symlink targets]' \
            'show[Show the current kernel symlink]' \
            'set[Set a new kernel symlink target]' && return 0
}
_eselect_ctags() {
    local ctagslist
        if (( $words[(I)(set)] )); then
            ctagslist=(${${${(M)${(f)"$(eselect --colour=no ctags list)"}## *}// \**/}//(  \[*\]   |  \[*\]  )/})
            _values 'available ctags version' $ctagslist[@] && return 0
        fi
        _values 'ctags options' $stdopts[@] \
            'list[List available ctags symlink targets]' \
            'show[Show the current target of the ctags symlink]' \
            'update[Automatically update the ctags symlink]' \
            'set[Set a new ctags symlink target]' && return 0
}
_eselect_profile() {
    local profilelist
        if (( $words[(I)(set)] )); then
            profilelist=(${${${(M)${(f)"$(eselect --colour=no profile list)"}## *}// \**/}//(  \[*\]   |  \[*\]  )/})
            _values -w 'available profiles' $profilelist[@] \
                '--force[Forcibly set the symlink]' && return 0
        fi
        _values 'profile options' $stdopts[@] \
            'list[List available profile symlink targets]' \
            'show[Show the current make.profile symlink]' \
            'set[Set a new profile symlink target]' && return 0
}
_eselect_fontconfig() {
    local fclistenabled fclistdisabled
        if (( $words[(I)(enable)] )); then
            fclistdisabled=(${${${${(M)${(f)"$(eselect --colour=no fontconfig list)"}## *}#*\*}// \**/}//(  \[*\]   |  \[*\]  )/})
            _values -w '.conf to enable' $fclistdisabled[@] && return 0
        elif (( $words[(I)(disable)] )); then
            fclistenabled=(${${${(M)${(M)${(f)"$(eselect --colour=no fontconfig list)"}## *}#*\*}// \**/}//(  \[*\]   |  \[*\]  )/})
            _values -w '.conf to disable' $fclistenabled[@] && return 0
        fi
        _values 'fontconfig options' $stdopts[@] \
            'list[List available fontconfig .conf files]' \
            'disable[Disable specified fontconfig .conf file(s)]' \
            'enable[Enable specified fontconfig .conf file(s)]' && return 0
}
_eselect_opengl() {
    local opengllist
        if (( $words[(I)(set)] )); then
            opengllist=(${${${(M)${(f)"$(eselect --colour=no opengl list)"}## *}// \**/}//(  \[*\]   |  \[*\]  )/})
            _values -w 'opengl implementations and options' $opengllist[@] \
                '--use-old[If an implementationis already set, use that one instead]' \
                '--prefix[Set the source prefix]:path:_files -/' \
                '--dst-prefix[Set the destination prefix]:path:_files -/' \
                '--impl-headers[Override global headers with ones provided by this profile]' && return 0
        fi
        _values 'opengl options' $stdopts[@] \
            'list[List the available OpenGL implementations]' \
            'set[Select the OpenGL implementation]' \
            'show[Print the current OpenGL implementation]' && return 0
}
_eselect_vi() {
    local vilist
        if (( $words[(I)(set)] )); then
            vilist=(${${${(M)${(f)"$(eselect --colour=no vi list)"}## *}// \**/}//(  \[*\]   |  \[*\]  )/})
            _values -w 'vi implementation' $vilist[@] && return 0
        elif (( $words[(I)(update)] )); then
            _values -w 'option' '--if-unset[Do not override existing implementation]' && return 0
        fi
        _values 'vi options' $stdopts[@] \
            'list[List available vi implementations]' \
            'set[Set a new vi implementation provider]' \
            'show[Show the current vi implementation]' \
            'update[Automatically update the vi provider]' && return 0
}

_eselect_news() {

    ##
    ## TODO: Normal numeric sorting.
    ##         I've spent all the day (12.11.2013) on trying to rewrite this
    ##         function to normally sort (to DO NOT sort, actually) news,
    ##         but it start to grow to very big size and going to be too
    ##         complicated.
    ##       So, I either need to help to do it normally, or to completely
    ##         rewrite this compdef.
    ##

    local -a newslist;
    if ((CURRENT == 3)); then
        _values 'news options' $stdopts[@] \
            'list[List news items]' \
            'count[Display number of news items]' \
            'purge[Purge read news]' \
            'read[Read news items]' \
            'unread[Mark read news items as unread again]' && return 0
    elif ((CURRENT == 4)); then
        if (( $words[(I)(count)] )); then
            _values -w 'news' 'new[Count only new news items]' 'all[Count all news items]' && return 0
        fi

        newslist=(${${${${${${${(M)${(f)"$(eselect --colour=no news list)"}## *}// \**/}/  \[/}/\] ##/\[}/%/]}/ \[/ (}/\] /) })

        if (( $words[(I)(read)] )); then
            newslist+=( "new[Read unread news items (default)]" "all[Read all news items]" "--mbox[Output in mbox format]" "--quiet[Suppress output, only change status]" "--raw[Output in raw format]" )
        fi;

        if (( $words[(I)(unread)] )); then
            newslist+=( "all[Unread all news items]" )
        fi

         _values -w 'news' $newslist[@] && return 0
    fi
}
_eselect() {
    local globopts sedcmd modnames modopts
    local stdopts
    stdopts=(
        'help[Display help text]'
        'usage[Display usage information]'
        'version[Display version information]'
    )
    globopts=(
            '--brief[Make output shorter]'
            '--colour=no[Disable colour output (default "auto")]'
            '--colour=yes[Enable colour output (default "auto")]'
            )
    modnames=(${${${(M)${(f)"$(eselect --colour=no modules list)"}## *}//   */}//  /})
    if ((CURRENT == 2)); then
        _arguments -s \
            "$globopts[@]" \
            "*:portage:_values 'eselect modules' \$modnames[@]" && return 0
    elif ((CURRENT == 3)); then
        if [[ $words[2] =~ "--colour" || $words[2] =~ "--brief" ]]; then
            _arguments -s \
                "*:portage:_values 'eselect modules' \$modnames[@]" && return 0
        elif (( $modnames[(I)$words[2]] )); then
            if [[ "$words[2]" == (env|binutils|kernel|ctags|profile|fontconfig|opengl|vi|news) ]]; then
                _eselect_$words[2] "$@"
            else
                modopts=(${${${${(M)${(f)"$(eselect --colour=no $words[2] usage)"}## *}//   */}//  /}// */})
                _arguments -s \
                    "*:portage:_values 'eselect $words[2] options' \$modopts[@]" && return 0
            fi
        fi
    elif ((CURRENT >= 4)); then
        if (( $words[(I)(--colour=no|--colour=yes|--brief)] )); then
            if (( $modnames[(I)$words[3]] )); then
                if [[ "$words[3]" == (env|binutils|kernel|ctags|profile|fontconfig|opengl|vi|news) ]]; then
                    _eselect_$words[3] "$@"
                else
                    modopts=(${${${${(M)${(f)"$(eselect --colour=no $words[3] usage)"}## *}//   */}//  /}// *})
                    _arguments -s \
                        "*:portage:_values 'eselect $words[3] options' \$modopts[@]" && return 0
                fi
            fi
        else
            if (( $modnames[(I)$words[2]] )); then
                (( $+functions[_eselect_$words[2]] )) && _eselect_$words[2] "$@"
            fi
        fi
    fi
}

_eselect "$@"

# Local Variables:
# mode: Shell-Script
# sh-indentation: 2
# indent-tabs-mode: nil
# sh-basic-offset: 2
# End:
# vim: ft=zsh sw=2 ts=2 et
