#!/usr/bin/env bash
# nimservice 5d5f66ee0e2e6e2fefa7babe3b1852826dc58f8f - base variant: epp-meta-llama3-8b-instruct
# 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/nimservice-epp-meta-llama3-8b-instruct.json
# Entry page: https://confighub.github.io/helm-expt/site/nimservice.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=./nimservice-epp-meta-llama3-8b-instruct-objects
objects_url=https://raw.githubusercontent.com/confighub/helm-expt/main/data/aicr-nim-operator-models/upstream/serving/advanced/epp/nimservice.yaml
objects_sha256=eb3952f4209f4b63a9cce3e232b28c01016c55fe28e3128ae0cbda3559382668

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 1 object. The cluster is untouched so far"
rm -rf "$work/docs" "$work/rest"
mkdir -p "$work/docs" "$work/rest"
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 0000; do mv "$work/docs/doc-$n.yaml" "$work/rest/"; done
ls "$work/rest"

if [ "${REQUIREMENTS_READY:-0}" != "1" ]; then
  say "Stop before applying anything. This entry's plan has 4 steps that an install needs first"
  cat >&2 <<'EOF_REQUIREMENTS'
This script cannot carry out these steps of the plan. They are yours to do, with your own values:
  - Install the k8s-nim-operator chart from https://helm.ngc.nvidia.com/nvidia at >=3.1.0 and wait for nimservices.apps.nvidia.com before applying this sample. The k8s-nim-operator chart is not yet a Catalog entry on this branch, so this requirement names the chart and does not link a record.
  - Create the image pull Secret ngc-secret (NIMService spec.image.pullSecrets) in the sample's namespace before apply. The sample carries the name only, and no credential is held here.
  - Create the API key Secret ngc-api-secret (NIMService spec.authSecret) in the sample's namespace before apply. The sample carries the name only, and no key or token is held here.
  - The NIMService reads its model from NIMCache meta-llama3-8b-instruct, which this sample file does not define. Nothing here checked that a destination can provide it.
Nothing was applied. Complete them before you apply the objects.
When they are done, re-run with:
  REQUIREMENTS_READY=1 bash try-objects.sh
EOF_REQUIREMENTS
  exit 1
fi
say "REQUIREMENTS_READY=1 is set. The script takes your word for 4 steps and does not check them"

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

serves_kind() {
  kubectl api-resources --api-group="$1" --no-headers 2>/dev/null | awk -v kind="$2" '$NF == kind { found = 1 } END { exit found ? 0 : 1 }'
}

say "Check that the cluster serves the 1 kind this file uses and does not define"
missing_kinds=""
if ! serves_kind apps.nvidia.com NIMService; then
  missing_kinds="$missing_kinds NIMService.apps.nvidia.com"
fi
if [ -n "$missing_kinds" ]; then
  printf 'The cluster does not serve:%s\n' "$missing_kinds" >&2
  printf 'This file uses these kinds and does not define them. Install what defines them, then re-run this script. Nothing was applied.\n' >&2
  exit 1
fi

say "Apply the 1 object"
kubectl apply -f "$work/rest"

say "Done. kubectl applied the 1 object of this entry's retained file."
say "The script stops here. It did not wait for any workload to start"
say "Not run by this script: 4 steps of this entry's plan. The entry page says who runs each one."
cat <<'EOF_NOT_RUN'
   - Install the k8s-nim-operator chart from https://helm.ngc.nvidia.com/nvidia at >=3.1.0 and wait for nimservices.apps.nvidia.com before applying this sample. The k8s-nim-operator chart is not yet a Catalog entry on this branch, so this requirement names the chart and does not link a record.
   - Create the image pull Secret ngc-secret (NIMService spec.image.pullSecrets) in the sample's namespace before apply. The sample carries the name only, and no credential is held here.
   - Create the API key Secret ngc-api-secret (NIMService spec.authSecret) in the sample's namespace before apply. The sample carries the name only, and no key or token is held here.
   - The NIMService reads its model from NIMCache meta-llama3-8b-instruct, which this sample file does not define. Nothing here checked that a destination can provide it.
EOF_NOT_RUN
