#!/usr/bin/env bash

# installs $(cat ./helm.rev) version of helm as $HOME/.cache/tailscale-helm

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

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

    read -r want_rev < "$(dirname "$0")/helm.rev"

    # Hashes are the sha256sum of the release tarballs published at
    # https://get.helm.sh/helm-v${want_rev}-${OS}-${ARCH}.tar.gz.sha256sum.
    # Update these when bumping helm.rev.
    want_shas=(
        darwin-amd64=e207e009b931162b0383b463c333a2792355200e91dbcf167c97c150e9f5fedb
        darwin-arm64=46596d6d2d9aa545eb74f40684858fac0841df373ca760af1259d3493161d8c9
        linux-amd64=98c363564d00afd0cc3088e8f830f2a0eeb5f28755b3d8c48df89866374a1ed0
        linux-arm64=8c4a0777218b266a7b977394aaf0e9cef30ed2df6e742d683e523d75508d6efe
    )

    got_rev=""
    if [[ -x "${cachedir}/helm" ]]; then
        got_rev=$("${cachedir}/helm" version --short)
        got_rev="${got_rev#v}" # trim the leading 'v'
        got_rev="${got_rev%+*}" # trim the trailing '+" followed by a commit SHA'
    fi

    if [[ "$want_rev" != "$got_rev" ]]; then
        if [[ -n "${IN_NIX_SHELL:-}" ]]; then
            nix_helm="$(which -a helm | grep /nix/store | head -1)"
            nix_helm="${nix_helm%/helm}"
            nix_helm_rev="${nix_helm##*-}"
            if [[ "$nix_helm_rev" != "$want_rev" ]]; then
                echo "Wrong helm version in Nix, got $nix_helm_rev want $want_rev" >&2
                exit 1
            fi
            rm -rf "$cachedir"
            ln -sf "$nix_helm" "$cachedir"
        else
            # works for linux and darwin
            # https://github.com/helm/helm/releases
            OS=$(uname -s | tr A-Z a-z)
            ARCH=$(uname -m)
            if [ "$ARCH" = "x86_64" ]; then
                ARCH="amd64"
            fi
            if [ "$ARCH" = "aarch64" ]; then
                ARCH="arm64"
            fi

            want_hash=$(select_sha256 "${OS}-${ARCH}" "${want_shas[@]}")

            download_tool --args=--strip-components=1 --verify-sha256="$want_hash" "https://get.helm.sh/helm-v${want_rev}-${OS}-${ARCH}.tar.gz" "$cachedir"
        fi
    fi
)

export PATH="$HOME/.cache/tailscale-helm:$PATH"
exec "$HOME/.cache/tailscale-helm/helm" "$@"
