#!/usr/bin/env bash
# traefik/traefik 41.4.0 - base variant: default
# 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/traefik-traefik-41-4-0-default.json
# Entry page: https://confighub.github.io/helm-expt/site/charts/traefik-traefik-41-4-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=./traefik-traefik-41-4-0-default-objects
objects_url=https://raw.githubusercontent.com/confighub/helm-expt/main/recipes/traefik/traefik/41.4.0/revisions/default/r001/rendered/release-objects.yaml
objects_sha256=8e17fdeb313402d3b8cc3f7827c1f35d278e6809cf93ea0d7a9be31ea82c464b

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 31 objects. The cluster is untouched so far"
rm -rf "$work/docs" "$work/crds" "$work/rest"
mkdir -p "$work/docs" "$work/crds" "$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 0002 0004 0006 0008 0010 0012 0014 0016 0018 0020 0022 0024; do mv "$work/docs/doc-$n.yaml" "$work/crds/"; done
for n in 0026 0028 0030 0032 0034 0036 0038 0040 0042 0044 0046 0048; do mv "$work/docs/doc-$n.yaml" "$work/crds/"; done
for n in 0050; do mv "$work/docs/doc-$n.yaml" "$work/crds/"; done
for n in 0051 0052 0053 0054 0055 0056; do mv "$work/docs/doc-$n.yaml" "$work/rest/"; done
ls "$work/crds" "$work/rest"

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 traefik namespace if it does not exist"
if ! kubectl get namespace traefik >/dev/null 2>&1; then
  kubectl create namespace traefik
fi

wait_for_crd() {
  crd_name="$1"
  deadline=$(( $(date +%s) + 180 ))
  until kubectl get "crd/${crd_name}" >/dev/null 2>&1; do
    if [ "$(date +%s)" -ge "$deadline" ]; then
      printf "CRD %s did not appear within 180 seconds.\n" "$crd_name" >&2
      return 1
    fi
    sleep 2
  done
  kubectl wait --for=condition=Established --timeout=120s "crd/${crd_name}"
}

say "Check the 25 CRDs this file holds"
missing_crds=0
if ! kubectl get crd/accesscontrolpolicies.hub.traefik.io >/dev/null 2>&1; then
  missing_crds=1
fi
if ! kubectl get crd/aiservices.hub.traefik.io >/dev/null 2>&1; then
  missing_crds=1
fi
if ! kubectl get crd/apiauths.hub.traefik.io >/dev/null 2>&1; then
  missing_crds=1
fi
if ! kubectl get crd/apibundles.hub.traefik.io >/dev/null 2>&1; then
  missing_crds=1
fi
if ! kubectl get crd/apicatalogitems.hub.traefik.io >/dev/null 2>&1; then
  missing_crds=1
fi
if ! kubectl get crd/apiplans.hub.traefik.io >/dev/null 2>&1; then
  missing_crds=1
fi
if ! kubectl get crd/apiportalauths.hub.traefik.io >/dev/null 2>&1; then
  missing_crds=1
fi
if ! kubectl get crd/apiportals.hub.traefik.io >/dev/null 2>&1; then
  missing_crds=1
fi
if ! kubectl get crd/apiratelimits.hub.traefik.io >/dev/null 2>&1; then
  missing_crds=1
fi
if ! kubectl get crd/apis.hub.traefik.io >/dev/null 2>&1; then
  missing_crds=1
fi
if ! kubectl get crd/apiversions.hub.traefik.io >/dev/null 2>&1; then
  missing_crds=1
fi
if ! kubectl get crd/contentitems.hub.traefik.io >/dev/null 2>&1; then
  missing_crds=1
fi
if ! kubectl get crd/managedapplications.hub.traefik.io >/dev/null 2>&1; then
  missing_crds=1
fi
if ! kubectl get crd/managedsubscriptions.hub.traefik.io >/dev/null 2>&1; then
  missing_crds=1
