## -*- sh -*-
## Analysis name completion for YODA scripts

## Load bash completion compatibility if zsh is used
if test -n "$ZSH_VERSION"; then
    autoload bashcompinit
    bashcompinit
fi


## Create a _filedir helper function if not already present
(type _filedir &> /dev/null) || \
function _filedir() {
    local cur prev commands options command
    cur="${COMP_WORDS[COMP_CWORD]}"
    COMPREPLY=( $(compgen -W "$(ls ${cur}* 2> /dev/null)" -- ${cur}) )
    return 0
}


##########################


function _yoda_config() {
    local cur prev commands options command
    COMPREPLY=()
    cur="${COMP_WORDS[COMP_CWORD]}"
    prev="${COMP_WORDS[COMP_CWORD-1]}"

    opts="--help --version"
    opts="$opts --prefix --includedir --libdir --pythonpath"
    opts="$opts --cxxflags --libs"
    opts="$opts -h"
    if [[ ${cur} = -* ]] ; then
        COMPREPLY=( $(compgen -W "${opts}" -- ${cur}) )
        if test -n "$COMPREPLY"; then return 0; fi
    fi

    COMPREPLY=( $(compgen -W "$opts" -- ${cur}) )
    if test -n "$COMPREPLY"; then
        return 0
    fi

    return 0
}


complete -F _yoda_config yoda-config


##############################


function _yodals() {
    local cur prev commands options command
    COMPREPLY=()
    cur="${COMP_WORDS[COMP_CWORD]}"
    prev="${COMP_WORDS[COMP_CWORD-1]}"

    opts="--help -h --quiet -q"

    if [[ ${cur} = -* ]] ; then
        COMPREPLY=( $(compgen -W "${opts}" -- ${cur}) )
        if test -n "$COMPREPLY"; then return 0; fi
    fi

    _filedir "yoda|yoda.gz"
    return 0
}


complete -F _yodals -o default yodals


##############################


function _yodadiff() {
    local cur prev commands options command
    COMPREPLY=()
    cur="${COMP_WORDS[COMP_CWORD]}"
    prev="${COMP_WORDS[COMP_CWORD-1]}"

    opts="--help -h --output -o --tol -t"

    if [[ ${cur} = -* ]] ; then
        COMPREPLY=( $(compgen -W "${opts}" -- ${cur}) )
        if test -n "$COMPREPLY"; then return 0; fi
    fi

    _filedir "yoda|yoda.gz"
    return 0
}


complete -F _yodadiff -o default yodadiff


##############################


function _yodamerge() {
    local cur prev commands options command
    COMPREPLY=()
    cur="${COMP_WORDS[COMP_CWORD]}"
    prev="${COMP_WORDS[COMP_CWORD-1]}"

    opts="--help -h --output -o --match -m --unmatch -M -v --verbose -q --quiet"

    if [[ ${cur} = -* ]] ; then
        COMPREPLY=( $(compgen -W "${opts}" -- ${cur}) )
        if test -n "$COMPREPLY"; then return 0; fi
    fi

    _filedir "yoda|yoda.gz"
    return 0
}


complete -F _yodamerge -o default yodamerge


##############################


function _yodastack() {
    local cur prev commands options command
    COMPREPLY=()
    cur="${COMP_WORDS[COMP_CWORD]}"
    prev="${COMP_WORDS[COMP_CWORD-1]}"

    opts="--help -h --output -o --match -m --unmatch -M -v --verbose -q --quiet"

    if [[ ${cur} = -* ]] ; then
        COMPREPLY=( $(compgen -W "${opts}" -- ${cur}) )
        if test -n "$COMPREPLY"; then return 0; fi
    fi

    _filedir "yoda|yoda.gz"
    return 0
}


complete -F _yodastack -o default yodastack


##############################


function _yodascale() {
    local cur prev commands options command
    COMPREPLY=()
    cur="${COMP_WORDS[COMP_CWORD]}"
    prev="${COMP_WORDS[COMP_CWORD-1]}"

    opts="--help -h --refs -r --ref-prefix"

    if [[ ${cur} = -* ]] ; then
        COMPREPLY=( $(compgen -W "${opts}" -- ${cur}) )
        if test -n "$COMPREPLY"; then return 0; fi
    fi

    _filedir "yoda|yoda.gz"
    return 0
}


