###############################################################################
#
#    HPCC SYSTEMS software Copyright (C) 2012 HPCC Systems®.
#
#    Licensed under the Apache License, Version 2.0 (the "License");
#    you may not use this file except in compliance with the License.
#    You may obtain a copy of the License at
#
#       http://www.apache.org/licenses/LICENSE-2.0
#
#    Unless required by applicable law or agreed to in writing, software
#    distributed under the License is distributed on an "AS IS" BASIS,
#    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
#    See the License for the specific language governing permissions and
#    limitations under the License.
###############################################################################

# Bash completion file for ecl
#
# To test, run ". ./ecl" and then test completion on ecl
# File must be copied/linked from /etc/bash_completion.d/ to work seamlessly
# You must have ". /etc/bash_completion" on your bashrc file (you probably have it)

#set -x

_flag_option()
{
    case "$1" in
        "--help" | "-v" | "--verbose" | "--no-reload" | "--no-files" | "-A" | "--activate" | "-O" | "--overwrite" | "--ecl-only" | "--legacy")
            echo 1
            ;;
        *)
            echo 0
            ;;
    esac
}

_ecl_opts_file()
{
    local prev="${COMP_WORDS[COMP_CWORD-1]}"
    if [ "$prev" = "=" ]
    then
       prev="${COMP_WORDS[COMP_CWORD-2]}"
    fi
    local isFlagOption=$(_flag_option $prev)
    if [ "$isFlagOption" = "1" ] || [ "${prev:0:1}" != "-" ] || [ "$prev" = "--manifest" ]
    then
       echo "<filelist>"
    fi
}

_ecl_nextcommand()
{
    local value=0
    local pos=$(($1))
    for ((; pos<$COMP_CWORD; pos++))
    do
        cur="${COMP_WORDS[pos]}"
        ch="${cur:0:1}"
        if [ "$ch" == "=" ]
        then
            pos=$(($pos + 1))
        elif [ "$ch" != "-" ]
        then
            value=$(($pos))
            break
        else
            isFlagOption=$(_flag_option $cur )
            if [ "$isFlagOption" -eq "0" ] && [ "${COMP_WORDS[pos+1]}" != "=" ]
            then
               pos=$(($pos + 1))
            fi
        fi
    done
    echo $value
}

_ecl_opts_common()
{
    echo "--help -v --verbose -s= --server= --port= -u= --username= -pw= --password="
}

_ecl_opts_queries()
{
    local subcmdpos=$(_ecl_nextcommand $1)
    local subcmdname="${COMP_WORDS[subcmdpos]}"
    case "${subcmdname}" in
        "" | ecl)
            echo "--help list copy config"
            ;;
        copy)
            echo -n "--no-reload --allow-foreign --wait= --timeLimit= --warnTimeLimit= --memoryLimit= --priority= --daliip= --update-super-files --update-clone-from --dont-append-cluster "
            _ecl_opts_common
            ;;
        config)
            echo -n "-t= --target= --no-files --daliip= -A --activate --no-reload "
            echo -n "-O --overwrite --wait= --timeLimit= --warnTimeLimit= --memoryLimit= --priority= "
            _ecl_opts_common
            ;;
        list)
            echo -n "-t= --target= --show= "
            _ecl_opts_common
            ;;
         *)
         ;;
    esac
}

_ecl_opts_roxie()
{
    local subcmdpos=$(_ecl_nextcommand $1)
    local subcmdname="${COMP_WORDS[subcmdpos]}"
    case "${subcmdname}" in
        "" | ecl)
            echo "--help attach detach check reload unused-files"
            ;;
        attach | detach | check | reload)
            echo -n "--wait= "
            _ecl_opts_common
            ;;
        "unused-files")
            echo -n "--delete-recursive --delete-subfiles --delete --check-packagemaps --wait="
            _ecl_opts_common
            ;;
         *)
            ;;
    esac
}

_ecl_opts_packagemap()
{
    local subcmdpos=$(_ecl_nextcommand $1)
    local subcmdname="${COMP_WORDS[subcmdpos]}"
    case "${subcmdname}" in
        "" | ecl)
             echo "--help add copyFiles delete activate deactivate list info"
            ;;
        add)
            if [ "${COMP_WORDS[COMP_CWORD]:0:1}" != "-" ]
            then
                _ecl_opts_file
            else
                echo -n "-O --overwrite --allow-foreign --update-super-files --update-clone-from --dont-append-cluster --replace "
                _ecl_opts_common
            fi
            ;;
        delete | activate | deactivate | list | info)
            _ecl_opts_common
            ;;
        *)
              ;;
    esac
}

_ecl_opts_deploy()
{
    echo -n "-t= --target= -n= --name= --main= --ecl-only --legacy --limit= --snapshot= -sn="
    echo -n "--wait= --manifest= "
}

_ecl_opts_core_file()
{
    if [ "${COMP_WORDS[COMP_CWORD]:0:1}" != "-" ]
    then
        _ecl_opts_file
    else
       case "$1" in
            deploy)
                _ecl_opts_deploy
                _ecl_opts_common
                ;;
            publish)
                echo -n "-A --activate --no-reload --allow-foreign --timeLimit= --warnTimeLimit= --memoryLimit= --priority= --daliip= --update-super-files --update-clone-from --dont-append-cluster "
                _ecl_opts_deploy
                _ecl_opts_common
                ;;
            run)
                echo -n "-in= --input= "
                _ecl_opts_deploy
                _ecl_opts_common
                ;;
             *)
             ;;
        esac
    fi
}

_ecl_opts_core()
{
   case "$1" in
        deploy | publish | run)
            _ecl_opts_core_file $1
            ;;
        unpublish | activate | deactivate)
            _ecl_opts_common
            ;;
         *)
            ;;
    esac
}

_ecl()
{
    COMPREPLY=()
    local cmdpos=$(_ecl_nextcommand 1)
    local cmdname="${COMP_WORDS[cmdpos]}"
    case "${cmdname}" in
        "" | ecl)
            opts="--help --version help deploy publish unpublish run activate deactivate queries packagemap roxie"
            ;;
        roxie)
            opts=$(_ecl_opts_roxie $(($cmdpos + 1)))
            ;;
        queries)
            opts=$(_ecl_opts_queries $(($cmdpos + 1)))
            ;;
        packagemap)
            opts=$(_ecl_opts_packagemap $(($cmdpos + 1)))
            ;;
        *)
            opts=$(_ecl_opts_core $cmdname)
            ;;
    esac

    local cur="${COMP_WORDS[COMP_CWORD]}"
    if [ "$opts" = "<filelist>" ]
    then
        COMPREPLY=( $(compgen -f ${cur}) )
    else
        COMPREPLY=( $(compgen -W "${opts}" -- ${cur}) )
    fi
    return 0
}

complete -o nospace -F _ecl ecl
