#!/usr/bin/env bash

# Copyright Authors of Cilium
# SPDX-License-Identifier: Apache-2.0

set -o errexit
set -o pipefail
set -o nounset

root_dir="$(git rev-parse --show-toplevel)"

cd "${root_dir}"

if [ "$#" -ne 1 ] ; then
  echo "$0 supports exactly 1 argument - <cni_version>"
  exit 1
fi

cni_version="${1}"

# this is a simple array that assumes the order of the loop;
# it's not an associative array because this script needs to
# work on any version of bash, and (most notably) macOS ships
# an old version that doesn't support associative arrays
cni_sha512=()

for arch in amd64 arm64 ; do
  tmpout="$(mktemp)"
  curl --fail --show-error --silent --location \
    "https://github.com/containernetworking/plugins/releases/download/v${cni_version}/cni-plugins-linux-${arch}-v${cni_version}.tgz.sha512" \
    --output "${tmpout}"
  read -ra sha512 < "${tmpout}"
  rm -f "${tmpout}"
  cni_sha512+=("${sha512[0]}")
done

cat > "${root_dir}/images/runtime/cni-version.sh" << EOF
# Code generated by images/scripts/update-cni-version.sh; DO NOT EDIT.
cni_version="${cni_version}"
declare -A cni_sha512
cni_sha512[amd64]="${cni_sha512[0]}"
cni_sha512[arm64]="${cni_sha512[1]}"
EOF
