4c9460
# bash completion for rpm                                  -*- shell-script -*-
4c9460
4c9460
# helper functions
4c9460
4c9460
_rpm_installed_packages()
4c9460
{
4c9460
    if [[ -r /var/log/rpmpkgs && \
4c9460
        /var/log/rpmpkgs -nt /var/lib/rpm/Packages ]]; then
4c9460
        # using RHL 7.2 or later - this is quicker than querying the DB
4c9460
        COMPREPLY=( $( compgen -W "$( sed -ne \
4c9460
            's|^\([^[:space:]]\{1,\}\)-[^[:space:]-]\{1,\}-[^[:space:]-]\{1,\}\.rpm$|\1|p' \
4c9460
            /var/log/rpmpkgs )" -- "$cur" ) )
4c9460
    elif type rpmqpack &>/dev/null ; then
4c9460
        # SUSE's rpmqpack is faster than rpm -qa
4c9460
        COMPREPLY=( $( compgen -W '$( rpmqpack )' -- "$cur" ) )
4c9460
    else
4c9460
        COMPREPLY=( $( ${1:-rpm} -qa --nodigest --nosignature \
4c9460
            --queryformat='%{NAME} ' "$cur*" 2>/dev/null ) )
4c9460
    fi
4c9460
}
4c9460
4c9460
_rpm_groups()
4c9460
{
4c9460
    local IFS=$'\n'
4c9460
    COMPREPLY=( $( compgen -W "$( ${1:-rpm} -qa --nodigest --nosignature \
4c9460
        --queryformat='%{GROUP}\n' 2>/dev/null )" -- "$cur" ) )
4c9460
}
4c9460
4c9460
_rpm_macros()
4c9460
{
4c9460
    # get a list of macros
4c9460
    COMPREPLY=( $( compgen -W "$( ${1:-rpm} --showrc | sed -ne \
4c9460
        's/^-\{0,1\}[0-9]\{1,\}[:=][[:space:]]\{1,\}\([^[:space:](]\{3,\}\).*/%\1/p' )" \
4c9460
        -- "$cur" ) )
4c9460
}
4c9460
4c9460
_rpm_buildarchs()
4c9460
{
4c9460
    COMPREPLY=( $( compgen -W "$( ${1:-rpm} --showrc | sed -ne \
4c9460
        's/^\s*compatible\s\s*build\s\s*archs\s*:\s*\(.*\)/\1/ p' )" \
4c9460
        -- "$cur" ) )
4c9460
}
4c9460
4c9460
# rpm(8) completion
4c9460
#
4c9460
_rpm()
4c9460
{
4c9460
    local cur prev words cword split
4c9460
    _init_completion -s || return
4c9460
4c9460
    if [[ $cword -eq 1 ]]; then
4c9460
        # first parameter on line
4c9460
        case $cur in
4c9460
            --*)
4c9460
                COMPREPLY=( $( compgen -W '--help --version --initdb
4c9460
                    --checksig --addsign --delsign --rebuilddb --showrc
4c9460
                    --setperms --setugids --eval --install --upgrade --query
4c9460
                    --freshen --erase --verify --querytags --import' \
4c9460
                        -- "$cur" ) )
4c9460
                ;;
4c9460
            *)
4c9460
                COMPREPLY=( $( compgen -W '-e -E -F -i -q -t -U -V' \
4c9460
                    -- "$cur" ) )
4c9460
                ;;
4c9460
        esac
4c9460
        return 0
4c9460
    fi
4c9460
4c9460
    case $prev in
4c9460
        --dbpath|--excludepath|--prefix|--relocate|--root|-r)
4c9460
            _filedir -d
4c9460
            return 0
4c9460
            ;;
4c9460
        --eval|-E)
4c9460
            _rpm_macros $1
4c9460
            return 0
4c9460
            ;;
4c9460
        --pipe)
4c9460
            compopt -o filenames
4c9460
            COMPREPLY=( $( compgen -c -- "$cur" ) )
4c9460
            return 0
4c9460
            ;;
4c9460
        --rcfile)
4c9460
            _filedir
4c9460
            return 0
4c9460
            ;;
4c9460
        --specfile)
4c9460
            # complete on .spec files
4c9460
            _filedir spec
4c9460
            return 0
4c9460
            ;;
4c9460
        --whatprovides)
4c9460
            if [[ "$cur" == */* ]]; then
4c9460
                _filedir
4c9460
            else
4c9460
                # complete on capabilities
4c9460
                local IFS=$'\n'
4c9460
                COMPREPLY=( $( compgen -W "$( $1 -qa --nodigest --nosignature \
4c9460
                    --queryformat='%{PROVIDENAME}\n' 2>/dev/null )" \
4c9460
                    -- "$cur" ) )
4c9460
            fi
4c9460
            return 0
4c9460
            ;;
4c9460
        --whatrequires)
4c9460
            if [[ "$cur" == */* ]]; then
