# Bash completion for t2conf

_t2conf() {
    local cur prev words cword

    if type -t _init_completion &> /dev/null; then
        _init_completion -n = || return
    else
        COMPREPLY=()
        cur="${COMP_WORDS[COMP_CWORD]}"
        prev="${COMP_WORDS[COMP_CWORD-1]}"
    fi

    case "$prev" in
        -t|-p)
            if type -t _filedir &> /dev/null; then
                _filedir -d
            else
                compopt -o dirnames 2> /dev/null
                COMPREPLY=($(compgen -d -- "$cur"))
            fi
            return
            ;;
        -s|-r|--patch|--rpatch|-C|--config)
            if type -t _filedir &> /dev/null; then
                _filedir
            else
                compopt -o filenames 2> /dev/null
                COMPREPLY=($(compgen -f -- "$cur"))
            fi
            return
            ;;
        -D|-G)
            local plugins=(tranalyzer2 utils)
            for i in "$T2PLHOME/"*; do
                local b=$(basename "$i")
                if [ -d "$i" ] && [ "$b" != "t2PSkel" ]; then
                    plugins+=($b)
                fi
            done
            local pname
            if [ $COMP_CWORD -gt 2 ]; then
                for i in $(seq 1 $((COMP_CWORD-2))); do
                    if grep -Fw "${COMP_WORDS[i]}" <<< "${plugins[@]}" &> /dev/null; then
                        pname="${COMP_WORDS[i]}"
                    fi
                done
            fi
            local flags
            if [ -n "$pname" ]; then
                local path
                if [ "$pname" = "tranalyzer2" ] || [ "$pname" = "utils" ]; then
                    path="$T2HOME/$pname"
                else
                    path="$T2PLHOME/$pname"
                fi
                if [ -f "$path/default.config" ]; then
                    flags=($($AWK -F= '
                        /^#/ || NF < 2 { next }
                        {
                            print $1
                        }' "$path/default.config"))
                elif [ -f "$path/t2plconf" ]; then
                    flags=($($AWK '
                        /^ITEMS=/ {
                            start = 1
                            next
                        }
                        /^\)\s*$/ {
                            start = 0
                            exit
                        }
                        start && /^\s*[A-Z_]+\s*/ {
                            print $1
                        }' "$path/t2plconf"))
                else
                    flags=($($AWK '
                        !/^#/ && NF == 2 {
                            print $1
                        }' "$path/tests/$pname.flags"))
                fi
            fi
            COMPREPLY=($(compgen -W "${flags[*]}" -- "$cur"))
            return
            ;;
    esac

    local plugins=(tranalyzer2 utils)
    for i in "$T2PLHOME/"*; do
        local b=$(basename "$i")
        if [ -d "$i" ] && [ "$b" != "t2PSkel" ]; then
            plugins+=($b)
        fi
    done

    if [[ "$cur" = -* ]]; then
        COMPREPLY=($(compgen -W "$(_parse_help "$T2HOME/scripts/t2conf/t2conf")" -- "$cur"))
    else
        COMPREPLY=($(compgen -W "${plugins[*]}" -- "$cur"))
    fi
}

complete -F _t2conf t2conf
