#compdef lttng-crash
#
# SPDX-FileCopyrightText: 2015-2023 Philippe Proulx <eeppeliteloop@gmail.com>
# SPDX-License-Identifier: MIT
#
# This is a Zsh completion function for the lttng-crash(1) command (see
# <https://lttng.org/>), for versions 2.7 to 2.15.
#
# If you want, at your own risk, the function to work with versions
# above 2.15, set `LTTNG_ZSH_COMP_IGNORE_VERSION_LIMIT=1`.

# Sets the `minor_version` parameter to the minor version of
# LTTng-tools, or to `0` if not found.
__lttng_set_minor_version() {
  minor_version=0

  local -a match

  if [[ $($words[1] --version) =~ '[[:blank:]]+2\.([[:digit:]]+)' ]]; then
    minor_version=$match[1]
  fi
}

# Adds completions for the arguments of the `lttng-crash` command.
__lttng_complete_lttng_crash() {
  local curcontext=$curcontext state state_descr line
  local -A opt_args
  local specs=(
    '*'{-v,--verbose}'[increase verbosity]'
    '(- : *)'{-V,--version}'[show version and quit]'
    '(- : *)'{-h,--help}'[show help]'
    '(-x --extract)'{-x+,--extract=}'[set the path of the directory where to extract the trace]:trace extraction directory path:_directories'
    '(-e --viewer)'{-e+,--viewer=}'[set the trace reader command]:trace reader command:_files'
    '1:shared memory directory:_directories'
  )

  _arguments -s -w : $specs
}

# Ensure predictable Zsh behavior and prevent option leakage
emulate -L zsh
setopt extended_glob

# First, set the `minor_version` parameter to the minor version of
# LTTng-tools. Some features depend on a specific version and this
# completion function supports many versions from LTTng-tools 2.7.
local -i minor_version

__lttng_set_minor_version

# Exit now with LTTng-tools < 2.7 or LTTng-tools > 2.15
local -r ignore_version_limit=${LTTNG_ZSH_COMP_IGNORE_VERSION_LIMIT:-0}

if ((minor_version < 7 || (minor_version > 15 && !ignore_version_limit))); then
  _message "completion not available for LTTng-tools 2.$minor_version; please update the completion files or set \`LTTNG_ZSH_COMP_IGNORE_VERSION_LIMIT=1\`"
  return 1
fi

# Add completions for lttng-crash(1)
__lttng_complete_lttng_crash "$@"
