#!/usr/bin/env bash
set -eu

version=0.7.21
arch="$(uname -m)"
tarball="uv-$arch-unknown-linux-gnu.tar.gz"
declare -A sha256=(
    [aarch64]=1dae18211605b9d00767d913da5108aea50200a88372bf8a2e1f56abdbe509f0
    [x86_64]=ca3e8898adfce5fcc891d393a079013fa4bd0d9636cef11aded8a7485bcba312
)

check_version() {
    out="$(uv --version)" && [ "$out" = "uv $version" ]
}

if ! check_version 2>/dev/null; then
    set -x
    tmpdir="$(mktemp -d)"
    trap 'rm -r "$tmpdir"' EXIT
    cd "$tmpdir"
    curl -fLO --retry 3 "https://github.com/astral-sh/uv/releases/download/$version/$tarball"
    sha256sum -c <<<"${sha256[$arch]} $tarball"
    tar -xzf "$tarball" --no-same-owner --strip-components=1 -C /usr/local/bin "uv-$arch-unknown-linux-gnu"/{uv,uvx}
    check_version
fi
