#!/usr/bin/env bash
# Run a command with our local yarn install, rather than any globally installed
# instance.

set -euo pipefail

if [[ "${CI:-}" == "true" ]]; then
    set -x
fi

(
    if [[ "${CI:-}" == "true" ]]; then
        set -x
    fi

    repo_root="${BASH_SOURCE%/*}/../"
    cd "$repo_root"

    # shellcheck source=lib/download.sh
    . tool/lib/download.sh

    ./tool/node --version >/dev/null # Ensure node is unpacked and ready

    cachedir="$HOME/.cache/tailscale-yarn"

    read -r want_rev < "./tool/yarn.rev"

    # Hash is the sha256sum of the release tarball at
    # https://github.com/yarnpkg/yarn/releases/download/v${want_rev}/yarn-v${want_rev}.tar.gz.
    # The tarball is architecture-independent. Update when bumping yarn.rev.
    want_shas=(
        all=732620bac8b1690d507274f025f3c6cfdc3627a84d9642e38a07452cc00e0f2e
    )

    got_rev=""
    if [[ -x "${cachedir}/bin/yarn" ]]; then
        got_rev=$(PATH="$HOME/.cache/tailscale-node/bin:$PATH" "${cachedir}/bin/yarn" --version)
    fi

    if [[ "$want_rev" != "$got_rev" ]]; then
        want_hash=$(select_sha256 "all" "${want_shas[@]}")
        download_tool --args=--strip-components=1 --verify-sha256="$want_hash" "https://github.com/yarnpkg/yarn/releases/download/v${want_rev}/yarn-v${want_rev}.tar.gz" "$cachedir"
    fi
)

# Deliberately not using cachedir here, to keep the environment
# completely pristine for execution of yarn.
export PATH="$HOME/.cache/tailscale-node/bin:$HOME/.cache/tailscale-yarn/bin:$PATH"
exec "$HOME/.cache/tailscale-yarn/bin/yarn" "$@"
