#!/bin/sh
set -eu

GO_VERSION="1.27.0"
GO_TARBALL="go${GO_VERSION}.linux-amd64.tar.gz"
GO_URL="https://go.dev/dl/${GO_TARBALL}"
GO_SHA256="675c26c449cbb18fc24b74650de1eabbae6e16f64326fd85a283fb3b58280685"

if [ -f "/tmp/${GO_TARBALL}" ]; then
	echo "File already downloaded: /tmp/${GO_TARBALL}"
	printf "Remove existing file and download again? [y/N] "
	read -r answer
	case "${answer}" in
		y|Y|yes|YES)
			rm -f "/tmp/${GO_TARBALL}"
			curl -fL -o "/tmp/${GO_TARBALL}" "${GO_URL}"
			;;
		*)
			echo "Keeping existing file."
			;;
	esac
else
	curl -fL -o "/tmp/${GO_TARBALL}" "${GO_URL}"
fi

echo "${GO_SHA256}  /tmp/${GO_TARBALL}" | sha256sum -c -

rm -rf /opt/go

tar -C /opt -xzf "/tmp/${GO_TARBALL}"
rm -f "/tmp/${GO_TARBALL}"

ln -sf /opt/go/bin/go /usr/bin/go
