#!/bin/sh
set -e
set -u

case ${1:-} in
  amd64|arm64) ARCH=$1; talos_version="";;
  *) ARCH=${2:-"amd64"}; talos_version=${1:-""};; #this supports previously available script call syntax
esac
echo "ARCH=$ARCH"

PROFILES="initramfs kernel iso installer nocloud metal"
FIRMWARES="amd-ucode amdgpu-firmware bnx2-bnx2x i915-ucode intel-ice-firmware intel-ucode qlogic-firmware"
EXTENSIONS="drbd zfs"

mkdir -p images/talos/profiles

printf "fetching talos version: "
talos_version=${talos_version:-$(skopeo --override-os=linux --override-arch=$ARCH list-tags docker://ghcr.io/siderolabs/imager | jq -r '.Tags[]' | grep '^v[0-9]\+.[0-9]\+.[0-9]\+$' | sort -V | tail -n 1)}
echo "$talos_version"

export "TALOS_VERSION=$talos_version"

for firmware in $FIRMWARES; do
  printf "fetching %s version: " "$firmware"
  firmware_var=$(echo "$firmware" | tr '[:lower:]' '[:upper:]' | tr - _)_VERSION
  version=$(skopeo --override-os=linux --override-arch=$ARCH list-tags docker://ghcr.io/siderolabs/$firmware | jq -r '.Tags[]|select(length == 8)|select(startswith("20"))' | sort -V | tail -n 1)
  echo "$version"
  export "$firmware_var=$version"
done

for extension in $EXTENSIONS; do
  printf "fetching %s version: " "$extension"
  extension_var=$(echo "$extension" | tr '[:lower:]' '[:upper:]' | tr - _)_VERSION
  version=$(skopeo --override-os=linux --override-arch=$ARCH list-tags docker://ghcr.io/siderolabs/$extension | jq -r '.Tags[]' | grep "\-${talos_version}$" | sort -V | tail -n1)
  echo "$version"
  export "$extension_var=$version"
done

for profile in $PROFILES; do
  echo "writing profile images/talos/profiles/${profile}-${ARCH}.yaml"
  case "$profile" in
    initramfs|kernel|iso)
      image_options="{}"
      out_format="raw"
      platform="metal"
      kind="$profile"
      ;;
    installer)
      image_options="{}"
      out_format="raw"
      platform="metal"
      kind="installer"
      ;;
    metal)
      image_options="{ diskSize: 1306525696, diskFormat: raw }"
      out_format=".xz"
      platform="metal"
      kind="image"
      ;;
    nocloud)
      image_options="{ diskSize: 1306525696, diskFormat: raw }"
      out_format=".xz"
      platform="nocloud"
      kind="image"
      ;;
    *)
      echo "Unknown profile: $profile" >&2
      exit 1
      ;;
  esac

  cat > images/talos/profiles/${profile}-${ARCH}.yaml <<EOT
# this file generated by hack/gen-profiles.sh
# do not edit it
arch: ${ARCH}
platform: ${platform}
secureboot: false
version: ${TALOS_VERSION}
input:
  kernel:
    path: /usr/install/${ARCH}/vmlinuz
  initramfs:
    path: /usr/install/${ARCH}/initramfs.xz
  baseInstaller:
    imageRef: "ghcr.io/siderolabs/installer:v1.10.3"
  systemExtensions:
    - imageRef: ghcr.io/siderolabs/amd-ucode:${AMD_UCODE_VERSION}
    - imageRef: ghcr.io/siderolabs/bnx2-bnx2x:${BNX2_BNX2X_VERSION}
    - imageRef: ghcr.io/siderolabs/intel-ice-firmware:${INTEL_ICE_FIRMWARE_VERSION}
    - imageRef: ghcr.io/siderolabs/intel-ucode:${INTEL_UCODE_VERSION}
    - imageRef: ghcr.io/siderolabs/qlogic-firmware:${QLOGIC_FIRMWARE_VERSION}
    - imageRef: ghcr.io/siderolabs/drbd:${DRBD_VERSION}
    - imageRef: ghcr.io/siderolabs/zfs:${ZFS_VERSION}
output:
  kind: ${kind}
  imageOptions: ${image_options}
  outFormat: ${out_format}
EOT
done
