#!/usr/bin/env bash
set -euo pipefail

bootstrap_enabled=false
if [ -f bootstrap/config.yaml ] && grep -Eq '^[[:space:]]*enabled:[[:space:]]*true[[:space:]]*$' bootstrap/config.yaml; then
  bootstrap_enabled=true
fi

mapfile -t manifests < <(
  find . -type f -name '*.yaml' \
    ! -path './.gitea/*' \
    ! -path './manifest/components/example-component/*' \
    ! -name '*kustomization.yaml' \
    ! -path './bootstrap/config.yaml' \
    | sort
)

if [ "$bootstrap_enabled" != "true" ]; then
  filtered=()
  for manifest in "${manifests[@]}"; do
    [ "$manifest" = "./bootstrap/applicationset.yaml" ] && continue
    filtered+=("$manifest")
  done
  manifests=("${filtered[@]}")
fi

if [ "${#manifests[@]}" -eq 0 ]; then
  echo "No manifests found"
  exit 0
fi

printf '%s\n' "${manifests[@]}" \
  | xargs kubeconform \
      -strict \
      -kubernetes-version 1.35.0 \
      -schema-location default \
      -schema-location 'http://gitea-ha-http.apps:3000/olb042/kubeconform/raw/branch/main/crdSchemas/{{ .ResourceKind }}_{{ .ResourceAPIVersion }}.json' \
      -summary