#!/usr/bin/env bash
# cloudnative-pg/cloudnative-pg 0.28.2 - base variant: no-crds
# Path: render this base variant locally, then upload the objects to your
# ConfigHub Space as Units you can edit, diff, and deliver.
# Needs a ConfigHub account: run cub auth login once before this script.
# 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/cloudnative-pg-cloudnative-pg-0-28-2.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

SPACE="${CUB_SPACE:-helm-cloudnative-pg-no-crds}"

say "Check ConfigHub auth"
if ! cub auth status >/dev/null 2>&1; then
  printf 'Not logged in. Run: cub auth login\nThen re-run this script.\n' >&2
  exit 1
fi

say "Render the no-crds base variant into ./cloudnative-pg-cloudnative-pg-0-28-2-no-crds"
cub installer setup --pull oci://europe-west1-docker.pkg.dev/nth-fort-499605-q5/helm-expt/cloudnative-pg-cloudnative-pg:0.28.2 --base no-crds --work-dir ./cloudnative-pg-cloudnative-pg-0-28-2-no-crds --non-interactive --namespace default

say "Upload the rendered objects to Space ${SPACE} (created on first upload)"
cub installer upload --work-dir ./cloudnative-pg-cloudnative-pg-0-28-2-no-crds --space "${SPACE}"

say "Uploaded. See your Units:"
printf '  cub unit list --space %s\n  or open https://hub.confighub.com and find that Space.\n' "${SPACE}"

say "Next: create an environment variant and promote reviewed changes"
printf '  Walkthrough with the why behind each flag:\n  https://confighub.github.io/helm-expt/site/d/docs/user/variants-after-upload.html\n'

# Before applying this base variant from ConfigHub to a cluster, it still needs:
#   - Secret default/cnpg-webhook-cert keys tls.crt,tls.key (suggested: kubectl -n default create secret generic cnpg-webhook-cert --from-literal=tls.crt=<value> --from-literal=tls.key=<value>)
#   - CRD backups.postgresql.cnpg.io (included in the package as prerequisites/target-facts/no-crds-crds.yaml)
#   - CRD clusterimagecatalogs.postgresql.cnpg.io (included in the package as prerequisites/target-facts/no-crds-crds.yaml)
#   - CRD clusters.postgresql.cnpg.io (included in the package as prerequisites/target-facts/no-crds-crds.yaml)
#   - CRD databases.postgresql.cnpg.io (included in the package as prerequisites/target-facts/no-crds-crds.yaml)
#   - CRD failoverquorums.postgresql.cnpg.io (included in the package as prerequisites/target-facts/no-crds-crds.yaml)
#   - CRD imagecatalogs.postgresql.cnpg.io (included in the package as prerequisites/target-facts/no-crds-crds.yaml)
#   - CRD poolers.postgresql.cnpg.io (included in the package as prerequisites/target-facts/no-crds-crds.yaml)
#   - CRD publications.postgresql.cnpg.io (included in the package as prerequisites/target-facts/no-crds-crds.yaml)
#   - CRD scheduledbackups.postgresql.cnpg.io (included in the package as prerequisites/target-facts/no-crds-crds.yaml)
#   - CRD subscriptions.postgresql.cnpg.io (included in the package as prerequisites/target-facts/no-crds-crds.yaml)
