set -eu

BIN_DIR="$(git rev-parse --show-toplevel)/bin"
export PATH="$BIN_DIR:$PATH"

_u_bzl=https://github.com/bazelbuild/bazelisk/releases/download/v1.12.0/bazelisk-

if [[ "${PRINT_TOOL_HASHES:-no}" = "yes" ]]; then
    for os in linux darwin; do
        for arch in amd64 arm64; do
            hash_bzl=$(direnv fetchurl "${_u_bzl}$os-$arch")
            echo -e "bzl:  $os-$arch\t$hash_bzl"
        done
    done
fi

# to fetch the hashes, run:
# $ PRINT_TOOL_HASHES=yes bash .envrc
case "$(uname | tr A-Z a-z)-$(uname -m)" in
    linux-x86_64)
        bzl=$(direnv fetchurl "${_u_bzl}linux-amd64" sha256-awvLLqFbyhb/+r5v2nWANEA3U1TAhUgP42HSy/MlAds=)
        ;;
    linux-aarch64)
        bzl=$(direnv fetchurl "${_u_bzl}linux-arm64" sha256-KdhhykjfJKPo3sV/sAUIumZKMZIQR7JobDjPmiDUY58=)
        ;;
    darwin-x86_64)
        bzl=$(direnv fetchurl "${_u_bzl}darwin-amd64" sha256-cM9/50gI0WQY03H+uMzU58RCFdsD0sT/x1t2e3ZUCfs=)
        ;;
    darwin-arm64)
        bzl=$(direnv fetchurl "${_u_bzl}darwin-arm64" sha256-NFu4uQDWue90I06enkE67Tj7Ke8lXkrhisYb9KYQLYQ=)
        ;;
    *)
        >&2 echo "unsupported architecture tuple $(uname | tr A-Z a-z)-$(uname -m)"
        exit 1;;
esac

ln -sf "${bzl}" "$BIN_DIR/bazel"