4c9460
                _filedir
4c9460
            else
4c9460
                # complete on capabilities
4c9460
                local IFS=$'\n'
4c9460
                COMPREPLY=( $( compgen -W "$( $1 -qa --nodigest --nosignature \
4c9460
                    --queryformat='%{REQUIRENAME}\n' 2>/dev/null )" \
4c9460
                    -- "$cur" ) )
4c9460
            fi
4c9460
            return 0
4c9460
            ;;
4c9460
        --define|-D|--fileid|--hdrid|--pkgid)
4c9460
            # argument required but no completions available
4c9460
            return 0
4c9460
            ;;
4c9460
    esac
4c9460
4c9460
    $split && return 0
4c9460
4c9460
    # options common to all modes
4c9460
    local opts="--define= --eval= --macros= --nodigest --nosignature --rcfile=
4c9460
        --quiet --pipe --verbose"
4c9460
4c9460
    case ${words[1]} in
4c9460
        -[iFU]*|--install|--freshen|--upgrade)
4c9460
            if [[ "$cur" == -* ]]; then
4c9460
                COMPREPLY=( $( compgen -W "$opts --percent --force --test
4c9460
                --replacepkgs --replacefiles --root --excludedocs --includedocs
4c9460
                --noscripts --ignorearch --dbpath --prefix= --ignoreos --nodeps
4c9460
                --allfiles --ftpproxy --ftpport --justdb --httpproxy --httpport
4c9460
                --noorder --relocate= --badreloc --notriggers --excludepath=
4c9460
                --ignoresize --oldpackage --queryformat --repackage
4c9460
                --nosuggests" -- "$cur" ) )
4c9460
            else
4c9460
                _filedir '[rs]pm'
4c9460
            fi
4c9460
            ;;
4c9460
        -e|--erase)
4c9460
            if [[ "$cur" == -* ]]; then
4c9460
                COMPREPLY=( $( compgen -W "$opts --allmatches --noscripts
4c9460
                    --notriggers --nodeps --test --repackage" -- "$cur" ) )
4c9460
            else
4c9460
                _rpm_installed_packages $1
4c9460
            fi
4c9460
            ;;
4c9460
        -q*|--query)
4c9460
            # options common to all query types
4c9460
            opts+=" --changelog --configfiles --conflicts --docfiles --dump
4c9460
                --enhances --filesbypkg --filecaps --fileclass --filecolor
4c9460
                --fileprovide --filerequire --filesbypkg --info --list
4c9460
                --obsoletes --pipe --provides --queryformat= --requires
4c9460
                --scripts --suggests --triggers --xml"
4c9460
4c9460
            if [[ ${words[@]} == *\ -@(*([^ -])f|-file )* ]]; then
4c9460
                # -qf completion
4c9460
                if [[ "$cur" == -* ]]; then
4c9460
                    COMPREPLY=( $( compgen -W "$opts --dbpath --fscontext
4c9460
                        --last --root --state" -- "$cur" ) )
4c9460
                else
4c9460
                    _filedir
4c9460
                fi
4c9460
            elif [[ ${words[@]} == *\ -@(*([^ -])g|-group )* ]]; then
4c9460
                # -qg completion
4c9460
                _rpm_groups $1
4c9460
            elif [[ ${words[@]} == *\ -@(*([^ -])p|-package )* ]]; then
4c9460
                # -qp; uninstalled package completion
4c9460
                if [[ "$cur" == -* ]]; then
4c9460
                    COMPREPLY=( $( compgen -W "$opts --ftpport --ftpproxy
4c9460
                        --httpport --httpproxy --nomanifest" -- "$cur" ) )
4c9460
                else
4c9460
                    _filedir '[rs]pm'
4c9460
                fi
4c9460
            else
4c9460
                # -q; installed package completion
4c9460
                if [[ "$cur" == -* ]]; then
4c9460
                    COMPREPLY=( $( compgen -W "$opts --all --file --fileid
4c9460
                        --dbpath --fscontext --ftswalk --group --hdrid --last
4c9460
                        --package --pkgid --root= --specfile --state
4c9460
                        --triggeredby --whatprovides --whatrequires" \
4c9460
                            -- "$cur" ) )
4c9460
                elif [[ ${words[@]} != *\ -@(*([^ -])a|-all )* ]]; then
4c9460
                    _rpm_installed_packages $1
4c9460
                fi
4c9460
            fi
4c9460
            ;;
4c9460
        -K*|--checksig)
4c9460
            if [[ "$cur" == -* ]]; then
4c9460
                COMPREPLY=( $( compgen -W "$opts --nopgp --nogpg --nomd5" \
4c9460
                    -- "$cur" ) )
4c9460
            else
4c9460
                _filedir '[rs]pm'
4c9460
            fi
4c9460
            ;;
4c9460
        -[Vy]*|--verify)
4c9460
            if [[ "$cur" == -* ]]; then
4c9460
                COMPREPLY=( $( compgen -W "$opts --root= --dbpath --nodeps
4c9460
                    --nogroup --nolinkto --nomode --nomtime --nordev --nouser
4c9460
                    --nofiles --noscripts --nomd5 --querytags --specfile
4c9460
                    --whatrequires --whatprovides" -- "$cur" ) )
4c9460
            # check whether we're doing file completion
4c9460
            elif [[ ${words[@]} == *\ -@(*([^ -])f|-file )* ]]; then
4c9460
                _filedir
4c9460
            elif [[ ${words[@]} == *\ -@(*([^ -])g|-group )* ]]; then
4c9460
                _rpm_groups $1
4c9460
            elif [[ ${words[@]} == *\ -@(*([^ -])p|-package )* ]]; then
4c9460
                _filedir '[rs]pm'
4c9460
            else
4c9460
                _rpm_installed_packages $1
4c9460
            fi
4c9460
            ;;
4c9460
        --resign|--addsign|--delsign)
4c9460
            _filedir '[rs]pm'
4c9460
            ;;
4c9460
        --setperms|--setgids)
4c9460
            _rpm_installed_packages $1
4c9460
            ;;
4c9460
        --import|--dbpath|--root)
4c9460
            if [[ "$cur" == -* ]]; then
4c9460
                COMPREPLY=( $( compgen -W '--import --dbpath --root=' \
4c9460
                    -- "$cur" ) )
4c9460
            else
4c9460
                _filedir
4c9460
            fi
4c9460
            ;;
4c9460
    esac
4c9460
    [[ $COMPREPLY == *= ]] && compopt -o nospace
4c9460
4c9460
    return 0
4c9460
} &&
4c9460
complete -F _rpm rpm
4c9460
4c9460
_rpmbuild()
4c9460
{
4c9460
    local cur prev words cword split
4c9460
    _init_completion -s || return
4c9460
4c9460
    local rpm="${1%build*}"
4c9460
    [[ $rpm == $1 ]] || ! type $rpm &>/dev/null && rpm=
4c9460
4c9460
    case $prev in
4c9460
        --buildroot|--root|-r|--dbpath)
4c9460
            _filedir -d
4c9460
            return 0
4c9460
            ;;
4c9460
        --target)
4c9460
            _rpm_buildarchs
4c9460
            return 0
4c9460
            ;;
4c9460
        --eval|-E)
