#!/bin/bash

_kaiming-builder() {
  local pre cur opts
  COMPREPLY=()
  pre="${COMP_WORDS[COMP_CWORD-1]}"
  cur="${COMP_WORDS[COMP_CWORD]}"

  case "${pre}" in
    "kaiming-builder")
    opts=$(echo "create build enter query create-from-debsource --help -h --bundle-filesystem=squashfs"  | tr '' '\n' | sort | tr '\n' ' ')
      ;;
    "build")
      opts="--help -h --verbose -v --arch --build-only --finish-only --export-only --export-all --force-clean -f --keep-build-dirs -k --stop-at --jobs MANIFEST --bundle-filesystem=squashfs"
      ;;
    "enter")
      opts="--help -h"
      ;;
    "-v" | "-f" | "-k")
      opts="--export-only --export-all"
      ;;
    "--export-all")
      opts="--bundle-filesystem=squashfs"
      ;;
    *)
      opts=""
      ;;
  esac

  # 只要确定是kaiming-builder build就补全为kaiming-builder build -v -f -k
  if [[ "${pre}" == "kaiming-builder" && "${cur}" =~ ^b(uild)? ]]; then
    COMPREPLY=("build -f -k")
  else
  # kaiming-builder build 显示后面可以跟的参数选项
      COMPREPLY=( $(compgen -f -W "${opts}" -- ${cur}) )
  fi

  return 0

}

complete -F _kaiming-builder kaiming-builder


