
_ucc()
{
  local cur prev commands global_opts opts
  COMPREPLY=()
  cur=`_get_cword`
  prev="${COMP_WORDS[COMP_CWORD-1]}"
  commands="abort-job batch bes-job-status bes-list-att bes-list-jobs bes-submit-job bes-terminate-job broker-run cip-query connect connect-to-testgrid copy-file copy-file-status create-storage find get-file get-output get-status issue-delegation list-applications list-jobs list-sites list-storages ls metadata mkdir put-file resolve rm run run-groovy shell system-info workflow-control workflow-info workflow-submit workflow-trace wsrf"
  global_opts="--registry --with-timing --delegationAssertion --truststore --user --truststorePassword --configuration --help --keystore --long --alias --output --password --verbose --storetype"


  # parsing for ucc command word (2nd word in commandline.
  # ucc <command> [OPTIONS] <args>)
  if [ $COMP_CWORD -eq 1 ]; then
    COMPREPLY=( $(compgen -W "${commands}" -- ${cur}) )
    return 0
  fi

  # looking for arguments matching to command
  case "${COMP_WORDS[1]}" in
    abort-job)
    opts="$global_opts "
    ;;
    batch)
    opts="$global_opts --threads --input --noFetchOutcome --noResourceCheck --update --jsdl --maxNewJobs --siteWeights --follow --submitOnly --max --keep --sitename"
    ;;
    bes-job-status)
    opts="$global_opts "
    ;;
    bes-list-att)
    opts="$global_opts --sitename"
    ;;
    bes-list-jobs)
    opts="$global_opts --sitename"
    ;;
    bes-submit-job)
    opts="$global_opts --sitename --jsdl --stdout --brief --stderr"
    ;;
    bes-terminate-job)
    opts="$global_opts "
    ;;
    broker-run)
    opts="$global_opts --sitename --noFilenameFix --asynchronous --lifetime --factoryURL --storageURL"
    ;;
    cip-query)
    opts="$global_opts --sitename --maxrecords --glue --topic"
    ;;
    connect)
    opts="$global_opts --lifetime"
    ;;
    connect-to-testgrid)
    opts="$global_opts "
    ;;
    copy-file)
    opts="$global_opts --protocols --source --force-remote --target --asynchronous"
    ;;
    copy-file-status)
    opts="$global_opts "
    ;;
    create-storage)
    opts="$global_opts --sitename --lifetime --factoryURL"
    ;;
    find)
    opts="$global_opts --name --recursive"
    ;;
    get-file)
    opts="$global_opts --source --target --protocols"
    ;;
    get-output)
    opts="$global_opts --sitename --stdout --jsdl --asynchronous --stderr --example --brief"
    ;;
    get-status)
    opts="$global_opts "
    ;;
    issue-delegation)
    opts="$global_opts --sitename --file --target --validity --subject"
    ;;
    list-applications)
    opts="$global_opts --sitename --filter --all"
    ;;
    list-jobs)
    opts="$global_opts --sitename --filter --all"
    ;;
    list-sites)
    opts="$global_opts --filter --all"
    ;;
    list-storages)
    opts="$global_opts --filter --all"
    ;;
    ls)
    opts="$global_opts --human --show-metadata --recursive"
    ;;
    metadata)
    opts="$global_opts --query --file --storage --metadata-service --advanced-query --command"
    ;;
    mkdir)
    opts="$global_opts "
    ;;
    put-file)
    opts="$global_opts --protocols --source --target --append"
    ;;
    resolve)
    opts="$global_opts "
    ;;
    rm)
    opts="$global_opts --quiet"
    ;;
    run)
    opts="$global_opts --sitename --stdout --jsdl --asynchronous --stderr --example --brief"
    ;;
    run-groovy)
    opts="$global_opts --expression --file"
    ;;
    shell)
    opts="$global_opts --file"
    ;;
    system-info)
    opts="$global_opts "
    ;;
    workflow-control)
    opts="$global_opts "
    ;;
    workflow-info)
    opts="$global_opts --filter --nofiles --all"
    ;;
    workflow-submit)
    opts="$global_opts --sitename --noFilenameFix --wait --workflowName --lifetime --factoryURL --storageURL --uccInput"
    ;;
    workflow-trace)
    opts="$global_opts "
    ;;

    wsrf)
    #looking for wsrf command
    if [ $COMP_CWORD -eq 2 ]; then
      opts="getproperties destroy extend"
    else
      opts="$global_opts "
    fi
    ;;
  esac
  
  COMPREPLY=( $(compgen -W "${opts}" -- ${cur}) )
  
  _filedir

}

complete -o filenames -F _ucc ucc
