# shellcheck shell=bash

main() {
  if [[ $# -eq 0 ]]; then
    usage
    exit 2
  fi
  case "$1" in
    -h|--help)
      cat <<'HELP'
okstra-ctl: okstra Control Center CLI.

Subcommands: projects, list, show, open, tail, rerun, batch, reconcile, reindex.
HELP
      usage
      exit 0
      ;;
    projects|list|show|open|tail|rerun|batch|reconcile|reindex)
      local sub="$1"; shift
      # reindex 는 사용자가 명시적으로 지정한 --project 필터를 가지므로,
      # 이 경로에서 자동(unqualified) backfill 을 추가로 돌리면 사용자
      # scope 을 무시하고 모든 프로젝트를 스캔하게 된다. reindex 자체가
      # backfill 을 수행하므로 prepare 의 자동 backfill 단계는 건너뛴다.
      if [[ "$sub" == "reindex" ]]; then
        OKSTRA_CTL_SKIP_BACKFILL=1 _okstra_ctl_prepare
      elif [[ "$sub" == "reconcile" ]]; then
        # 사용자 --project 필터를 보존하기 위해 prepare 의 unscoped
        # reconcile 단계를 건너뛴다(서브커맨드 본체가 스코프 reconcile 수행).
        OKSTRA_CTL_SKIP_RECONCILE=1 _okstra_ctl_prepare
      else
        _okstra_ctl_prepare
      fi
      "_okstra_ctl_${sub}" "$@"
      ;;
    *)
      printf 'unknown subcommand: %s\n' "$1" >&2
      usage
      exit 2
      ;;
  esac
}
