url=$1

hash=$(echo "$url" | grep -oP '/QQNT/\K[^/]+')
linux_version=$(echo "$url" | grep -oP 'linuxqq_\K[^_]+')

linux_x86_64_url="https://dldir1v6.qq.com/qqfile/qq/QQNT/${hash}/linuxqq_${linux_version}_amd64.deb"
linux_aarch64_url="https://dldir1v6.qq.com/qqfile/qq/QQNT/${hash}/linuxqq_${linux_version}_arm64.deb"

linux_x86_64_hash=$(nix-prefetch-url $linux_x86_64_url)
linux_aarch64_hash=$(nix-prefetch-url $linux_aarch64_url)

# use friendlier hashes
linux_x86_64_hash=$(nix --extra-experimental-features nix-command hash convert --to sri --hash-algo sha256 "$linux_x86_64_hash")
linux_aarch64_hash=$(nix --extra-experimental-features nix-command hash convert --to sri --hash-algo sha256 "$linux_aarch64_hash")

cat >sources.nix <<EOF
# Generated by ./update.sh - do not update manually!
# Last updated: $(date +%F)
{ fetchurl }:
let
  any-darwin = {
    version = "$darwin_version";
    src = fetchurl {
      url = "$darwin_url";
      hash = "$darwin_hash";
    };
  };
in
{
  aarch64-darwin = any-darwin;
  x86_64-darwin = any-darwin;
  aarch64-linux = {
    version = "$linux_version";
    src = fetchurl {
      url = "$linux_aarch64_url";
      hash = "$linux_aarch64_hash";
    };
  };
  x86_64-linux = {
    version = "$linux_version";
    src = fetchurl {
      url = "$linux_x86_64_url";
      hash = "$linux_x86_64_hash";
    };
  };
}
EOF
