#!/usr/bin/env nix-shell
#!nix-shell -I nixpkgs=./. -i bash -p curl jq nix-prefetch common-updater-scripts nix coreutils
# shellcheck shell=bash
set -euo pipefail

VERSION=$(curl -s https://api.github.com/repos/facebook/buck2/releases \
  | jq -r 'sort_by(.created_at) | reverse |
           (map
             (select ((.prerelease == true) and (.name != "latest"))) |
             first
           ) | .name')
echo "Latest buck2 prerelease: $VERSION"

ARCHS=(
    "x86_64-linux:x86_64-unknown-linux-musl"
    "x86_64-darwin:x86_64-apple-darwin"
    "aarch64-linux:aarch64-unknown-linux-musl"
    "aarch64-darwin:aarch64-apple-darwin"
)

NFILE=pkgs/development/tools/build-managers/buck2/default.nix
HFILE=pkgs/development/tools/build-managers/buck2/hashes.json
rm -f "$HFILE" && touch "$HFILE"

printf "{ \"_comment\": \"@generated by pkgs/development/tools/build-managers/buck2/update.sh\"\n" >> "$HFILE"
for arch in "${ARCHS[@]}"; do
    IFS=: read -r arch_name arch_target <<< "$arch"
    sha256hash="$(nix-prefetch-url --type sha256 "https://github.com/facebook/buck2/releases/download/${VERSION}/buck2-${arch_target}.zst")"
    srihash="$(nix hash to-sri --type sha256 "$sha256hash")"
    echo ", \"$arch_name\": \"$srihash\"" >> "$HFILE"
done
echo "}" >> "$HFILE"

sed -i \
  's/buck2-version\s*=\s*".*";/buck2-version = "'"$VERSION"'";/' \
  "$NFILE"

echo "Done; wrote $HFILE and updated version"