fi
if ! kubectl get crd/uplinks.hub.traefik.io >/dev/null 2>&1; then
  missing_crds=1
fi
if ! kubectl get crd/ingressroutes.traefik.io >/dev/null 2>&1; then
  missing_crds=1
fi
if ! kubectl get crd/ingressroutetcps.traefik.io >/dev/null 2>&1; then
  missing_crds=1
fi
if ! kubectl get crd/ingressrouteudps.traefik.io >/dev/null 2>&1; then
  missing_crds=1
fi
if ! kubectl get crd/middlewares.traefik.io >/dev/null 2>&1; then
  missing_crds=1
fi
if ! kubectl get crd/middlewaretcps.traefik.io >/dev/null 2>&1; then
  missing_crds=1
fi
if ! kubectl get crd/serverstransports.traefik.io >/dev/null 2>&1; then
  missing_crds=1
fi
if ! kubectl get crd/serverstransporttcps.traefik.io >/dev/null 2>&1; then
  missing_crds=1
fi
if ! kubectl get crd/tlsoptions.traefik.io >/dev/null 2>&1; then
  missing_crds=1
fi
if ! kubectl get crd/tlsstores.traefik.io >/dev/null 2>&1; then
  missing_crds=1
fi
if ! kubectl get crd/traefikservices.traefik.io >/dev/null 2>&1; then
  missing_crds=1
fi
if [ "$missing_crds" -eq 1 ]; then
  say "A CRD is missing, so apply the file's CRDs first"
  kubectl apply --server-side -f "$work/crds"
  crd_outcome="were applied first"
elif kubectl diff --server-side -f "$work/crds" >/dev/null 2>&1; then
  say "The CRDs already exist and match this file. The script leaves them under their current owner"
  crd_outcome="already existed and matched, so the script left them alone"
elif [ "${APPLY_CRDS:-0}" = "1" ]; then
  say "The existing CRDs differ from this file. APPLY_CRDS=1 is set, so the script applies the file's CRDs"
  kubectl apply --server-side -f "$work/crds"
  crd_outcome="differed and were applied, because APPLY_CRDS=1 was set"
else
  say "The CRDs already exist and differ from this file, or another tool owns them"
  cat <<'EOF_CRDS'
This script does not change CRDs it did not just create.
If this is an upgrade, the objects that follow may need the newer CRDs.
To let this script apply the file's CRDs, re-run with:
  APPLY_CRDS=1 bash try-objects.sh
EOF_CRDS
  exit 1
fi
wait_for_crd accesscontrolpolicies.hub.traefik.io
wait_for_crd aiservices.hub.traefik.io
wait_for_crd apiauths.hub.traefik.io
wait_for_crd apibundles.hub.traefik.io
wait_for_crd apicatalogitems.hub.traefik.io
wait_for_crd apiplans.hub.traefik.io
wait_for_crd apiportalauths.hub.traefik.io
wait_for_crd apiportals.hub.traefik.io
wait_for_crd apiratelimits.hub.traefik.io
wait_for_crd apis.hub.traefik.io
wait_for_crd apiversions.hub.traefik.io
wait_for_crd contentitems.hub.traefik.io
wait_for_crd managedapplications.hub.traefik.io
wait_for_crd managedsubscriptions.hub.traefik.io
wait_for_crd uplinks.hub.traefik.io
wait_for_crd ingressroutes.traefik.io
wait_for_crd ingressroutetcps.traefik.io
wait_for_crd ingressrouteudps.traefik.io
wait_for_crd middlewares.traefik.io
wait_for_crd middlewaretcps.traefik.io
wait_for_crd serverstransports.traefik.io
wait_for_crd serverstransporttcps.traefik.io
wait_for_crd tlsoptions.traefik.io
wait_for_crd tlsstores.traefik.io
wait_for_crd traefikservices.traefik.io

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

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