#!/usr/bin/env bash
#
# mbi Bash Completion
# =======================
#
# Bash completion support for the `mbi` command,
# generated by [picocli](https://picocli.info/) version 4.7.7.
#
# Installation
# ------------
#
# 1. Source all completion scripts in your .bash_profile
#
#   cd $YOUR_APP_HOME/bin
#   for f in $(find . -name "*_completion"); do line=". $(pwd)/$f"; grep "$line" ~/.bash_profile || echo "$line" >> ~/.bash_profile; done
#
# 2. Open a new bash console, and type `mbi [TAB][TAB]`
#
# 1a. Alternatively, if you have [bash-completion](https://github.com/scop/bash-completion) installed:
#     Place this file in a `bash-completion.d` folder:
#
#   * /etc/bash-completion.d
#   * /usr/local/etc/bash-completion.d
#   * ~/bash-completion.d
#
# Documentation
# -------------
# The script is called by bash whenever [TAB] or [TAB][TAB] is pressed after
# 'mbi (..)'. By reading entered command line parameters,
# it determines possible bash completions and writes them to the COMPREPLY variable.
# Bash then completes the user input if only one entry is listed in the variable or
# shows the options if more than one is listed in COMPREPLY.
#
# References
# ----------
# [1] http://stackoverflow.com/a/12495480/1440785
# [2] http://tiswww.case.edu/php/chet/bash/FAQ
# [3] https://www.gnu.org/software/bash/manual/html_node/The-Shopt-Builtin.html
# [4] http://zsh.sourceforge.net/Doc/Release/Options.html#index-COMPLETE_005fALIASES
# [5] https://stackoverflow.com/questions/17042057/bash-check-element-in-array-for-elements-in-another-array/17042655#17042655
# [6] https://www.gnu.org/software/bash/manual/html_node/Programmable-Completion.html#Programmable-Completion
# [7] https://stackoverflow.com/questions/3249432/can-a-bash-tab-completion-script-be-used-in-zsh/27853970#27853970
#

if [ -n "$BASH_VERSION" ]; then
  # Enable programmable completion facilities when using bash (see [3])
  shopt -s progcomp
elif [ -n "$ZSH_VERSION" ]; then
  # Make alias a distinct command for completion purposes when using zsh (see [4])
  setopt COMPLETE_ALIASES
  alias compopt=complete

  # Enable bash completion in zsh (see [7])
  # Only initialize completions module once to avoid unregistering existing completions.
  if ! type compdef > /dev/null; then
    autoload -U +X compinit && compinit
  fi
  autoload -U +X bashcompinit && bashcompinit
fi

# CompWordsContainsArray takes an array and then checks
# if all elements of this array are in the global COMP_WORDS array.
#
# Returns zero (no error) if all elements of the array are in the COMP_WORDS array,
# otherwise returns 1 (error).
function CompWordsContainsArray() {
  declare -a localArray
  localArray=("$@")
  local findme
  for findme in "${localArray[@]}"; do
    if ElementNotInCompWords "$findme"; then return 1; fi
  done
  return 0
}
function ElementNotInCompWords() {
  local findme="$1"
  local element
  for element in "${COMP_WORDS[@]}"; do
    if [[ "$findme" = "$element" ]]; then return 1; fi
  done
  return 0
}

# The `currentPositionalIndex` function calculates the index of the current positional parameter.
#
# currentPositionalIndex takes three parameters:
# the command name,
# a space-separated string with the names of options that take a parameter, and
# a space-separated string with the names of boolean options (that don't take any params).
# When done, this function echos the current positional index to std_out.
#
# Example usage:
# local currIndex=$(currentPositionalIndex "mysubcommand" "$ARG_OPTS" "$FLAG_OPTS")
function currentPositionalIndex() {
  local commandName="$1"
  local optionsWithArgs="$2"
  local booleanOptions="$3"
  local previousWord
  local result=0

  for i in $(seq $((COMP_CWORD - 1)) -1 0); do
    previousWord=${COMP_WORDS[i]}
    if [ "${previousWord}" = "$commandName" ]; then
      break
    fi
    if [[ "${optionsWithArgs}" =~ ${previousWord} ]]; then
      ((result-=2)) # Arg option and its value not counted as positional param
    elif [[ "${booleanOptions}" =~ ${previousWord} ]]; then
      ((result-=1)) # Flag option itself not counted as positional param
    fi
    ((result++))
  done
  echo "$result"
}

