#compdef kdesrc-build kde-builder --run

# SPDX-FileCopyrightText: 2022 Ivan Tkachenko <me@ratijas.tk>
# SPDX-FileCopyrightText: 2023 - 2024 Andrew Shark <ashark@linuxcomp.ru>
#
# SPDX-License-Identifier: GPL-2.0-or-later

local exec_cmd i

# Scan command line for --exec argument and its value.
# I could not find any easier built-in way of doing this.
for (( i = 1; i <= ${#words}; i++ )); do
  case $words[$i] in
    -e=*|--exec=*)
      exec_cmd="${${words[$i]}#*=}"
      break
      ;;
    -e|--exec)
      (( i += 1 ))
      exec_cmd="${words[$i]}"
      break
      ;;
  esac
done

_kdesrc_run_precommand() {
  local module

  # trick zsh into thinking that we are now completing opitons for $exec_cmd (if any)
  if [[ -n "$exec_cmd" ]]; then
    module=${words[1]}
    words[1]=$exec_cmd
    line=${line:/$module/$exec_cmd}
  fi

  _normal
}

_arguments -s \
  "(-f --fork)"{-f,--fork}"[Launch the program in a new session]" \
  "(-)1: :_kde-builder_modules" \
  "*:: :_kdesrc_run_precommand"
