# Bash completion for Pology scripts.

# Functionality depending on Bash version.
_wcompopt=0
_globnospace="-o nospace"
if [[ ${BASH_VERSINFO[0]} -ge 4 ]]; then
    _wcompopt=1
    _globnospace=
fi

shopt -s extglob


_posieve()
{
    local i cur prev opts prefix sieves sieve
    local IFS=$'\n'

    COMPREPLY=()

    [[ -z "$_posieve_options" ]] && _posieve_options=`posieve --list-options`
    [[ -z "$_posieve_sieves" ]] && _posieve_sieves=`posieve --list-sieve-names`

    # Check if sieves were already issued.
    i=1
    while [[ $i -lt $COMP_CWORD ]]; do
        prev="${COMP_WORDS[i]}"
        if [[ $prev =~ , ]] || [[ $prev == *.py ]]; then
            sieves=$prev
        else
            for sieve in $_posieve_sieves; do
                if [[ $sieve == $prev ]]; then
                    sieves=$prev
                    break
                fi
            done
        fi
        [[ -n "$sieves" ]] && break
        i=$((i + 1))
    done

    # Reassemble parameters if current sieve chain is different from previous.
    if [[ -n "$sieves" ]] && [[ $_posieve_lastsieves != $sieves ]]; then
        _posieve_lastsieves=$sieves
        _posieve_params=`posieve $sieves --list-sieve-params`
        # Add trailing space to non-colon ending parameters.
        _posieve_params=${_posieve_params//$'\n'/ $'\n'}' '
        _posieve_params=${_posieve_params//: /:}
        # Clean parameters, without trailing colons.
        _posieve_cparams=${_posieve_params//:/}
    fi

    [[ $_wcompopt == 1 ]] && compopt -o nospace

    cur="${COMP_WORDS[COMP_CWORD]}"
    prev="${COMP_WORDS[COMP_CWORD - 1]}"
    if    ( [[ $cur == -s* ]] || [[ $prev == -s ]] ) && [[ $cur != *:* ]] \
       && [[ -n "$sieves" ]]; \
    then
        if [[ $cur == -* ]]; then
            prefix=${cur%%s*}s
            cur=${cur#*s}
        fi
        COMPREPLY=($(compgen -W "$_posieve_params" -P "$prefix" -- $cur))
    elif ( [[ $cur == -S* ]] || [[ $prev == -S ]] ) && [[ -n "$sieves" ]]; then
        if [[ $cur == -* ]]; then
            prefix=${cur%%S*}S
            cur=${cur#*S}
        fi
        COMPREPLY=($(compgen -W "$_posieve_cparams" -P "$prefix" -- $cur))
    elif [[ $cur == -* ]] ; then
        COMPREPLY=($(compgen -W "$_posieve_options" -S " " -- $cur))
    elif [[ -z "$sieves" ]] ; then
        if [[ $cur =~ , ]]; then
            prefix=${cur%,*},
            cur=${cur##*,}
        fi
        COMPREPLY=($(compgen -W "$_posieve_sieves" -P "$prefix" -S " " -- $cur))
    else
        # Turn on trailing space after completion on files.
        [[ $_wcompopt == 1 ]] && compopt +o nospace
    fi
}
_posieve_options=
_posieve_sieves=
_posieve_lastsieves=
complete -F _posieve -o default $_globnospace posieve posieve.py


_poediff()
{
    local i cur prev opts prefix

    COMPREPLY=()

    [[ -z "$_poediff_options" ]] && _poediff_options=`poediff --list-options`
    [[ -z "$_poediff_vcs" ]] && _poediff_vcs=`poediff --list-vcs`

    cur="${COMP_WORDS[COMP_CWORD]}"
    prev="${COMP_WORDS[COMP_CWORD - 1]}"
    if [[ $cur == -*([[:alnum:]])c* ]] || [[ $prev == -*([[:alnum:]])c ]]; then
        if [[ $cur == -* ]]; then
            prefix=${cur%%c*}c
            cur=${cur#*c}
        fi
        COMPREPLY=($(compgen -W "$_poediff_vcs" -P "$prefix" -- $cur))
    elif [[ $cur == -* ]] ; then
        COMPREPLY=($(compgen -W "$_poediff_options" -- $cur))
    fi
}
_poediff_options=
_poediff_vcs=
complete -F _poediff -o default poediff poediff.py

