# The original code of this source file comes from
# https://github.com/rancher/rancher/blob/1f6bae66d73aed3f214c48f895cac06cb6b9a322/scripts/package-env
# 
# This is necessary in order to properly source the versions of images that are
# owned/used by Rancher, but are developed outside of the rancher/rancher repo.
# For more context, please see the issue
# https://github.com/rancher/image-scanning/issues/232 .
#
# In case new variables and versions are added in the original upstream code and
# if they are required by the image exporter Go script, then we might have to
# update this file accordingly.

#!/bin/bash

set -x

REPO=${REPO:-"rancher"}

WINS_AGENT_UPGRADE_TAG=$(grep "ENV CATTLE_WINS_AGENT_VERSION" package/Dockerfile | awk '{ print $NF }')
WINS_AGENT_UPGRADE_IMAGE=${REPO}/wins:${WINS_AGENT_UPGRADE_TAG}

SYSTEM_AGENT_UPGRADE_TAG=$(grep "ENV CATTLE_SYSTEM_AGENT_VERSION" package/Dockerfile | awk '{ print $NF }')-suc
SYSTEM_AGENT_UPGRADE_IMAGE=${REPO}/system-agent:${SYSTEM_AGENT_UPGRADE_TAG}

# Query KDM data for RKE2 released versions where server args are defined.
RKE2_RELEASE_VERSIONS=$(jq -r '[.rke2.releases[] | select(.serverArgs) | .version] | join(" ")' bin/data.json)
# Convert versions with build metadata into valid image tags (replace + for -) and construct an array of tags.
RKE2_RELEASE_TAGS=( $(echo $RKE2_RELEASE_VERSIONS | tr + -) )
# Prefix image repo and name to tags.
SYSTEM_AGENT_INSTALLER_RKE2_IMAGES=( "${RKE2_RELEASE_TAGS[@]/#/${REPO}/system-agent-installer-rke2:}" )

# Query KDM data for K3S released versions where server args are defined.
K3S_RELEASE_VERSIONS=$(jq -r '[.k3s.releases[] | select(.serverArgs) | .version] | join(" ")' bin/data.json)
# Convert versions with build metadata into valid image tags (replace + for -) and construct an array of tags.
K3S_RELEASE_TAGS=( $(echo $K3S_RELEASE_VERSIONS | tr + -) )
# Prefix image repo and name to tags.
SYSTEM_AGENT_INSTALLER_K3S_IMAGES=( "${K3S_RELEASE_TAGS[@]/#/${REPO}/system-agent-installer-k3s:}" )

