#!/usr/bin/env bash
# aws-controllers-k8s/ec2-chart 1.18.4 - base variant: eks-inference
# 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/aws-controllers-k8s-ec2-chart-1-18-4.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 eks-inference base variant into ./aws-controllers-k8s-ec2-chart-1-18-4-eks-inference"
cub installer setup --pull oci://europe-west1-docker.pkg.dev/nth-fort-499605-q5/helm-expt/aws-controllers-k8s-ec2-chart:1.18.4@sha256:40806df17d8ff6732613c13d151c5eff9c1e9551389718a9f3d3dac68d9f3721 --base eks-inference --work-dir ./aws-controllers-k8s-ec2-chart-1-18-4-eks-inference --non-interactive --namespace ack-system

say "Read what was rendered; nothing has touched the cluster yet"
ls ./aws-controllers-k8s-ec2-chart-1-18-4-eks-inference/out/manifests

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

if [ "${REQUIREMENTS_READY:-0}" != "1" ]; then
  cat >&2 <<'EOF_REQUIREMENTS'
This base variant needs resources you must create with your own values first:
  - Secret ack-system/aws-creds key credentials
    kubectl -n ack-system create secret generic aws-creds --from-literal=credentials=<value>
Complete these prerequisites before applying the rendered objects.
Replace any <...> placeholders with values or files for your environment.
When the resources exist, re-run with:
  REQUIREMENTS_READY=1 bash try.sh
EOF_REQUIREMENTS
  exit 1
fi

if [ -d ./aws-controllers-k8s-ec2-chart-1-18-4-eks-inference/out/secrets ]; then
  say "Apply rendered Secrets first"
  kubectl apply -f ./aws-controllers-k8s-ec2-chart-1-18-4-eks-inference/out/secrets
fi

say "Apply the rendered objects"
kubectl apply -f ./aws-controllers-k8s-ec2-chart-1-18-4-eks-inference/out/manifests

say "Done. The cluster received exactly the files in ./aws-controllers-k8s-ec2-chart-1-18-4-eks-inference/out."