# compReplyArray generates a list of completion suggestions based on an array, ensuring all values are properly escaped.
#
# compReplyArray takes a single parameter: the array of options to be displayed
#
# The output is echoed to std_out, one option per line.
#
# Example usage:
# local options=("foo", "bar", "baz")
# local IFS=$'\n'
# COMPREPLY=($(compReplyArray "${options[@]}"))
function compReplyArray() {
  declare -a options
  options=("$@")
  local curr_word=${COMP_WORDS[COMP_CWORD]}
  local i
  local quoted
  local optionList=()

  for (( i=0; i<${#options[@]}; i++ )); do
    # Double escape, since we want escaped values, but compgen -W expands the argument
    printf -v quoted %q "${options[i]}"
    quoted=\'${quoted//\'/\'\\\'\'}\'

    optionList[i]=$quoted
  done

  # We also have to add another round of escaping to $curr_word.
  curr_word=${curr_word//\\/\\\\}
  curr_word=${curr_word//\'/\\\'}

  # Actually generate completions.
  local IFS=$'\n'
  echo -e "$(compgen -W "${optionList[*]}" -- "$curr_word")"
}

# Bash completion entry point function.
# _complete_mbi finds which commands and subcommands have been specified
# on the command line and delegates to the appropriate function
# to generate possible options and subcommands for the last specified subcommand.
function _complete_mbi() {
  # Edge case: if command line has no space after subcommand, then don't assume this subcommand is selected (remkop/picocli#1468).
  if [ "${COMP_LINE}" = "${COMP_WORDS[0]} init" ];    then _picocli_mbi; return $?; fi
  if [ "${COMP_LINE}" = "${COMP_WORDS[0]} config" ];    then _picocli_mbi; return $?; fi
  if [ "${COMP_LINE}" = "${COMP_WORDS[0]} run" ];    then _picocli_mbi; return $?; fi
  if [ "${COMP_LINE}" = "${COMP_WORDS[0]} log" ];    then _picocli_mbi; return $?; fi
  if [ "${COMP_LINE}" = "${COMP_WORDS[0]} validate" ];    then _picocli_mbi; return $?; fi
  if [ "${COMP_LINE}" = "${COMP_WORDS[0]} test" ];    then _picocli_mbi; return $?; fi
  if [ "${COMP_LINE}" = "${COMP_WORDS[0]} status" ];    then _picocli_mbi; return $?; fi
  if [ "${COMP_LINE}" = "${COMP_WORDS[0]} local-subject" ];    then _picocli_mbi; return $?; fi
  if [ "${COMP_LINE}" = "${COMP_WORDS[0]} generate" ];    then _picocli_mbi; return $?; fi
  if [ "${COMP_LINE}" = "${COMP_WORDS[0]} execute" ];    then _picocli_mbi; return $?; fi
  if [ "${COMP_LINE}" = "${COMP_WORDS[0]} kube-exec" ];    then _picocli_mbi; return $?; fi
  if [ "${COMP_LINE}" = "${COMP_WORDS[0]} report" ];    then _picocli_mbi; return $?; fi
  if [ "${COMP_LINE}" = "${COMP_WORDS[0]} shell" ];    then _picocli_mbi; return $?; fi

  # Find the longest sequence of subcommands and call the bash function for that subcommand.
  local cmds0=(init)
  local cmds1=(config)
  local cmds2=(run)
  local cmds3=(log)
  local cmds4=(validate)
  local cmds5=(test)
  local cmds6=(status)
  local cmds7=(local-subject)
  local cmds8=(generate)
  local cmds9=(execute)
  local cmds10=(kube-exec)
  local cmds11=(report)
  local cmds12=(shell)

  if CompWordsContainsArray "${cmds12[@]}"; then _picocli_mbi_shell; return $?; fi
  if CompWordsContainsArray "${cmds11[@]}"; then _picocli_mbi_report; return $?; fi
  if CompWordsContainsArray "${cmds10[@]}"; then _picocli_mbi_kubeexec; return $?; fi
  if CompWordsContainsArray "${cmds9[@]}"; then _picocli_mbi_execute; return $?; fi
  if CompWordsContainsArray "${cmds8[@]}"; then _picocli_mbi_generate; return $?; fi
  if CompWordsContainsArray "${cmds7[@]}"; then _picocli_mbi_localsubject; return $?; fi
  if CompWordsContainsArray "${cmds6[@]}"; then _picocli_mbi_status; return $?; fi
  if CompWordsContainsArray "${cmds5[@]}"; then _picocli_mbi_test; return $?; fi
  if CompWordsContainsArray "${cmds4[@]}"; then _picocli_mbi_validate; return $?; fi
  if CompWordsContainsArray "${cmds3[@]}"; then _picocli_mbi_log; return $?; fi
  if CompWordsContainsArray "${cmds2[@]}"; then _picocli_mbi_run; return $?; fi
  if CompWordsContainsArray "${cmds1[@]}"; then _picocli_mbi_config; return $?; fi
  if CompWordsContainsArray "${cmds0[@]}"; then _picocli_mbi_init; return $?; fi

  # No subcommands were specified; generate completions for the top-level command.
  _picocli_mbi; return $?;
}

# Generates completions for the options and subcommands of the `mbi` command.
function _picocli_mbi() {
  # Get completion data
  local curr_word=${COMP_WORDS[COMP_CWORD]}

  local commands="init config run log validate test status local-subject generate execute kube-exec report shell"
  local flag_opts="'-h' '--help' '-V' '--version'"
  local arg_opts=""

  if [[ "${curr_word}" == -* ]]; then
    COMPREPLY=( $(compgen -W "${flag_opts} ${arg_opts}" -- "${curr_word}") )
  else
    local positionals=""
    local IFS=$'\n'
    COMPREPLY=( $(compgen -W "${commands// /$'\n'}${IFS}${positionals}" -- "${curr_word}") )
  fi
}

# Generates completions for the options and subcommands of the `init` subcommand.
function _picocli_mbi_init() {
  # Get completion data
  local curr_word=${COMP_WORDS[COMP_CWORD]}
  local prev_word=${COMP_WORDS[COMP_CWORD-1]}

  local commands=""
  local flag_opts="'--fedora' '--centos' '--rhel' '-h' '--help' '-V' '--version'"
  local arg_opts="'--subject-path' '--workflow-path' '--plan-path' '--platform-path' '--test-platform-path' '--result-dir' '--cache-dir' '--work-dir' '--link-dir' '--report-dir' '--test-plan-dir' '--test-result-dir' '--lookaside' '--scm-dir' '--scm-ref' '--max-checkout-tasks' '--max-srpm-tasks' '--max-rpm-tasks'"

  type compopt &>/dev/null && compopt +o default

  case ${prev_word} in
    '--subject-path')
      local IFS=$'\n'
      type compopt &>/dev/null && compopt -o filenames
      COMPREPLY=( $( compgen -f -- "${curr_word}" ) ) # files
      return $?
      ;;
    '--workflow-path')
      local IFS=$'\n'
      type compopt &>/dev/null && compopt -o filenames
      COMPREPLY=( $( compgen -f -- "${curr_word}" ) ) # files
      return $?
      ;;
    '--plan-path')
      local IFS=$'\n'
      type compopt &>/dev/null && compopt -o filenames
      COMPREPLY=( $( compgen -f -- "${curr_word}" ) ) # files
      return $?
      ;;
    '--platform-path')
      local IFS=$'\n'
      type compopt &>/dev/null && compopt -o filenames
      COMPREPLY=( $( compgen -f -- "${curr_word}" ) ) # files
      return $?
      ;;
    '--test-platform-path')
      local IFS=$'\n'
      type compopt &>/dev/null && compopt -o filenames
      COMPREPLY=( $( compgen -f -- "${curr_word}" ) ) # files
      return $?
      ;;
    '--result-dir')
      local IFS=$'\n'
      type compopt &>/dev/null && compopt -o filenames
      COMPREPLY=( $( compgen -f -- "${curr_word}" ) ) # files
      return $?
      ;;
    '--cache-dir')
      local IFS=$'\n'
      type compopt &>/dev/null && compopt -o filenames
      COMPREPLY=( $( compgen -f -- "${curr_word}" ) ) # files
      return $?
      ;;
    '--work-dir')
      local IFS=$'\n'
      type compopt &>/dev/null && compopt -o filenames
      COMPREPLY=( $( compgen -f -- "${curr_word}" ) ) # files
      return $?
      ;;
    '--link-dir')
      local IFS=$'\n'
      type compopt &>/dev/null && compopt -o filenames
      COMPREPLY=( $( compgen -f -- "${curr_word}" ) ) # files
      return $?
      ;;
    '--report-dir')
      local IFS=$'\n'
      type compopt &>/dev/null && compopt -o filenames
      COMPREPLY=( $( compgen -f -- "${curr_word}" ) ) # files
      return $?
      ;;
    '--test-plan-dir')
      local IFS=$'\n'
      type compopt &>/dev/null && compopt -o filenames
      COMPREPLY=( $( compgen -f -- "${curr_word}" ) ) # files
      return $?
      ;;
    '--test-result-dir')
      local IFS=$'\n'
      type compopt &>/dev/null && compopt -o filenames
      COMPREPLY=( $( compgen -f -- "${curr_word}" ) ) # files
      return $?
      ;;
    '--lookaside')
      return
      ;;
    '--scm-dir')
      local IFS=$'\n'
      type compopt &>/dev/null && compopt -o filenames
      COMPREPLY=( $( compgen -f -- "${curr_word}" ) ) # files
      return $?
      ;;
    '--scm-ref')
      return
      ;;
    '--max-checkout-tasks')
      return
      ;;
    '--max-srpm-tasks')
      return
      ;;
    '--max-rpm-tasks')
      return
      ;;
  esac

  if [[ "${curr_word}" == -* ]]; then
    COMPREPLY=( $(compgen -W "${flag_opts} ${arg_opts}" -- "${curr_word}") )
  else
    local positionals=""
    local IFS=$'\n'
    COMPREPLY=( $(compgen -W "${commands// /$'\n'}${IFS}${positionals}" -- "${curr_word}") )
  fi
}

