#!/usr/bin/env bash

# Copyright 2025 The Kubermatic Kubernetes Platform contributors.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

set -euo pipefail

cd $(dirname $0)/..
source hack/lib.sh

if [ "$#" -lt 1 ] || [ "$1" == "--help" ]; then
  cat << EOF
Usage: $(basename $0) (version)
where:
    version:    Kyverno version used to generate the CRDs, e.g. v1.14.1
e.g: $(basename $0) v1.14.1
EOF
  exit 0
fi

containerize ./hack/update-kyverno-crds.sh $1

# Handle containerized execution
if is_containerized; then
  version="$2"
else
  version="$1"
fi

crd_dir="pkg/ee/kyverno/resources/user-cluster/static/kyverno-crds"
mkdir -p "$crd_dir"
cd "$crd_dir"

# Clean existing CRDs
rm -f *.yaml

echodate "Downloading Kyverno CRDs for version $version..."
url="https://github.com/kyverno/kyverno/releases/download/$version/install.yaml"

curl -sL "$url" |
  yq eval-all --split-exp '(.metadata.name + ".yaml")' 'select(.kind == "CustomResourceDefinition")' -

for crd_filename in *.yaml; do
  [ -e "$crd_filename" ] || continue

  processed_content_tmpfile=$(mktemp "./${crd_filename}.processed.XXXXXX")

  if yq eval 'del(.metadata.creationTimestamp)' "$crd_filename" > "$processed_content_tmpfile"; then
    echo "# This file has been generated by hack/update-kyverno-crds.sh using Kyverno version: $version. Do not edit." > "$crd_filename"

    cat "$processed_content_tmpfile" >> "$crd_filename"
  else
    echodate "Error: Failed to process $crd_filename with yq. It may be left unmodified or corrupted." >&2
  fi
  rm -f "$processed_content_tmpfile"
done

echodate "Successfully updated Kyverno CRDs in $crd_dir"
