#compdef ethna

# usage:
#   1. copy this file to your favorite directory (ex, ~/.zshrc.d/completion)
#   2. add below lines to your .zshrc:
#      > fpath=(~/.zshrc.d/completion $fpath)
#      > autoload -U compinit
#      > compinit -u
#   3. push [tab] when you typing ethna command's argument.
#      % ethna add-[tab]
#
# note:
#   - we tested this function only with Debian/GNU Linux.
#   - $Id: _ethna 411 2006-11-17 02:32:32Z ichii386 $

_ethna() {
    local curcontext="$curcontext" state line expl ret=1

    _arguments -C \
        '(-v --version)'{-v,--version}'[show version]' \
        '1:ethna command:->ethna-cmd' \
        '*:ethna command args:->ethna-cmd-arg' \
        && ret=0

    if [[ -n "$state" ]]; then
        case $state in
            ethna-cmd)
                handles=( $($service | grep '^ \+.* -> .*:$' | awk '{print $1}') )
                _wanted ethna-cmd expl 'ethna cmd' compadd -a handles && ret=0
            ;;
            ethna-cmd-arg)
                usage=$($service | grep -A 1 "^ \+$words[2] ->")
                if [[ -n "$usage" ]]; then
                    _wanted ethna-cmd-arg expl 'ethna cmd arg' compadd -x "$usage" && ret=0
                else
                    _wanted ethna-cmd-arg expl 'ethna cmd arg' compadd -x "unknown command" && ret=0
                fi
            ;;
        esac    
    fi

    return ret
}
