#!/usr/bin/env nix-shell
#!nix-shell -i bash -p bash curl coreutils jq nix

set -euo pipefail

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

linux_version=$(
  curl -sSfL https://www.snipaste.com/linux_version \
    | jq -r '.subject' \
    | sed 's/^v//'
)
mac_version=$(
  curl -sSfL https://www.snipaste.com/mac_version \
    | jq -r '.subject' \
    | sed 's/^v//'
)

linux_url="https://download.snipaste.com/archives/Snipaste-${linux_version}-x86_64.AppImage"
mac_url="https://download.snipaste.com/archives/Snipaste-${mac_version}.dmg"

linux_hash=$(nix-prefetch-url "$linux_url")
mac_hash=$(nix-prefetch-url "$mac_url")

linux_hash=$(nix --extra-experimental-features nix-command hash convert --to sri --hash-algo sha256 "$linux_hash")
mac_hash=$(nix --extra-experimental-features nix-command hash convert --to sri --hash-algo sha256 "$mac_hash")

cat >sources.nix <<EOF
# Generated by ./update.sh - do not update manually!
# Last updated: $(date +%F)
{ fetchurl }:
{
  aarch64-darwin = {
    version = "$mac_version";
    src = fetchurl {
      url = "$mac_url";
      hash = "$mac_hash";
    };
  };
  x86_64-linux = {
    version = "$linux_version";
    src = fetchurl {
      url = "$linux_url";
      hash = "$linux_hash";
    };
  };
}
EOF
