# bash >= 3 completion for mock(1)

_mock_root()
{
    test $# -eq 0 && set -- /etc/mock "$HOME/.config/mock"

    local suggest=$(
      shopt -s nullglob
      for dir in "$@"; do
         # work with absolute paths!
         cd "$dir" &>/dev/null || continue
         for file in *; do
             case $file in
             site-defaults*) ;; # skip site defaults
             *' '*) ;; # skip files with white spaces
             *.cfg) echo "${file%%.cfg}" ;;
             esac
         done
      done
    )

    COMPREPLY+=( $( compgen -W "$suggest" -- "$cur" ) )
    _filedir 'cfg'
}

_mock_isopt()
{
    [[ ${1:0:1} = - ]] && return 0
    case $1 in
    install|remove)
            return 0
            ;;
    esac
    return 1
}

_mock_isopt_multiarg() {
    local the_option=$1
    _mock_isopt "$the_option" || return 1
    case $the_option in
    --*)
        the_option=${the_option##--}
        ;;
    esac
    case $the_option in
    rebuild|install|installdeps|remove|chain|update|copyin|copyout|pm-cmd|yum-cmd|dnf-cmd|chroot|shell)
        return 0
        ;;
    esac
    return 1
}

_mock_parse_help()
{
    # PRECOMPILED_PARSED_MOCK_HELP
    _parse_help "$1"
}

_mock()
{
    local cur prev words cword split
    _init_completion -s || return

    local cfgdirs=( /etc/mock "$HOME/.config/mock" )
    local count=0
    local greedyopt=rebuild  # the default mode eating srpms
    local prevopt=rebuild

    for word in "${words[@]}" ; do
        if [[ $count -eq $cword ]] ; then
            # If the last (i.e. current) argument is an option, clear prevopt so that we complete
            # the current argument as an option instead an argument to prevopt
            _mock_isopt "$word" && prevopt=
            break
        fi
        # Record the option argument previous to the current argument to determine the type of
        # completion that is needed
        if _mock_isopt_multiarg "$word"; then
            prevopt=$word
            # last greedy option wins
            greedyopt=$word
        elif _mock_isopt "$word"; then
            prevopt=$word
        else
            # Revert back to the last greedy option.  E.g. for 'mock -r fedora-rawhide-x86_64 <tab>',
            # we want to react on the default 'rebuild' mode, not `-r`.
            prevopt=$greedyopt
        fi

        if [[ "$word" == --configdir ]] ; then
            cfgdirs=( "${words[((count+1))]}" )
        elif [[ "$word" == --configdir=* ]] ; then
            cfgdirs=( ${word/*=/} )
        fi
        count=$((++count))
    done

    case "$prevopt" in
        -h|--help|--version)
            # no further arguments are accepted after the above arguments
            return
            ;;
        --arch|--config-opts|-D|--define|--disablerepo|--enablerepo|--forcearch|--plugin-option|\
        --rpmbuild-opts|--rpmbuild_timeout|--scm-option|--uniqueext|--with|--without)
            # argument required but no completions available
            return
            ;;
        -r|--root|--chain)
            _mock_root "${cfgdirs[@]}"
            return
            ;;
        --configdir|--cwd|--resultdir|--rootdir)
            _filedir -d
            return
            ;;
        --copyin|--copyout|--macro-file|--sources)
            _filedir
            return
            ;;
        --spec)
            _filedir 'spec'
            return
            ;;
        --target)
            # Yep, compatible archs, not compatible build archs
            # (e.g. ix86 chroot builds in x86_64 mock host)
            # This would actually depend on what the target root
            # can be used to build for...
            COMPREPLY=( $( compgen -W "$( command rpm --showrc | \
                sed -ne 's/^\s*compatible\s\s*archs\s*:\s*\(.*\)/\1/i p' )" \
                -- "$cur" ) )
            return
            ;;
        --enable-plugin|--disable-plugin)
            COMPREPLY=( $( compgen -W "$( $1 $prev=DOES_NOT_EXIST 2>&1 | \
                sed -ne "s/[',]//g" -e 's/.*[[(]\([^])]*\)[])]/\1/p' )" \
                -- "$cur" ) ) #' unconfuse emacs
            return
            ;;
        --scrub)
            COMPREPLY=( $( compgen -W "all chroot cache root-cache c-cache
                yum-cache dnf-cache lvm overlayfs bootstrap" -- "$cur" ) )
            return
            ;;
        -i|--install|install)
            _filedir 'rpm'
            COMPREPLY=( $( compgen -W '"${COMPREPLY[@]}"' -X '*.src.rpm' ) )
            COMPREPLY=( $( compgen -W '"${COMPREPLY[@]}"' -X '*.nosrc.rpm' ) )
            [[ $cur != */* && $cur != [.~]* ]] && \
                declare -F _yum_list &>/dev/null && _yum_list all "$cur"
            return
            ;;
        --isolation)
            COMPREPLY=( $( compgen -W "auto simple nspawn" -- "$cur" ) )
            return
            ;;
        --remove|remove)
            declare -F _yum_list &>/dev/null && _yum_list all "$cur"
            return
            ;;
        --short-circuit)
            COMPREPLY=( $( compgen -W "install binary build prep" -- "$cur" ) )
            return
            ;;
    esac

    $split && return

    if [[ "$cur" == -* ]] ; then
        COMPREPLY=( $( compgen -W '$( _mock_parse_help "$1" )' -- "$cur" ) )
        # _parse_help fails to pick up --define (it's a parsing failure due to
        # the quoted 'MACRO EXPR' argument)
        COMPREPLY+=( $( compgen -W '--define=' -- "$cur" ) )
        [[ $COMPREPLY == *= ]] && compopt -o nospace
    else
        _filedir '@(?(no)src.r|s)pm'
    fi

} &&
complete -F _mock mock mock.py

_mock_parse_buildlog()
{
    local cur prev cword split
    _init_completion -s || return

    case "$prev" in
        -h|--help)
            # no further arguments are accepted after the above arguments
            return
            ;;
        -p|--path)
            _filedir
            return
            ;;
    esac

    $split && return

    if [[ $cword -eq 1 ]] ; then
        COMPREPLY=( $( compgen -W '$( _parse_help "$1" )' -- "$cur" ) )
        [[ $COMPREPLY == *= ]] && compopt -o nospace
    fi
} &&
complete -F _mock_parse_buildlog mock-parse-buildlog mock-parse-buildlog.py

# Local variables:
# mode: shell-script
# sh-basic-offset: 4
# sh-indent-comment: t
# indent-tabs-mode: nil
# End:
# ex: ts=4 sw=4 et filetype=sh