# Generates completions for the options and subcommands of the `config` subcommand.
function _picocli_mbi_config() {
  # Get completion data
  local curr_word=${COMP_WORDS[COMP_CWORD]}
  local prev_word=${COMP_WORDS[COMP_CWORD-1]}

  local commands=""
  local flag_opts="'-s' '--show' '--env' '-h' '--help' '-V' '--version'"
  local arg_opts="'--subject-path' '--workflow-path' '--plan-path' '--platform-path' '--test-platform-path' '--result-dir' '--cache-dir' '--work-dir' '--link-dir' '--report-dir' '--test-plan-dir' '--test-result-dir' '--lookaside' '--scm-dir' '--scm-ref' '--max-checkout-tasks' '--max-srpm-tasks' '--max-rpm-tasks'"

  type compopt &>/dev/null && compopt +o default

  case ${prev_word} in
    '--subject-path')
      local IFS=$'\n'
      type compopt &>/dev/null && compopt -o filenames
      COMPREPLY=( $( compgen -f -- "${curr_word}" ) ) # files
      return $?
      ;;
    '--workflow-path')
      local IFS=$'\n'
      type compopt &>/dev/null && compopt -o filenames
      COMPREPLY=( $( compgen -f -- "${curr_word}" ) ) # files
      return $?
      ;;
    '--plan-path')
      local IFS=$'\n'
      type compopt &>/dev/null && compopt -o filenames
      COMPREPLY=( $( compgen -f -- "${curr_word}" ) ) # files
      return $?
      ;;
    '--platform-path')
      local IFS=$'\n'
      type compopt &>/dev/null && compopt -o filenames
      COMPREPLY=( $( compgen -f -- "${curr_word}" ) ) # files
      return $?
      ;;
    '--test-platform-path')
      local IFS=$'\n'
      type compopt &>/dev/null && compopt -o filenames
      COMPREPLY=( $( compgen -f -- "${curr_word}" ) ) # files
      return $?
      ;;
    '--result-dir')
      local IFS=$'\n'
      type compopt &>/dev/null && compopt -o filenames
      COMPREPLY=( $( compgen -f -- "${curr_word}" ) ) # files
      return $?
      ;;
    '--cache-dir')
      local IFS=$'\n'
      type compopt &>/dev/null && compopt -o filenames
      COMPREPLY=( $( compgen -f -- "${curr_word}" ) ) # files
      return $?
      ;;
    '--work-dir')
      local IFS=$'\n'
      type compopt &>/dev/null && compopt -o filenames
      COMPREPLY=( $( compgen -f -- "${curr_word}" ) ) # files
      return $?
      ;;
    '--link-dir')
      local IFS=$'\n'
      type compopt &>/dev/null && compopt -o filenames
      COMPREPLY=( $( compgen -f -- "${curr_word}" ) ) # files
      return $?
      ;;
    '--report-dir')
      local IFS=$'\n'
      type compopt &>/dev/null && compopt -o filenames
      COMPREPLY=( $( compgen -f -- "${curr_word}" ) ) # files
      return $?
      ;;
    '--test-plan-dir')
      local IFS=$'\n'
      type compopt &>/dev/null && compopt -o filenames
      COMPREPLY=( $( compgen -f -- "${curr_word}" ) ) # files
      return $?
      ;;
    '--test-result-dir')
      local IFS=$'\n'
      type compopt &>/dev/null && compopt -o filenames
      COMPREPLY=( $( compgen -f -- "${curr_word}" ) ) # files
      return $?
      ;;
    '--lookaside')
      return
      ;;
    '--scm-dir')
      local IFS=$'\n'
      type compopt &>/dev/null && compopt -o filenames
      COMPREPLY=( $( compgen -f -- "${curr_word}" ) ) # files
      return $?
      ;;
    '--scm-ref')
      return
      ;;
    '--max-checkout-tasks')
      return
      ;;
    '--max-srpm-tasks')
      return
      ;;
    '--max-rpm-tasks')
      return
      ;;
  esac

  if [[ "${curr_word}" == -* ]]; then
    COMPREPLY=( $(compgen -W "${flag_opts} ${arg_opts}" -- "${curr_word}") )
  else
    local positionals=""
    local IFS=$'\n'
    COMPREPLY=( $(compgen -W "${commands// /$'\n'}${IFS}${positionals}" -- "${curr_word}") )
  fi
}