4c9460
            _rpm_macros $rpm
4c9460
            return 0
4c9460
            ;;
4c9460
        --macros|--rcfile)
4c9460
            _filedir
4c9460
            return 0
4c9460
            ;;
4c9460
        --buildpolicy)
4c9460
            local cfgdir=$( $rpm --eval '%{_rpmconfigdir}' 2>/dev/null )
4c9460
            if [[ $cfgdir ]]; then
4c9460
                COMPREPLY=( $( compgen -W "$( command ls $cfgdir 2>/dev/null \
4c9460
                    | sed -ne 's/^brp-//p' )" -- "$cur" ) )
4c9460
            fi
4c9460
            ;;
4c9460
        --define|-D|--with|--without)
4c9460
            return 0
4c9460
            ;;
4c9460
    esac
4c9460
4c9460
    $split && return 0
4c9460
4c9460
    if [[ $cur == -* ]]; then
4c9460
        COMPREPLY=( $( compgen -W "$( _parse_help "$1" )" -- "$cur" ) )
4c9460
        [[ $COMPREPLY == *= ]] && compopt -o nospace
4c9460
        return 0
4c9460
    fi
4c9460
4c9460
    # Figure out file extensions to complete
4c9460
    local word ext
4c9460
    for word in ${words[@]}; do
4c9460
        case $word in
4c9460
            -b?)
4c9460
                ext=spec
4c9460
                break
4c9460
                ;;
4c9460
            -t?|--tarbuild)
4c9460
                ext='@(t?(ar.)@([gx]z|bz?(2))|tar?(.@(lzma|Z)))'
4c9460
                break
4c9460
                ;;
4c9460
            --rebuild|--recompile)
4c9460
                ext='@(?(no)src.r|s)pm'
4c9460
                break
4c9460
                ;;
4c9460
        esac
4c9460
    done
4c9460
    [[ -n $ext ]] && _filedir $ext
4c9460
} &&
4c9460
complete -F _rpmbuild rpmbuild rpmbuild-md5
4c9460
4c9460
# ex: ts=4 sw=4 et filetype=sh