#!/bin/bash

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

  #if [[ ${cur} == .* ]]; then
    case "${pre}" in
      "kaiming")
	      opts="install repair uninstall update list info enter export run ps kill search build build-init build-finish build-export --help --version -h -v"
        ;;
      "run")
        opts="$(find /opt/kaiming/layers -mindepth 4 -maxdepth 4 -type d \
              -path '*/app/*' -o \
              -path '*/base/*' -o \
              -path '*/runtime/*' -o \
              -path '*/extension/*' -o \
              -path '*/depend/*' 2>/dev/null | \
              xargs -I {} basename {} | sort | uniq ) \
              --help --verbose -h -v"
        ;;
      "uninstall")
        opts="$(find /opt/kaiming/layers -mindepth 4 -maxdepth 4 -type d \
              -path '*/app/*' -o \
              -path '*/base/*' -o \
              -path '*/runtime/*' -o \
              -path '*/extension/*' -o \
              -path '*/depend/*' 2>/dev/null | \
              xargs -I {} basename {} | sort | uniq ) \
              --help --verbose --assumeyes --arch= --unused --delete-data -h -y -v"
        ;;
      "info")
        # opts=$(find /opt/kaiming/layers/*/app -mindepth 1 -maxdepth 1 -type d -exec basename {} \;)  # 也可以实现
        opts="$(find /opt/kaiming/layers -mindepth 4 -maxdepth 4 -type d \
              -path '*/app/*' -o \
              -path '*/base/*' -o \
              -path '*/runtime/*' -o \
              -path '*/extension/*' -o \
              -path '*/depend/*' 2>/dev/null | \
              xargs -I {} basename {} | sort | uniq ) \
              --help --verbose -h -v"
        ;;
      "list")
	      opts="--help --verbose --arch= --kind=  -h -v"
        ;;
      "enter")
	      opts="--help -h"
        ;;
      "ps")
	      opts="--help -h"
        ;;
      "kill")
	      opts="--help -h"
        ;;
      "export")
        opts="$(find /opt/kaiming/layers -mindepth 4 -maxdepth 4 -type d \
              -path '*/app/*' -o \
              -path '*/base/*' -o \
              -path '*/runtime/*' -o \
              -path '*/extension/*' -o \
              -path '*/depend/*' 2>/dev/null | \
              xargs -I {} basename {} | sort | uniq ) \
              --help -y -v"
        ;;
      "build-init")
        opts="--help --arch= --runtime= --base --kind= --channel= -h"
        ;;
      "build")
        opts="--help --build-mount= --build-dir= --with-appdir --die-with-parent= --env= --unset-env= -h -p"
        ;;
      "build-finish")
        opts="--help --extra-data --env= --unset-env= -h"
        ;;
      "build-export")
        opts="--help --arch= --exclude --include -h"
        ;;
      # 假设远程库里的包名都存在/opt/kaiming/packages文件中
      "install")
        if [[ -f /opt/kaiming/packages ]]; then
          opts="$(cat /opt/kaiming/packages | sort | uniq | tr '\n' ' ') --help --verbose --assumeyes --arch= -h -y -v" 
        else
          opts="--help --verbose --assumeyes --arch= -h -y -v"
        fi
        ;;
      "repair")
        if [[ -f /opt/kaiming/packages ]]; then
          opts="$(cat /opt/kaiming/packages | sort | uniq | tr '\n' ' ') --help --verbose --assumeyes --arch= -h -y -v"
        else
          opts="--help --verbose --assumeyes --arch= -h -y -v"
        fi
        ;;
      "update")
        if [[ -f /opt/kaiming/packages ]]; then
          opts="$(cat /opt/kaiming/packages | sort | uniq | tr '\n' ' ') --help --verbose --assumeyes --arch= -h -y -v"
        else
          opts="--help --verbose --assumeyes --arch= -h -y -v"
        fi
        ;;
      "search")
        if [[ -f /opt/kaiming/packages ]]; then
          opts="$(cat /opt/kaiming/packages | sort | uniq | tr '\n' ' ') --help --verbose --assumeyes --arch= -h -v"
        else
          opts="--help --verbose --arch= -h -v"
        fi
        ;;
      *)
        opts=""
        ;;
    esac
    COMPREPLY=( $(compgen -W "${opts}" -- ${cur}) )
    return 0
  #fi
}

complete -F _kaiming kaiming


