# bash completion for openldap

have ldapsearch && {
_ldap_uris()
{
    COMPREPLY=( $( compgen -W 'ldap:// ldaps://' -- "$cur" ) )
}

_ldap_protocols()
{
    COMPREPLY=( $( compgen -W '2 3' -- "$cur" ) )
}

_ldapsearch()
{
    local cur prev

    COMPREPLY=()
    _get_comp_words_by_ref cur prev

    case $prev in
        -h)
            _known_hosts_real "$cur"
            return 0
            ;;
        -H)
            _ldap_uris
            return 0
            ;;
        -T)
            _filedir -d
            return 0
            ;;
        -f|-y)
            _filedir
            return 0
            ;;
        -s)
            COMPREPLY=( $( compgen -W 'base one sub children' -- "$cur" ) )
            return 0
            ;;
        -a)
            COMPREPLY=( $( compgen -W 'never always search find' \
                -- "$cur" ) )
            return 0
            ;;
        -P)
            _ldap_protocols
            return 0
            ;;
    esac

    if [[ "$cur" == -* ]]; then
        COMPREPLY=( $( compgen -W '-n -u -v -t -tt -T -F -A -C -L -LL \
            -LLL -M -MM -S -d -f -x -D -W -w -y -H -h -p -b -s -a \
            -P -e -E -l -z -O -I -Q -U -R -X -Y -Z -ZZ' -- "$cur" ) )
    fi
}
complete -F _ldapsearch ldapsearch

_ldapaddmodify()
{
    local cur prev options

    COMPREPLY=()
    _get_comp_words_by_ref cur prev

    case $prev in
        -h)
            _known_hosts_real "$cur"
            return 0
            ;;
        -H)
            _ldap_uris
            return 0
            ;;
        -S|-f|-y)
            _filedir
            return 0
            ;;
        -P)
            _ldap_protocols
            return 0
            ;;
    esac

    if [[ "$cur" == -* ]]; then
        options='-c -S -n -v -M -MM -d -D -W -w -y -h -H -p -P -O -I \
            -Q -U -R -x -X -Y -Z -ZZ -f'
        if [[ ${COMP_WORDS[0]} == ldapmodify ]]; then
            options="$options -a"
        fi
        COMPREPLY=( $( compgen -W "$options" -- "$cur" ) )
    fi
}
complete -F _ldapaddmodify ldapadd ldapmodify

_ldapdelete()
{
    local cur prev

    COMPREPLY=()
    _get_comp_words_by_ref cur prev

    case $prev in
        -h)
            _known_hosts_real "$cur"
            return 0
            ;;
        -H)
            _ldap_uris
            return 0
            ;;
        -f|-y)
            _filedir
            return 0
            ;;
        -P)
            _ldap_protocols
            return 0
            ;;
    esac

    if [[ "$cur" == -* ]]; then
        COMPREPLY=( $( compgen -W '-n -v -c -M -MM -d -f -D -W -w -y \
            -H -h -P -p -O -U -R -r -x -I -Q -X -Y -Z -ZZ' -- "$cur" ) )
    fi
}
complete -F _ldapdelete ldapdelete

_ldapcompare()
{
    local cur prev

    COMPREPLY=()
    _get_comp_words_by_ref cur prev

    case $prev in
        -h)
            _known_hosts_real "$cur"
            return 0
            ;;
        -H)
            _ldap_uris
            return 0
            ;;
        -y)
            _filedir
            return 0
            ;;
        -P)
            _ldap_protocols
            return 0
            ;;
    esac

    if [[ "$cur" == -* ]]; then
        COMPREPLY=( $( compgen -W '-n -v -z -M -MM -d -D -W -w -y \
            -H -h -P -p -O -I -Q -U -R -x -X -Y -Z -ZZ' -- "$cur" ) )
    fi
}
complete -F _ldapcompare ldapcompare

_ldapmodrdn()
{
    local cur prev

    COMPREPLY=()
    _get_comp_words_by_ref cur prev

    case $prev in
        -h)
            _known_hosts_real "$cur"
            return 0
            ;;
        -H)
            _ldap_uris
            return 0
            ;;
        -f|-y)
            _filedir
            return 0
            ;;
        -P)
            _ldap_protocols
            return 0
            ;;
    esac

    if [[ "$cur" == -* ]]; then
        COMPREPLY=( $( compgen -W '-r -s -n -v -c -M -MM -d -D -W -w \
            -y -H -h -P -p -O -I -Q -U -R -x -X -Y -Z -ZZ -f' -- "$cur" ) )
    fi
}
complete -F _ldapmodrdn ldapmodrdn

_ldapwhoami()
{
    local cur prev

    COMPREPLY=()
    _get_comp_words_by_ref cur prev

    case $prev in
        -h)
            _known_hosts_real "$cur"
            return 0
            ;;
        -H)
            _ldap_uris
            return 0
            ;;
        -f|-y)
            _filedir
            return 0
            ;;
        -P)
            _ldap_protocols
            return 0
            ;;
    esac

    if [[ "$cur" == -* ]]; then
        COMPREPLY=( $( compgen -W '-n -v -z -d -D -W -w -y -H -h -p -P \
            -O -I -Q -U -R -x -X -Y -Z -ZZ' -- "$cur" ) )
    fi
}
complete -F _ldapwhoami ldapwhoami

_ldappasswd()
{
    local cur prev

    COMPREPLY=()
    _get_comp_words_by_ref cur prev

    case $prev in
        -h)
            _known_hosts_real "$cur"
            return 0
            ;;
        -H)
            _ldap_uris
            return 0
            ;;
        -t|-T|-y)
            _filedir
            return 0
            ;;
    esac

    if [[ "$cur" == -* ]]; then
        COMPREPLY=( $( compgen -W '-A -a -t -d -D -H -h -n -p -S -s -T \
            -v -W -w -y -O -I -Q -U -R -x -X -Y -Z -ZZ' -- "$cur" ) )
    fi
}
complete -F _ldappasswd ldappasswd
}

# 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