# Generates completions for the options and subcommands of the `run` subcommand.
function _picocli_mbi_run() {
  # Get completion data
  local curr_word=${COMP_WORDS[COMP_CWORD]}

  local commands=""
  local flag_opts="'-B' '--batch-mode' '-k' '--kubernetes' '-h' '--help' '-V' '--version'"
  local arg_opts=""

  if [[ "${curr_word}" == -* ]]; then
    COMPREPLY=( $(compgen -W "${flag_opts} ${arg_opts}" -- "${curr_word}") )
  else
    local positionals=""
    local IFS=$'\n'
    COMPREPLY=( $(compgen -W "${commands// /$'\n'}${IFS}${positionals}" -- "${curr_word}") )
  fi
}

# Generates completions for the options and subcommands of the `log` subcommand.
function _picocli_mbi_log() {
  # Get completion data
  local curr_word=${COMP_WORDS[COMP_CWORD]}
  local prev_word=${COMP_WORDS[COMP_CWORD-1]}

  local commands=""
  local flag_opts="'-P' '--path' '--no-pager' '-r' '--root' '-s' '--srpm' '-h' '--help' '-V' '--version'"
  local arg_opts="'-p' '--phase' '-a' '--artifact'"

  type compopt &>/dev/null && compopt +o default

  case ${prev_word} in
    '-p'|'--phase')
      return
      ;;
    '-a'|'--artifact')
      return
      ;;
  esac

  if [[ "${curr_word}" == -* ]]; then
    COMPREPLY=( $(compgen -W "${flag_opts} ${arg_opts}" -- "${curr_word}") )
  else
    local positionals=""
    local IFS=$'\n'
    COMPREPLY=( $(compgen -W "${commands// /$'\n'}${IFS}${positionals}" -- "${curr_word}") )
  fi
}

