hack/scripts/update-helm-role.sh
#!/bin/bash

#  2025 NVIDIA CORPORATION & AFFILIATES
#
#  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 -o nounset
set -o pipefail
set -o errexit

if [[ "${TRACE-0}" == "1" ]]; then
    set -o xtrace
fi

INPUT_FILE="config/rbac/role.yaml"
OUTPUT_FILE="deployment/network-operator/templates/role.yaml"
COPYRIGHT_FILE="hack/boilerplate.go.txt"

# Strip leading/trailing comment delimiters (/* */) and indent each line
COPYRIGHT=$(awk '
    BEGIN { skip = 0 }
    /^\s*\/\*/ { skip = 1; next }
    /^\s*\*\/$/ { skip = 0; next }
    skip { print "  " $0 }
' "$COPYRIGHT_FILE")

# Wrap in Helm-style block comment
COPYRIGHT_BLOCK="{{/*
$COPYRIGHT
  // Code generated by controller-gen. DO NOT EDIT.
*/}}"

YAML_HEADER='apiVersion: rbac.authorization.k8s.io/v1
kind: ClusterRole
metadata:
  creationTimestamp: null
  name: {{ include "network-operator.fullname" . }}
'

# Extract only the `rules:` section from the input file (starting from "rules:" onward)
RULES_SECTION=$(awk '/^rules:/,0' "$INPUT_FILE")

# Ensure destination directory exists
mkdir -p "$(dirname "$OUTPUT_FILE")"

# Combine and write to output
{
  echo "$COPYRIGHT_BLOCK"
  echo "$YAML_HEADER"
  echo "$RULES_SECTION"
} > "$OUTPUT_FILE"
