#!/bin/bash
set -e

# This creates go bootstrap tarballs for the version of go currently
# installed on your system.
# It should be run as part of bumping dev-lang/go when a newer version
# of go is required for bootstrapping.
# Make sure the version of go required for bootstrapping is installed
# then run this script.
# The script will output the location where the bootstrap tarballs are
# stored.
# Next, update the GO_BV variable in the new version of the dev-lang/go
# ebuild to the version in the bootstrap tarballs file name.

go_tuples=(
	darwin-amd64
	darwin-arm64
	linux-386
	linux-amd64
	linux-arm
	linux-arm64
	linux-loong64
	linux-mips
	linux-mipsle
	linux-mips64
	linux-mips64le
	linux-ppc64
	linux-ppc64le
	linux-riscv64
	linux-s390x
	solaris-amd64
)

go_tag=$(go env GOVERSION)
go_version=${go_tag#go}
go_repo="https://github.com/golang/go"
build_path=$(mktemp -d /tmp/go-bootstraps-XXXXXX)
pushd "${build_path}" > /dev/null
git clone --depth 1 --quiet --branch "${go_tag}" "${go_repo}" "${build_path}/go"
for tuple in "${go_tuples[@]}"; do
	printf "Building go version %s bootstrap tarball for %s\n" "${go_version}" "${tuple}"
	pushd go/src > /dev/null
	GO386=softfloat GOOS=${tuple%%-*} GOARCH=${tuple##*-} ./bootstrap.bash
	git clean -d -f -x
	popd > /dev/null
	rm -fr "go-${tuple}-bootstrap"
	mv "go-${tuple}-bootstrap.tbz" "go-bootstrap-${go_version}-${tuple}.tbz"
done
rm -fr go
popd /dev/null
final_path="${HOME}/go-bootstrap-${go_version}"
mv "${build_path}" "${final_path}"
printf "The bootstrap tarballs are stored in %s\n" "${final_path}"