# Generates completions for the options and subcommands of the `validate` subcommand.
function _picocli_mbi_validate() {
  # Get completion data
  local curr_word=${COMP_WORDS[COMP_CWORD]}
  local prev_word=${COMP_WORDS[COMP_CWORD-1]}

  local commands=""
  local flag_opts="'-h' '--help' '-V' '--version'"
  local arg_opts="'-e' '--environment' '-c' '--context'"

  type compopt &>/dev/null && compopt +o default

  case ${prev_word} in
    '-e'|'--environment')
      return
      ;;
    '-c'|'--context')
      return
      ;;
  esac

  if [[ "${curr_word}" == -* ]]; then
    COMPREPLY=( $(compgen -W "${flag_opts} ${arg_opts}" -- "${curr_word}") )
  else
    local positionals=""
    local IFS=$'\n'
    COMPREPLY=( $(compgen -W "${commands// /$'\n'}${IFS}${positionals}" -- "${curr_word}") )
  fi
}

# Generates completions for the options and subcommands of the `test` subcommand.
function _picocli_mbi_test() {
  # Get completion data
  local curr_word=${COMP_WORDS[COMP_CWORD]}
  local prev_word=${COMP_WORDS[COMP_CWORD-1]}

  local commands=""
  local flag_opts="'-r' '--reserve' '--no-provision' '-h' '--help' '-V' '--version'"
  local arg_opts="'-e' '--environment' '-c' '--context'"

  type compopt &>/dev/null && compopt +o default

  case ${prev_word} in
    '-e'|'--environment')
      return
      ;;
    '-c'|'--context')
      return
      ;;
  esac

  if [[ "${curr_word}" == -* ]]; then
    COMPREPLY=( $(compgen -W "${flag_opts} ${arg_opts}" -- "${curr_word}") )
  else
    local positionals=""
    local IFS=$'\n'
    COMPREPLY=( $(compgen -W "${commands// /$'\n'}${IFS}${positionals}" -- "${curr_word}") )
  fi
}

