#!/usr/bin/env nix-shell
#!nix-shell -i bash -p curl jq nix common-updater-scripts

set -euo pipefail

gh_curl() { curl ${GITHUB_TOKEN:+-u ":$GITHUB_TOKEN"} -sf "$@"; }

version="${1:-$(gh_curl "https://api.github.com/repos/ROCm/rocm-libraries/releases?per_page=4" \
  | jq -re 'map(.tag_name // .name)
    | map(select(test("^rocm-[0-9]+\\.[0-9]+(\\.[0-9]+)?$")))
    | first | ltrimstr("rocm-")')}"

# main source version
echo "Updating MIOpen to $version" >&2
update-source-version rocmPackages.miopen "$version" --ignore-same-hash

# kdbs.nix from s3 bucket
cd "$(dirname "${BASH_SOURCE[0]}")"
bucket="https://therock-dvc.s3.amazonaws.com/rocm-libraries/files/md5"

# Fetch directory listing; extract name + download_url for .dvc files
dvc_entries=$(gh_curl "https://api.github.com/repos/ROCm/rocm-libraries/contents/projects/miopen/src/kernels?ref=rocm-${version}" \
  | jq -r '.[] | select(.name | endswith(".kdb.bz2.dvc")) | "\(.name)\t\(.download_url)"')

[[ -n "$dvc_entries" ]] || { echo "No .kdb.bz2.dvc files found, upstream likely changed packaging again" >&2; exit 1; }

{
  echo "# Generated by update.sh"
  echo "{"

  while IFS=$'\t' read -r dvc_file raw_url; do
    arch="${dvc_file%.kdb.bz2.dvc}"
    md5=$(curl -sf "$raw_url" | awk '/- md5:/{print $3}')
    url="${bucket}/${md5:0:2}/${md5:2}"
    sri=$(nix --extra-experimental-features nix-command store prefetch-file --json "$url" --name "${arch}.kdb.bz2" | jq -r .hash)
    echo "  $arch: $sri" >&2
    cat <<EOF
  $arch = {
    url = "$url";
    hash = "$sri";
  };
EOF
  done <<< "$dvc_entries"

  echo "}"
} > kdbs.nix

echo "Wrote kdbs.nix" >&2
