#!/usr/bin/env bash
# bitnami/rabbitmq 16.0.14 - base variant: existing-secret
# 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/bitnami-rabbitmq-16-0-14.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 existing-secret base variant into ./bitnami-rabbitmq-16-0-14-existing-secret"
cub installer setup --pull oci://europe-west1-docker.pkg.dev/nth-fort-499605-q5/helm-expt/bitnami-rabbitmq:16.0.14 --base existing-secret --work-dir ./bitnami-rabbitmq-16-0-14-existing-secret --non-interactive --namespace rabbitmq

say "Read what was rendered; nothing has touched the cluster yet"
ls ./bitnami-rabbitmq-16-0-14-existing-secret/out/manifests

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

say "Requirement before apply: Secret rabbitmq/rabbitmq-auth key rabbitmq-password"
if ! kubectl -n rabbitmq create secret generic rabbitmq-auth --from-literal=rabbitmq-password="$(openssl rand -base64 32)"; then
  printf '!! The requirement command failed. If the resource already exists, review it and re-run; otherwise fix the error above.\n' >&2
  exit 1
fi

say "Requirement before apply: Secret rabbitmq/rabbitmq-erlang-cookie key rabbitmq-erlang-cookie"
if ! kubectl -n rabbitmq create secret generic rabbitmq-erlang-cookie --from-literal=rabbitmq-erlang-cookie="$(openssl rand -base64 48)"; then
  printf '!! The requirement command failed. If the resource already exists, review it and re-run; otherwise fix the error above.\n' >&2
  exit 1
fi

if [ -d ./bitnami-rabbitmq-16-0-14-existing-secret/out/secrets ]; then
  say "Apply rendered Secrets first"
  kubectl apply -f ./bitnami-rabbitmq-16-0-14-existing-secret/out/secrets
fi

say "Apply the rendered objects"
kubectl apply -f ./bitnami-rabbitmq-16-0-14-existing-secret/out/manifests

say "Done. The cluster received exactly the files in ./bitnami-rabbitmq-16-0-14-existing-secret/out."