# Generates completions for the options and subcommands of the `status` subcommand.
function _picocli_mbi_status() {
  # Get completion data
  local curr_word=${COMP_WORDS[COMP_CWORD]}

  local commands=""
  local flag_opts="'-a' '--all' '-h' '--help' '-V' '--version'"
  local arg_opts=""

  if [[ "${curr_word}" == -* ]]; then
    COMPREPLY=( $(compgen -W "${flag_opts} ${arg_opts}" -- "${curr_word}") )
  else
    local positionals=""
    local IFS=$'\n'
    COMPREPLY=( $(compgen -W "${commands// /$'\n'}${IFS}${positionals}" -- "${curr_word}") )
  fi
}

# Generates completions for the options and subcommands of the `local-subject` subcommand.
function _picocli_mbi_localsubject() {
  # Get completion data
  local curr_word=${COMP_WORDS[COMP_CWORD]}
  local prev_word=${COMP_WORDS[COMP_CWORD-1]}

  local commands=""
  local flag_opts="'-h' '--help' '-V' '--version'"
  local arg_opts="'-s' '--subject' '-m' '--plan' '-L' '--lookaside' '-S' '--scm' '-r' '--ref'"

  type compopt &>/dev/null && compopt +o default

  case ${prev_word} in
    '-s'|'--subject')
      local IFS=$'\n'
      type compopt &>/dev/null && compopt -o filenames
      COMPREPLY=( $( compgen -f -- "${curr_word}" ) ) # files
      return $?
      ;;
    '-m'|'--plan')
      local IFS=$'\n'
      type compopt &>/dev/null && compopt -o filenames
      COMPREPLY=( $( compgen -f -- "${curr_word}" ) ) # files
      return $?
      ;;
    '-L'|'--lookaside')
      return
      ;;
    '-S'|'--scm')
      local IFS=$'\n'
      type compopt &>/dev/null && compopt -o filenames
      COMPREPLY=( $( compgen -f -- "${curr_word}" ) ) # files
      return $?
      ;;
    '-r'|'--ref')
      return
      ;;
  esac

  if [[ "${curr_word}" == -* ]]; then
    COMPREPLY=( $(compgen -W "${flag_opts} ${arg_opts}" -- "${curr_word}") )
  else
    local positionals=""
    local IFS=$'\n'
    COMPREPLY=( $(compgen -W "${commands// /$'\n'}${IFS}${positionals}" -- "${curr_word}") )
  fi
}

# Generates completions for the options and subcommands of the `generate` subcommand.
function _picocli_mbi_generate() {
  # Get completion data
  local curr_word=${COMP_WORDS[COMP_CWORD]}
  local prev_word=${COMP_WORDS[COMP_CWORD-1]}

  local commands=""
  local flag_opts="'-h' '--help' '-V' '--version'"
  local arg_opts="'-m' '--plan' '-p' '--platform' '-s' '--subject' '-w' '--workflow'"

  type compopt &>/dev/null && compopt +o default

  case ${prev_word} in
    '-m'|'--plan')
      local IFS=$'\n'
      type compopt &>/dev/null && compopt -o filenames
      COMPREPLY=( $( compgen -f -- "${curr_word}" ) ) # files
      return $?
      ;;
    '-p'|'--platform')
      local IFS=$'\n'
      type compopt &>/dev/null && compopt -o filenames
      COMPREPLY=( $( compgen -f -- "${curr_word}" ) ) # files
      return $?
      ;;
    '-s'|'--subject')
      local IFS=$'\n'
      type compopt &>/dev/null && compopt -o filenames
      COMPREPLY=( $( compgen -f -- "${curr_word}" ) ) # files
      return $?
      ;;
    '-w'|'--workflow')
      local IFS=$'\n'
      type compopt &>/dev/null && compopt -o filenames
      COMPREPLY=( $( compgen -f -- "${curr_word}" ) ) # files
      return $?
      ;;
  esac

  if [[ "${curr_word}" == -* ]]; then
    COMPREPLY=( $(compgen -W "${flag_opts} ${arg_opts}" -- "${curr_word}") )
  else
    local positionals=""
    local IFS=$'\n'
    COMPREPLY=( $(compgen -W "${commands// /$'\n'}${IFS}${positionals}" -- "${curr_word}") )
  fi
}

