# Manjaro pamac completion                             -*- shell-script -*-
# ------------------------------------------------------------------------------
# Description
# -----------
#
#  Completion script for the pamac command
#  (https://gitlab.manjaro.org/applications/pamac).
#
# ------------------------------------------------------------------------------
# Authors
# -------
#
#  * Anton Palgunov <toxblh@gmail.com>
#
# ------------------------------------------------------------------------------
# How to use
# -------
#
# Copy this file to /usr/share/bash-completion/completions/pamac
#

_pamac()
{
    local cur prev words cword
    _init_completion || return

    # see if the user selected a command already
    local COMMANDS=(
        "search" 
        "list" 
        "info" 
        "install" 
        "reinstall" 
        "remove" 
        "checkupdates" 
        "update" "upgrade" 
        "clone" 
        "build" 
        "clean")


    local command i
    for (( i=0; i < ${#words[@]}-1; i++ )); do
        if [[ ${COMMANDS[@]} =~ ${words[i]} ]]; then
            command=${words[i]}
            break
        fi
    done

    case $prev in
        --help | --version| -!(-*)[hv])
            return
            ;;
        --builddir | -!(-*)b)
            _filedir
            return
            ;;
    esac

    # supported options per command
    if [[ "$cur" == -* ]]; then
        case $command in
            build)
                COMPREPLY=( $( compgen -W '
                  --builddir 
                  -k --keep
                  --no-keep
                  -d --dry-run
                  --no-clone
                  --no-confirm
                  ' -- "$cur" ) )
                return 0
                ;;

            checkupdates)
                COMPREPLY=( $( compgen -W '
                  --builddir 
                  -a --aur
                  --no-aur
                  -q --quiet
                  --devel
                  --no-devel
                  ' -- "$cur" ) )
                return 0
                ;;

            clean)
                COMPREPLY=( $( compgen -W '
                  -k --keep
                  -u --uninstalled
                  -b --build-files
                  -d --dry-run
                  -v --verbose
                  --no-confirm
                  ' -- "$cur" ) )
                return 0
                ;;

            clone)
                COMPREPLY=( $( compgen -W '
                  --builddir
                  -r,--recurse
                  -q,--quiet
                  --overwrite
                  ' -- "$cur" ) )
                return 0
                ;;


            info)
                COMPREPLY=( $( compgen -W '
                  -a,--aur
                  --no-aur
                  ' -- "$cur" ) )
                return 0
                ;;


            install)
                COMPREPLY=( $( compgen -W '
                  --as-deps
                  --as-explicit
                  -w --download-only
                  -d --dry-run
                  --ignore
                  --no-confirm
                  --no-upgrade
                  --overwrite
                  --upgrade
                  ' -- "$cur" ) )
                return 0
                ;;


            list)
                COMPREPLY=( $( compgen -W '
                  -e --explicitly-installed
                  -f --files
                  -m --foreign
                  -g --groups
                  -i --installed
                  -o --orphans
                  -q --quiet
                  -r --repos
                  ' -- "$cur" ) )
                return 0
                ;;

            reinstall)
                COMPREPLY=( $( compgen -W '
                  --as-deps
                  --as-explicit
                  -w --download-only
                  --no-confirm
                  --overwrite
                  ' -- "$cur" ) )
                return 0
                ;;

            remove)
                COMPREPLY=( $( compgen -W '
                  --as-deps
                  --as-explicit
                  -w --download-only
                  -c --cascade
                  -d --dry-run
                  --no-confirm
                  ' -- "$cur" ) )
                return 0
                ;;

            search)
                COMPREPLY=( $( compgen -W '
                  -a --aur
                  -f --files
                  -i --installed
                  --no-aur
                  -q --quiet
                  -r --repos
                  ' -- "$cur" ) )
                return 0
                ;;

            update|upgrade)
                COMPREPLY=( $( compgen -W '
                  -a --aur
                  --builddir
                  --devel
                  --disable-downgrade
                  -w --download-only
                  -d --dry-run
                  --enable-downgrade
                  --force-refresh
                  --ignore
                  --no-aur
                  --no-confirm
                  --no-devel
                  --overwrite
                  ' -- "$cur" ) )
                return 0
                ;;
            *) 
                COMPREPLY=( $( compgen -W '
                  -h --help
                  --version
                  ' -- "$cur" ) )
                return 0
                ;;
        esac
    fi

    # specific command arguments
    if [[ -n $command ]]; then
        case $command in

            # installed packages
            reinstall|remove)
                COMPREPLY=( $( compgen -W '$( pamac list -q -i )' \
                    -- "$cur" ) )
                return 0
                ;;

            # all packages
            install|clone|build|info)
                COMPREPLY=( $( pamac search -q  "$cur" \
                    2> /dev/null ) )
                return 0
                ;;
        esac
    fi

    # no command yet, show what commands we have
    if [ "$command" = "" ]; then
        COMPREPLY=( $( compgen -W '${COMMANDS[@]}' -- "$cur" ) )
    fi

    return 0

} &&
complete -F _pamac pamac

# ex: ts=4 sw=4 et filetype=sh
