#!/usr/bin/env nix-shell
#! nix-shell -i bash --pure -p nix

set -euo pipefail

cd "$(readlink -e "$(dirname "${BASH_SOURCE[0]}")")"

version="1.5"

aarch64_darwin_url="https://github.com/GaloisInc/saw-script/releases/download/v${version}/saw-${version}-macos-15-ARM64-with-solvers.tar.gz"
x86_64_darwin_url="https://github.com/GaloisInc/saw-script/releases/download/v${version}/saw-${version}-macos-15-intel-X64-with-solvers.tar.gz"
x86_64_linux_url="https://github.com/GaloisInc/saw-script/releases/download/v${version}/saw-${version}-ubuntu-22.04-X64-with-solvers.tar.gz"

aarch64_darwin_hash=$(nix-prefetch-url "$aarch64_darwin_url")
x86_64_darwin_hash=$(nix-prefetch-url "$x86_64_darwin_url")
x86_64_linux_hash=$(nix-prefetch-url "$x86_64_linux_url")

aarch64_darwin_hash=$(nix --extra-experimental-features nix-command hash convert --to sri --hash-algo sha256 "$aarch64_darwin_hash")
x86_64_darwin_hash=$(nix --extra-experimental-features nix-command hash convert --to sri --hash-algo sha256 "$x86_64_darwin_hash")
x86_64_linux_hash=$(nix --extra-experimental-features nix-command hash convert --to sri --hash-algo sha256 "$x86_64_linux_hash")

sed -i "s/version = \".*\"/version = \"${version}\"/" package.nix

cat >sources.nix <<EOF
# Generated by ./update.sh - do not update manually!
# Last updated: $(date +%F)
{
  aarch64-darwin = {
    url = "$aarch64_darwin_url";
    hash = "$aarch64_darwin_hash";
  };
  x86_64-darwin = {
    url = "$x86_64_darwin_url";
    hash = "$x86_64_darwin_hash";
  };
  x86_64-linux = {
    url = "$x86_64_linux_url";
    hash = "$x86_64_linux_hash";
  };
}
EOF