# Generates completions for the options and subcommands of the `execute` subcommand.
function _picocli_mbi_execute() {
  # Get completion data
  local curr_word=${COMP_WORDS[COMP_CWORD]}
  local prev_word=${COMP_WORDS[COMP_CWORD-1]}

  local commands=""
  local flag_opts="'-B' '--batch-mode'"
  local arg_opts="'-w' '--workflow' '-R' '--result-dir' '-C' '--cache-dir' '-W' '--work-dir' '-L' '--link-dir' '--max-checkout-tasks' '--max-srpm-tasks' '--max-rpm-tasks' '-h' '--webhook-url' '-t' '--webhook-token'"

  type compopt &>/dev/null && compopt +o default

  case ${prev_word} in
    '-w'|'--workflow')
      local IFS=$'\n'
      type compopt &>/dev/null && compopt -o filenames
      COMPREPLY=( $( compgen -f -- "${curr_word}" ) ) # files
      return $?
      ;;
    '-R'|'--result-dir')
      local IFS=$'\n'
      type compopt &>/dev/null && compopt -o filenames
      COMPREPLY=( $( compgen -f -- "${curr_word}" ) ) # files
      return $?
      ;;
    '-C'|'--cache-dir')
      local IFS=$'\n'
      type compopt &>/dev/null && compopt -o filenames
      COMPREPLY=( $( compgen -f -- "${curr_word}" ) ) # files
      return $?
      ;;
    '-W'|'--work-dir')
      local IFS=$'\n'
      type compopt &>/dev/null && compopt -o filenames
      COMPREPLY=( $( compgen -f -- "${curr_word}" ) ) # files
      return $?
      ;;
    '-L'|'--link-dir')
      local IFS=$'\n'
      type compopt &>/dev/null && compopt -o filenames
      COMPREPLY=( $( compgen -f -- "${curr_word}" ) ) # files
      return $?
      ;;
    '--max-checkout-tasks')
      return
      ;;
    '--max-srpm-tasks')
      return
      ;;
    '--max-rpm-tasks')
      return
      ;;
    '-h'|'--webhook-url')
      return
      ;;
    '-t'|'--webhook-token')
      return
      ;;
  esac

  if [[ "${curr_word}" == -* ]]; then
    COMPREPLY=( $(compgen -W "${flag_opts} ${arg_opts}" -- "${curr_word}") )
  else
    local positionals=""
    local IFS=$'\n'
    COMPREPLY=( $(compgen -W "${commands// /$'\n'}${IFS}${positionals}" -- "${curr_word}") )
  fi
}

# Generates completions for the options and subcommands of the `kube-exec` subcommand.
function _picocli_mbi_kubeexec() {
  # Get completion data
  local curr_word=${COMP_WORDS[COMP_CWORD]}
  local prev_word=${COMP_WORDS[COMP_CWORD-1]}

  local commands=""
  local flag_opts="'-B' '--batch-mode'"
  local arg_opts="'-w' '--workflow' '-R' '--result-dir' '-C' '--cache-dir' '-W' '--work-dir' '-L' '--link-dir' '--max-checkout-tasks' '--max-srpm-tasks' '--max-rpm-tasks' '-h' '--webhook-url' '-t' '--webhook-token' '--namespace' '--container-image' '--cache-volume-claim-name' '--result-volume-claim-name' '--pod-running-timeout' '--srpm-cpu-request' '--srpm-cpu-limit' '--rpm-cpu-request' '--rpm-cpu-limit' '--srpm-memory-request' '--srpm-memory-limit' '--rpm-memory-request' '--rpm-memory-limit'"

  type compopt &>/dev/null && compopt +o default

  case ${prev_word} in
    '-w'|'--workflow')
      local IFS=$'\n'
      type compopt &>/dev/null && compopt -o filenames
      COMPREPLY=( $( compgen -f -- "${curr_word}" ) ) # files
      return $?
      ;;
    '-R'|'--result-dir')
      local IFS=$'\n'
      type compopt &>/dev/null && compopt -o filenames
      COMPREPLY=( $( compgen -f -- "${curr_word}" ) ) # files
      return $?
      ;;
    '-C'|'--cache-dir')
      local IFS=$'\n'
      type compopt &>/dev/null && compopt -o filenames
      COMPREPLY=( $( compgen -f -- "${curr_word}" ) ) # files
      return $?
      ;;
    '-W'|'--work-dir')
      local IFS=$'\n'
      type compopt &>/dev/null && compopt -o filenames
      COMPREPLY=( $( compgen -f -- "${curr_word}" ) ) # files
      return $?
      ;;
    '-L'|'--link-dir')
      local IFS=$'\n'
      type compopt &>/dev/null && compopt -o filenames
      COMPREPLY=( $( compgen -f -- "${curr_word}" ) ) # files
      return $?
      ;;
    '--max-checkout-tasks')
      return
      ;;
    '--max-srpm-tasks')
      return
      ;;
    '--max-rpm-tasks')
      return
      ;;
    '-h'|'--webhook-url')
      return
      ;;
    '-t'|'--webhook-token')
      return
      ;;
    '--namespace')
      return
      ;;
    '--container-image')
      return
      ;;
    '--cache-volume-claim-name')
      return
      ;;
    '--result-volume-claim-name')
      return
      ;;
    '--pod-running-timeout')
      return
      ;;
    '--srpm-cpu-request')
      return
      ;;
    '--srpm-cpu-limit')
      return
      ;;
    '--rpm-cpu-request')
      return
      ;;
    '--rpm-cpu-limit')
      return
      ;;
    '--srpm-memory-request')
      return
      ;;
    '--srpm-memory-limit')
      return
      ;;
    '--rpm-memory-request')
      return
      ;;
    '--rpm-memory-limit')
      return
      ;;
  esac

  if [[ "${curr_word}" == -* ]]; then
    COMPREPLY=( $(compgen -W "${flag_opts} ${arg_opts}" -- "${curr_word}") )
  else
    local positionals=""
    local IFS=$'\n'
    COMPREPLY=( $(compgen -W "${commands// /$'\n'}${IFS}${positionals}" -- "${curr_word}") )
  fi
}

