#------------------------------------------------------------------------------
#   This file is part of the Code_Saturne Kernel, element of the
#   Code_Saturne CFD tool.
#
#   Copyright (C) 2009-2011 EDF S.A., France
#
#   The Code_Saturne Kernel is free software; you can redistribute it
#   and/or modify it under the terms of the GNU General Public License
#   as published by the Free Software Foundation; either version 2 of
#   the License, or (at your option) any later version.
#
#   The Code_Saturne Kernel is distributed in the hope that it will be
#   useful, but WITHOUT ANY WARRANTY; without even the implied warranty
#   of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#   GNU General Public License for more details.
#
#   You should have received a copy of the GNU General Public Licence
#   along with the Code_Saturne Kernel; if not, write to the
#   Free Software Foundation, Inc.,
#   51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
#-------------------------------------------------------------------------------

# Batch completion script for:
# - "code_saturne" user Python script
# - "cs_solver" binary executable

_code_saturne() {

    local cur prev opt cmds cmdOpts opt i

    COMPREPLY=()

    cur="${COMP_WORDS[COMP_CWORD]}"
    prev="${COMP_WORDS[COMP_CWORD-1]}"

    # Possible commands except "help", as given by "code_saturne help"
    cmds="autovnv compile config create gui info run salome"

    # Help and Case options
    local helpOpts="-h --help"
    local caseOpts="-c --case"

    # Readers and guides
    local readers="okular evince kpdf gpdf xpdf acroread"
    local guides="refcard user theory tutorial developer Doxygen"

    # Boolean values
    local isCmd=0
    local isOptCase=0

    if [[ ${COMP_CWORD} -eq 1 ]]; then
        COMPREPLY=( $(compgen -W "help ${cmds}" -- ${cur}) )
        return 0
    fi

    # Certain tokens preclude further activity
    if [[ ${prev} == @(-h|--help|-v|--version) ]]; then
        return 0
    fi

    # Check if previous token is a command or not
    [[ ${prev} == @(${cmds// /|}) ]] && isCmd=1

    # Parse arguments and set various variables about what was found.
    #
    # cmd: the current command if available
    local cmd=${COMP_WORDS[1]}

    # Check options depending on the command and the previous token
    case ${cmd} in
        help)
            case ${prev} in
                help)
                    COMPREPLY=( $(compgen -W "${cmds}" -- ${cur}) )
                    return 0
                    ;;
                *) return 0;;
            esac
            ;;
        autovnv)
            case ${prev} in
                -f|--file) _filedir -d; return 0;;
                -m|--mail) COMPREPLY=( ); return 0;;
                *) cmdOpts="-q --quiet -r --run -c --compare -p --post";;
            esac
            ;;
        compile)
            case ${prev} in
                -d|--dest)   _filedir -d; return 0;;
                -s|--source) _filedir -d; return 0;;
                *) cmdOpts="-t --test -f --force -k -keep-going \
                     -s --source -d --dest --opt-libs --syrthes";;
            esac
            ;;
        config)
            case ${prev} in
                --have)               COMPREPLY=( ); return 0;;
                --cppflags|--ldflags) COMPREPLY=( ); return 0;;
                --libs|--deplibs)     COMPREPLY=( ); return 0;;
                *) cmdOpts="--cc --cxx --fc --cflags --cxxflags --fcflags \
                     --rpath --pyuic4 --pyrcc4 \
                     --have --cppflags --ldflags --libs --deplibs";;
            esac
            ;;
        create)
            case ${prev} in
                -c|--case)     COMPREPLY=( ); return 0;;
                -s|--study)    COMPREPLY=( ); return 0;;
                --syrthes|--aster) COMPREPLY=( ); return 0;;
                --copy-from)   _filedir -d; return 0;;
                *) cmdOpts="-s --study -c --case --noref --copy-from \
                     --syrthes --aster -q --quiet -v --verbose";;
            esac
            ;;
        gui)
            case ${prev} in
                -p|--file)  _filedir; return 0;;
                -b|--batch) COMPREPLY=( ); return 0;;
                *) cmdOpts="-p --param -n --new -z --no-splash";;
            esac
            ;;
        info)
            case ${prev} in
                -r|--reader)
                    COMPREPLY=( $(compgen -W "${readers}" -- ${cur}) )
                    return 0
                    ;;
                -g|--guide)
                    COMPREPLY=( $(compgen -W "${guides}" -- ${cur}) )
                    return 0
                    ;;
                *) cmdOpts="-r --reader -g --guide --version"
            esac
            ;;
        run)
            case ${prev} in
                -p|--param)  _filedir; return 0;;
                --case)      _filedir -d; return 0;;
                --id)        COMPREPLY=( ); return 0;;
                --id-prefix) COMPREPLY=( ); return 0;;
                --id-suffix) COMPREPLY=( ); return 0;;
                *) cmdOpts="-p --param --case --id --id-prefix --id-suffix \
                     --suggest-id --force --initialize --execute --finalize";;
            esac
            ;;
        *)
            cmdOpts=""
            ;;
    esac

    # Take out options already given
    for (( i=2; i<=$COMP_CWORD-1; ++i ))
    do
        opt=${COMP_WORDS[$i]}

        local optBase

        # Remove leading dashes and arguments
        case $opt in
            --*)    optBase=${opt/=*/} ;;
            -*)     optBase=${opt:0:2} ;;
        esac

        cmdOpts=" $cmdOpts "
        cmdOpts=${cmdOpts/ ${optBase} / }

        # Take out alternatives and mutually exclusives
        case $optBase in
            --aster)         cmdOpts=${cmdOpts/ --syrthes / } ;;
            -c)              cmdOpts=${cmdOpts/ --case / }; isOptcase=1 ;;
            --case)          cmdOpts=${cmdOpts/ -c / }; isOptcase=1 ;;
            -d)              cmdOpts=${cmdOpts/ --dest / } ;;
            --dest)          cmdOpts=${cmdOpts/ -d / } ;;
            -f)              cmdOpts=${cmdOpts/ --file / } ;;
            --file)          cmdOpts=${cmdOpts/ -f / } ;;
            -g)              cmdOpts=${cmdOpts/ --guide / } ;;
            --guide)         cmdOpts=${cmdOpts/ -g / } ;;
            -k)              cmdOpts=${cmdOpts/ --keep-going / } ;;
            --keep-going)    cmdOpts=${cmdOpts/ -k / } ;;
            -n)              cmdOpts=${cmdOpts/ --new / } ;;
            --new)           cmdOpts=${cmdOpts/ -n / }
                             cmdOpts=${cmdOpts/ --file / } ;;
            --no-splash)     cmdOpts=${cmdOpts/ -z / } ;;
            --opt-libs)      cmdOpts=${cmdOpts/ --syrthes / } ;;
            -p)              cmdOpts=${cmdOpts/ --param / } ;;
            --param)         cmdOpts=${cmdOpts/ -p / } ;;
            -q)              cmdOpts=${cmdOpts/ --quiet / } ;;
            --quiet)         cmdOpts=${cmdOpts/ -q / } ;;
            -r)              cmdOpts=${cmdOpts/ --reader / } ;;
            --reader)        cmdOpts=${cmdOpts/ -r / } ;;
            -s)
                case ${cmd} in
                    compile) cmdOpts=${cmdOpts/ --source / } ;;
                    create)  cmdOpts=${cmdOpts/ --study / } ;;
                esac ;;
            --source)        cmdOpts=${cmdOpts/ -s / } ;;
            --study)         cmdOpts=${cmdOpts/ -s / } ;;
            --syrthes)
                case ${cmd} in
                    compile) cmdOpts=${cmdOpts/ --opt-libs / } ;;
                    create)  cmdOpts=${cmdOpts/ --aster / } ;;
                esac ;;
            -t)              cmdOpts=${cmdOpts/ --test / } ;;
            --test)          cmdOpts=${cmdOpts/ -t / } ;;
            -v)              cmdOpts=${cmdOpts/ --verbose / } ;;
            --verbose)       cmdOpts=${cmdOpts/ -v / } ;;
            -z)              cmdOpts=${cmdOpts/ --no-splash / } ;;
        esac

    done

    # Add case options (may appear several times in command line)
    [[ $isOptCase -eq 1 ]] && cmdOpts="${caseOpts} ${cmdOpts}"

    # Add help options for every command but "help"
    [[ $isCmd -eq 1 ]] && cmdOpts="${helpOpts} ${cmdOpts}"


    COMPREPLY=( ${COMPREPLY[@]} $(compgen -W "${cmdOpts}" -- ${cur}) )

}
complete -F _code_saturne code_saturne
