#!/usr/bin/env bash
# projectcalico/tigera-operator v3.32.0 - base variant: default
# Path: pull the package, render this base variant locally, read the objects,
# then apply them with kubectl. No ConfigHub account is needed.
# Generated by scripts/generate-public-site.mjs from the committed package
# data for this base variant. Chart page: https://confighub.github.io/helm-expt/site/charts/projectcalico-tigera-operator-v3-32-0.html
set -euo pipefail

say() { printf '\n>> %s\n' "$*"; }

if ! command -v cub >/dev/null 2>&1; then
  printf 'cub is not installed. Install the cub CLI with:\n  curl -fsSL https://hub.confighub.com/cub/install.sh | bash\nthen add ~/.confighub/bin to your PATH and re-run this script.\n' >&2
  exit 1
fi

# Installer is a cub plugin. Check it before this script does any work.
if ! cub installer --help >/dev/null 2>&1; then
  printf 'The cub installer plugin is missing. Follow https://confighub.github.io/helm-expt/site/try.html#install-cub, then re-run this script.\n' >&2
  exit 1
fi

if ! command -v kustomize >/dev/null 2>&1; then
  printf 'kustomize is missing. Follow https://confighub.github.io/helm-expt/site/try.html#install-cub, then re-run this script.\n' >&2
  exit 1
fi

if ! command -v kubectl >/dev/null 2>&1; then
  printf 'kubectl is required for the apply step.\n' >&2
  exit 1
fi

say "Pull the package and render the default base variant into ./projectcalico-tigera-operator-v3-32-0-default"
cub installer setup --pull oci://europe-west1-docker.pkg.dev/nth-fort-499605-q5/helm-expt/projectcalico-tigera-operator:v3.32.0 --base default --work-dir ./projectcalico-tigera-operator-v3-32-0-default --non-interactive --namespace default

say "Read what was rendered; nothing has touched the cluster yet"
ls ./projectcalico-tigera-operator-v3-32-0-default/out/manifests

wait_for_crd() {
  crd_name="$1"
  deadline=$(( $(date +%s) + 180 ))
  until kubectl get "crd/${crd_name}" >/dev/null 2>&1; do
    if [ "$(date +%s)" -ge "$deadline" ]; then
      printf "CRD %s did not appear within 180 seconds.\n" "$crd_name" >&2
      return 1
    fi
    sleep 2
  done
  kubectl wait --for=condition=Established --timeout=120s "crd/${crd_name}"
}

say "Ensure the default namespace exists"
kubectl create namespace default --dry-run=client -o yaml | kubectl apply -f -

say "Check 4 CRDs included with this package"
missing_crds=0
if ! kubectl get crd/apiservers.operator.tigera.io >/dev/null 2>&1; then
  missing_crds=1
fi
if ! kubectl get crd/goldmanes.operator.tigera.io >/dev/null 2>&1; then
  missing_crds=1
fi
if ! kubectl get crd/installations.operator.tigera.io >/dev/null 2>&1; then
  missing_crds=1
fi
if ! kubectl get crd/whiskers.operator.tigera.io >/dev/null 2>&1; then
  missing_crds=1
fi
if [ "$missing_crds" -eq 1 ]; then
  kubectl apply --server-side -k ./projectcalico-tigera-operator-v3-32-0-default/package/prerequisites/tigera-operator-bootstrap
else
  say "The required CRDs already exist; leave them under their current owner"
fi
wait_for_crd apiservers.operator.tigera.io
wait_for_crd goldmanes.operator.tigera.io
wait_for_crd installations.operator.tigera.io
wait_for_crd whiskers.operator.tigera.io

if [ -d ./projectcalico-tigera-operator-v3-32-0-default/out/secrets ]; then
  say "Apply rendered Secrets first"
  kubectl apply -f ./projectcalico-tigera-operator-v3-32-0-default/out/secrets
fi

say "Apply the rendered objects"
kubectl apply -f ./projectcalico-tigera-operator-v3-32-0-default/out/manifests

say "Done. The cluster received exactly the files in ./projectcalico-tigera-operator-v3-32-0-default/out."
