# Bash/ZSH completion for t2build

_t2build() {
    #compdef t2build
    local cur prev
    if type compdef &> /dev/null; then
        cur="${words[CURRENT]}"
        [ $CURRENT -gt 2 ] && prev=${words[CURRENT-1]}
    else
        local words cword
        _init_completion || return
    fi
    local args=(-a --all -b --build -i --install -R --rebuild -I --ignore
                -k --package -r --configure -d --debug -c --clean -f --force
                -u --unload -o -p --plugin-dir -K --package-all -l --list
                -e --empty -\? -h --help)
    case $prev in
        -p)
            if type compdef &> /dev/null; then
                _files -/
            else
                _filedir -d
            fi
            return
            ;;
        -I)
            if type compdef &> /dev/null; then
                _files
            else
                _filedir
            fi
            return
            ;;
    esac
    if [[ "$cur" == -* ]]; then
        if type compdef &> /dev/null; then
            _arguments --
        else
            COMPREPLY=($(compgen -W "${args[*]}" -- "$cur"))
        fi
        return
    fi
    local plugins=()
    for i in "$T2HOME"/*; do
        local b=$(basename "$i")
        if [ -d "$i" ] && [ "$b" != "doc" ] && [ "$b" != "t2PSkel" ] && [ "$b" != "scripts" ] && [ "$b" != "utils" ]; then
            plugins+=($b)
        fi
    done
    if [ "$prev" = "-b" ]; then
        if type compdef &> /dev/null; then
            compadd "${plugins[@]}"
        else
            compopt -o nospace
            COMPREPLY=($(compgen -f -W "${plugins[*]}" -- "$cur"))
        fi
    elif type compdef &> /dev/null; then
        compadd "${plugins[@]}"
    else
        COMPREPLY=($(compgen -W "${plugins[*]}" -- "$cur"))
    fi
}

if type compdef &> /dev/null; then
    compdef _t2build t2build
else
    complete -F _t2build t2build
fi
