# 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.


_getSubmitOptions() {
    echo "--user=
          --password=
          --batchManager=
          --controlPropertiesFile=
          --httpTimeout_s=
          --jobXMLFile=
          --jobParametersFile=
          --applicationName=
          --jobParameter=
         #--jobPropertiesFile=
          --componentName=
          --moduleName=
          --jobXMLName=
          --pollingInterval_s="
}

_getStopOptions() {
    echo " --user=
           --password
           --batchManager=
           --controlPropertiesFile=
           --trustSslCertificates
           --httpTimeout_s=
           --jobInstanceId=
           --jobExecutionId=
           --pollingInterval_s=
           --returnExitStatus
           --wait
           --getJobLog
           --verbose"
}

_getRestartOptions() {
    echo "--user=
          --password
          --batchManager=
          --controlPropertiesFile=
          --trustSslCertificates
          --httpTimeout_s=
          --jobExecutionId=
          --jobParameter=
          --jobParametersFile=
          --reusePreviousParams
          --jobPropertiesFile=
          --stopOnShutdown
          --jobInstanceId=
          --pollingInterval_s=
          --returnExitStatus
          --wait
          --getJobLog
          --verbose"
}

_getStatusOptions() {
    echo "--user=
          --password
          --batchManager=
          --controlPropertiesFile=
          --trustSslCertificates
          --httpTimeout_s=
          --jobInstanceId=
          --jobExecutionId="
}

_getGetJobLogOptions() {
    echo "--user=
          --password
          --batchManager=
          --controlPropertiesFile=
          --trustSslCertificates
          --httpTimeout_s=
          --type=text|zip
          --jobExecutionId=
          --jobInstanceId=
          --outputFile="
}

_getListJobsOptions() {
    echo "--user=
          --password
          --batchManager=
          --controlPropertiesFile=
          --trustSslCertificates
          --httpTimeout_s=
          --pageSize=
          --jobInstanceId=
          --page=
          --createTime=
          --exitStatus=
          --instanceState="
}

_getPurgeOptions() {
    echo "--user=
          --password
          --batchManager=
          --controlPropertiesFile=
          --trustSslCertificates
          --httpTimeout_s=
          --jobInstanceId=
          --createTime=
          --pageSize=
          --exitStatus=
          --instanceState=
          --page=
          --purgeJobStoreOnly"
}

_getHelpOptions() {
    echo "submit stop restart status getJobLog listJobs purge"
}

_batchManager() {

    local cur prev opts command

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

    if (( COMP_CWORD > 1 )); then
        command=${COMP_WORDS[1]}
        case "$command" in
            help) options=`_getHelpOptions`
                  COMPREPLY=( $(compgen -W "${options}" -- ${cur}) )
                  return 0
                  ;;
            submit) options=`_getSubmitOptions`
                    COMPREPLY=( $(compgen -W "${options}" -- ${cur}) )
                    return 0
                    ;;
            stop) options=`_getStopOptions`
                  COMPREPLY=( $(compgen -W "${options}" -- ${cur}) )
                  return 0
                  ;;
            restart) options=`_getRestartOptions`
                     COMPREPLY=( $(compgen -W "${options}" -- ${cur}) )
                     return 0
                     ;;
            status) options=`_getStatusOptions`
                    COMPREPLY=( $(compgen -W "${options}" -- ${cur}) )
                    return 0
                    ;;
            getJobLog) options=`_getGetJobLogOptions`
                       COMPREPLY=( $(compgen -W "${options}" -- ${cur}) )
                       return 0
                       ;;
            listJobs) options=`_getListJobsOptions`
                      COMPREPLY=( $(compgen -W "${options}" -- ${cur}) )
                      return 0
                      ;;
            purge) options=`_getPurgeOptions`
                   COMPREPLY=( $(compgen -W "${options}" -- ${cur}) )
                   return 0
                   ;;
        esac
    fi

    if [[ ${cur} == * ]] ; then
        opts="help submit stop restart status getJobLog listJobs purge"
        COMPREPLY=( $(compgen -W "${opts}" -- ${cur}) )
        return 0
    fi

    return 0
}

complete -o nospace -F _batchManager batchManager