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

version=3.12.0
arch="$(uname -m)"

case $arch in
    "x86_64")
        binary="shfmt_v${version}_linux_amd64"
        sha256=d9fbb2a9c33d13f47e7618cf362a914d029d02a6df124064fff04fd688a745ea
        ;;

    "aarch64")
        binary="shfmt_v${version}_linux_arm64"
        sha256=5f3fe3fa6a9f766e6a182ba79a94bef8afedafc57db0b1ad32b0f67fae971ba4
        ;;
esac

check_version() {
    out="$(shfmt --version)" && [ "$out" = "v$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/mvdan/sh/releases/download/v$version/$binary"
    sha256sum -c <<<"$sha256 $binary"
    chmod +x "$binary"
    mv "$binary" /usr/local/bin/shfmt
    check_version
fi