# Generates completions for the options and subcommands of the `report` subcommand.
function _picocli_mbi_report() {
  # Get completion data
  local curr_word=${COMP_WORDS[COMP_CWORD]}
  local prev_word=${COMP_WORDS[COMP_CWORD-1]}

  local commands=""
  local flag_opts="'-t' '--tmt' '-q' '--quiet' '-h' '--help' '-V' '--version'"
  local arg_opts="'-m' '--plan' '-p' '--platform' '-s' '--subject' '-w' '--workflow' '-R' '--result-dir' '-r' '--report-dir'"

  type compopt &>/dev/null && compopt +o default

  case ${prev_word} in
    '-m'|'--plan')
      local IFS=$'\n'
      type compopt &>/dev/null && compopt -o filenames
      COMPREPLY=( $( compgen -f -- "${curr_word}" ) ) # files
      return $?
      ;;
    '-p'|'--platform')
      local IFS=$'\n'
      type compopt &>/dev/null && compopt -o filenames
      COMPREPLY=( $( compgen -f -- "${curr_word}" ) ) # files
      return $?
      ;;
    '-s'|'--subject')
      local IFS=$'\n'
      type compopt &>/dev/null && compopt -o filenames
      COMPREPLY=( $( compgen -f -- "${curr_word}" ) ) # files
      return $?
      ;;
    '-w'|'--workflow')
      local IFS=$'\n'
      type compopt &>/dev/null && compopt -o filenames
      COMPREPLY=( $( compgen -f -- "${curr_word}" ) ) # files
      return $?
      ;;
    '-R'|'--result-dir')
      local IFS=$'\n'
      type compopt &>/dev/null && compopt -o filenames
      COMPREPLY=( $( compgen -f -- "${curr_word}" ) ) # files
      return $?
      ;;
    '-r'|'--report-dir')
      local IFS=$'\n'
      type compopt &>/dev/null && compopt -o filenames
      COMPREPLY=( $( compgen -f -- "${curr_word}" ) ) # files
      return $?
      ;;
  esac

  if [[ "${curr_word}" == -* ]]; then
    COMPREPLY=( $(compgen -W "${flag_opts} ${arg_opts}" -- "${curr_word}") )
  else
    local positionals=""
    local IFS=$'\n'
    COMPREPLY=( $(compgen -W "${commands// /$'\n'}${IFS}${positionals}" -- "${curr_word}") )
  fi
}

# Generates completions for the options and subcommands of the `shell` subcommand.
function _picocli_mbi_shell() {
  # Get completion data
  local curr_word=${COMP_WORDS[COMP_CWORD]}

  local commands=""
  local flag_opts="'-p' '--provision' '-h' '--help' '-V' '--version'"
  local arg_opts=""

  if [[ "${curr_word}" == -* ]]; then
    COMPREPLY=( $(compgen -W "${flag_opts} ${arg_opts}" -- "${curr_word}") )
  else
    local positionals=""
    local IFS=$'\n'
    COMPREPLY=( $(compgen -W "${commands// /$'\n'}${IFS}${positionals}" -- "${curr_word}") )
  fi
}

# Define a completion specification (a compspec) for the
# `mbi`, `mbi.sh`, and `mbi.bash` commands.
# Uses the bash `complete` builtin (see [6]) to specify that shell function
# `_complete_mbi` is responsible for generating possible completions for the
# current word on the command line.
# The `-o default` option means that if the function generated no matches, the
# default Bash completions and the Readline default filename completions are performed.
complete -F _complete_mbi -o default mbi mbi.sh mbi.bash
