#!/usr/bin/env bash
# nvidia/nvsentinel v1.26.0 - base variant: no-pod-monitor
# Path: fetch this entry's retained objects, check their SHA-256, then apply
# them with kubectl. It uses curl, shasum, awk and kubectl. It needs no cub
# CLI and no ConfigHub account.
# Generated by scripts/generate-public-site.mjs from this entry's listing:
# https://confighub.github.io/helm-expt/site/listings/nvidia-nvsentinel-v1-26-0-no-pod-monitor.json
# Entry page: https://confighub.github.io/helm-expt/site/charts/nvidia-nvsentinel-v1-26-0.html
set -euo pipefail

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

for tool in curl awk kubectl; do
  if ! command -v "$tool" >/dev/null 2>&1; then
    printf '%s is required and was not found.\n' "$tool" >&2
    exit 1
  fi
done
if command -v shasum >/dev/null 2>&1; then
  sha256_of() { shasum -a 256 "$1" | awk '{print $1}'; }
elif command -v sha256sum >/dev/null 2>&1; then
  sha256_of() { sha256sum "$1" | awk '{print $1}'; }
else
  printf 'shasum is required and was not found.\n' >&2
  exit 1
fi

work=./nvidia-nvsentinel-v1-26-0-no-pod-monitor-objects
objects_url=https://raw.githubusercontent.com/confighub/helm-expt/main/recipes/nvidia/nvsentinel/v1.26.0/revisions/no-pod-monitor/r001/rendered/release-objects.yaml
objects_sha256=46e1eea6fdb17a85b71a8e36978dc96364ed5d65ee7951b02b22cf7de8733c00

say "Fetch the retained objects into $work/objects.yaml"
mkdir -p "$work"
curl -fsSL -o "$work/objects.yaml" "$objects_url"

say "Check the file against the SHA-256 in the listing"
fetched_sha256="$(sha256_of "$work/objects.yaml")"
if [ "$fetched_sha256" != "$objects_sha256" ]; then
  printf 'The file differs from the listing, so the script stops. Nothing was applied.\n  listing: %s\n  fetched: %s\n' "$objects_sha256" "$fetched_sha256" >&2
  exit 1
fi
printf 'The file matches: sha256 %s\n' "$fetched_sha256"

say "Split the file into its 21 objects. The cluster is untouched so far"
rm -rf "$work/docs" "$work/rest" "$work/unplaced"
mkdir -p "$work/docs" "$work/rest" "$work/unplaced"
awk -v dir="$work/docs" 'BEGIN { n = 0; f = sprintf("%s/doc-%04d.yaml", dir, n) } /^---[ \t]*$/ { close(f); n++; f = sprintf("%s/doc-%04d.yaml", dir, n); next } { print > f }' "$work/objects.yaml"
for n in 0009 0010 0011 0012 0013 0014; do mv "$work/docs/doc-$n.yaml" "$work/rest/"; done
for n in 0001 0002 0003 0004 0005 0006 0007 0008 0015 0016 0017 0018; do mv "$work/docs/doc-$n.yaml" "$work/unplaced/"; done
for n in 0019 0020 0021; do mv "$work/docs/doc-$n.yaml" "$work/unplaced/"; done
ls "$work/rest" "$work/unplaced"

say "Check that kubectl reaches a cluster"
if ! kubectl get --raw /version >/dev/null 2>&1; then
  printf 'kubectl cannot reach a cluster. Point it at a throwaway cluster and re-run this script.\n' >&2
  exit 1
fi
kubectl config current-context 2>/dev/null || true

say "Create the nvsentinel namespace if it does not exist"
if ! kubectl get namespace nvsentinel >/dev/null 2>&1; then
  kubectl create namespace nvsentinel
fi

say "Apply the 6 objects"
kubectl apply -f "$work/rest"

say "Apply 15 objects that name no namespace into nvsentinel, the namespace this entry was built for"
kubectl apply -n nvsentinel -f "$work/unplaced"

say "Done. kubectl applied the 21 objects of this entry's retained file."
say "The script stops here. It did not wait for any workload to start"