complete -F _yodascale -o default yodascale


##############################


function _yodaenvelope() {
    local cur prev commands options command
    COMPREPLY=()
    cur="${COMP_WORDS[COMP_CWORD]}"
    prev="${COMP_WORDS[COMP_CWORD-1]}"

    opts="--help -h --refs -r"

    if [[ ${cur} = -* ]] ; then
        COMPREPLY=( $(compgen -W "${opts}" -- ${cur}) )
        if test -n "$COMPREPLY"; then return 0; fi
    fi

    _filedir "yoda|yoda.gz"
    return 0
}


complete -F _yodaenvelope -o default yodaenvelope


##############################


function _yodahist() {
    local cur prev commands options command
    COMPREPLY=()
    cur="${COMP_WORDS[COMP_CWORD]}"
    prev="${COMP_WORDS[COMP_CWORD-1]}"

    opts="--help -h --quiet -q"
    cmds1a="hist1 prof1"
    cmds1b="hist2 prof2"
    cmds1c="scat2"
    cmds2="path title xlabel ylabel ann in out"
    cmds3="logx logy show xlogbins ylogbins"

    if [[ ${cur} = -* ]] ; then
        COMPREPLY=( $(compgen -W "${opts}" -- ${cur}) )
        return 0
    else
        ## First command must be a bin definition
        if [[ ${prev} = "yodahist" ]] ; then
            COMPREPLY=( $(compgen -W "$cmds1a $cmds1b $cmds1c" -- ${cur}) )
            return 0;
        fi
        ## Print help for bin defn commands
        for c in $cmds1a; do
            if [[ ${prev} = $c ]] ; then
                # echo -e "\nnx xlow xhigh"
                return 0
            fi
        done
        for c in $cmds1b; do
            if [[ ${prev} = $c ]] ; then
                # echo -e "\nnx xlow xhigh ny ylow yhigh"
                return 0
            fi
        done
        ## Single freeform arg commands can't be completed
        for c in $cmds2; do
            if [[ ${prev} = $c ]] ; then return 0; fi
        done
        ## Boolean arg commands
        if [[ ${prev} =~ logx|logy|show ]] ; then
            COMPREPLY=( $(compgen -W "yes no true false on off 1 0" -- ${cur}) )
            return 0
        fi
        ## Fall back to suggesting all non-bin defn commands
        COMPREPLY=( $(compgen -W "$cmds2 $cmds3" -- ${cur}) )
    fi

    return 0
}


complete -F _yodahist -o default yodahist


##############################


function _yodacmp() {
    local cur prev commands options command
    COMPREPLY=()
    cur="${COMP_WORDS[COMP_CWORD]}"
    prev="${COMP_WORDS[COMP_CWORD-1]}"

    opts="--help -h --outdir -o --config -c --match -m --unmatch -M"

    if [[ ${cur} = -* ]] ; then
        COMPREPLY=( $(compgen -W "${opts}" -- ${cur}) )
        if test -n "$COMPREPLY"; then return 0; fi
    fi

    _filedir "yoda|yoda.gz"
    return 0
}


complete -F _yodacmp -o default yodacmp


##############################


function _yodaplot() {
    local cur prev commands options command
    COMPREPLY=()
    cur="${COMP_WORDS[COMP_CWORD]}"
    prev="${COMP_WORDS[COMP_CWORD-1]}"

    opts="--help -h --format -f --nproc -n --debug --quiet"

    if [[ ${cur} = -* ]] ; then
        COMPREPLY=( $(compgen -W "${opts}" -- ${cur}) )
    elif [[ ${prev} =~ "--format|-f" ]] ; then
        COMPREPLY=( $(compgen -W "PDF PNG TEX PGF" -- ${cur}) )
    fi
    if test -n "$COMPREPLY"; then return 0; fi

    _filedir "yoda|yoda.gz"
    return 0
}


complete -F _yodaplot -o default yodaplot


##############################


function _yodacnv() {
    local cur prev commands options command
    COMPREPLY=()
    cur="${COMP_WORDS[COMP_CWORD]}"
    prev="${COMP_WORDS[COMP_CWORD-1]}"

    opts="--help -h --match -m --unmatch -M"

    if [[ ${cur} = -* ]] ; then
        COMPREPLY=( $(compgen -W "${opts}" -- ${cur}) )
        if test -n "$COMPREPLY"; then return 0; fi
    fi

    _filedir "yoda|yoda.gz"
    return 0
}


complete -F _yodacnv -o default yodacnv


#############################


function _yoda2aida() {
    local cur prev commands options command
    COMPREPLY=()
    cur="${COMP_WORDS[COMP_CWORD]}"
    prev="${COMP_WORDS[COMP_CWORD-1]}"

    opts="--help -h --match -m --unmatch -M"

    if [[ ${cur} = -* ]] ; then
        COMPREPLY=( $(compgen -W "${opts}" -- ${cur}) )
        if test -n "$COMPREPLY"; then return 0; fi
    fi

    _filedir "yoda|yoda.gz"
    return 0
}


complete -F _yoda2aida -o default yoda2aida


##############################


function _yoda2flat() {
    local cur prev commands options command
    COMPREPLY=()
    cur="${COMP_WORDS[COMP_CWORD]}"
    prev="${COMP_WORDS[COMP_CWORD-1]}"

    opts="--help -h --match -m --unmatch -M"

    if [[ ${cur} = -* ]] ; then
        COMPREPLY=( $(compgen -W "${opts}" -- ${cur}) )
        if test -n "$COMPREPLY"; then return 0; fi
    fi

    _filedir "yoda|yoda.gz"
    return 0
}


complete -F _yoda2flat -o default yoda2flat


##############################


function _yoda2root() {
    local cur prev commands options command
    COMPREPLY=()
    cur="${COMP_WORDS[COMP_CWORD]}"
    prev="${COMP_WORDS[COMP_CWORD-1]}"

    opts="--help -h --match -m --unmatch -M"

    if [[ ${cur} = -* ]] ; then
        COMPREPLY=( $(compgen -W "${opts}" -- ${cur}) )
        if test -n "$COMPREPLY"; then return 0; fi
    fi

    _filedir "yoda|yoda.gz"
    return 0
}


complete -F _yoda2root -o default yoda2root


##############################


function _aida2yoda() {
    local cur prev commands options command
    COMPREPLY=()
    cur="${COMP_WORDS[COMP_CWORD]}"
    prev="${COMP_WORDS[COMP_CWORD-1]}"

    opts="--help -h --match -m --unmatch -M"

    if [[ ${cur} = -* ]] ; then
        COMPREPLY=( $(compgen -W "${opts}" -- ${cur}) )
        if test -n "$COMPREPLY"; then return 0; fi
    fi

    _filedir aida
    return 0
}


complete -F _aida2yoda -o default aida2yoda


##############################


function _aida2flat() {
    local cur prev commands options command
    COMPREPLY=()
    cur="${COMP_WORDS[COMP_CWORD]}"
    prev="${COMP_WORDS[COMP_CWORD-1]}"

    opts="--help -h --match -m --unmatch -M"

    if [[ ${cur} = -* ]] ; then
        COMPREPLY=( $(compgen -W "${opts}" -- ${cur}) )
        if test -n "$COMPREPLY"; then return 0; fi
    fi

    _filedir aida
    return 0
}


complete -F _aida2flat -o default aida2flat


##############################


function _flat2yoda() {
    local cur prev commands options command
    COMPREPLY=()
    cur="${COMP_WORDS[COMP_CWORD]}"
    prev="${COMP_WORDS[COMP_CWORD-1]}"

    opts="--help -h --match -m --unmatch -M"

    if [[ ${cur} = -* ]] ; then
        COMPREPLY=( $(compgen -W "${opts}" -- ${cur}) )
        if test -n "$COMPREPLY"; then return 0; fi
    fi

    _filedir dat
    return 0
}


complete -F _aida2flat -o default aida2flat
