abinfo "Setting up build environment ..."
export GOOS=linux
export GOROOT="$SRCDIR"
export GOBIN="$GOROOT/bin"
export GOPATH="$SRCDIR/../"
export GOROOT_FINAL="/usr/lib/go"
export GOROOT_BOOTSTRAP="/usr/lib/go"

if ab_match_arch amd64; then
    export GOARCH="amd64" GOAMD64=v1
elif ab_match_arch arm64; then
    export GOARCH="arm64"
elif ab_match_arch armv6hf; then
    export GOARCH="arm" GOARM=6
elif ab_match_arch armv7hf; then
    export GOARCH="arm" GOARM=7
elif ab_match_arch loongarch64 || ab_match_arch loongarch64_nosimd; then
    export GOARCH="loong64"
elif ab_match_arch loongson3; then
    export GOARCH="mips64le"
elif ab_match_arch ppc64el; then
    export GOARCH="ppc64le"
elif ab_match_arch riscv64; then
    export GOARCH="riscv64"
fi

abinfo "Building Go toolchain ..."
pushd "$GOROOT"/src
bash make.bash -v
popd
# NOTE: Only amd64, arm64 and ppc64el support `-buildmode=shared`.
if ab_match_arch amd64 || ab_match_arch arm64 || ab_match_arch ppc64el; then
    "$GOROOT"/bin/go install -v -buildmode=shared std
    # Prepare to build `go-runtime`.
    mv -v "$SRCDIR"/pkg/"$GOOS"_"$GOARCH"_dynlink \
        "$SRCDIR"/"$GOOS"_"$GOARCH"_dynlink
fi

abinfo "Installing the Go toolchain ..."
mkdir -pv "$PKGDIR"/usr/lib/go
for i in misc bin pkg src lib; do
    cp -av "$SRCDIR"/$i \
        "$PKGDIR"/usr/lib/go/
done

abinfo "Installing other Go toolchain files ..."
install -Dvm644 "$SRCDIR"/VERSION \
    "$PKGDIR/usr/lib/go/VERSION"
install -Dvm644 "$SRCDIR"/go.env \
    "$PKGDIR/usr/lib/go/go.env"

abinfo "Installing basic Go tools ..."
install -Dvm755 "$SRCDIR"/pkg/tool/"$GOOS"_"$GOARCH"/* \
    "$PKGDIR"/usr/lib/go/pkg/tool/"$GOOS"_"$GOARCH"

abinfo "Linking 'go' and 'gofmt' installed in GOROOT to /usr/bin ..."
mkdir -pv "$PKGDIR"/usr/bin
ln -sfv ../lib/go/bin/go \
    "$PKGDIR"/usr/bin/go-google
ln -sfv ../lib/go/bin/gofmt \
    "$PKGDIR"/usr/bin/gofmt-google

abinfo "Preparing to build extra Go tools ..."
export GOBIN="$GOROOT/tools-bin"
export GOPATH="$SRCDIR/go-tools"
mkdir -pv "$SRCDIR"/go-tools/src/golang.org/x/
cp -rv "$SRCDIR"/../tools \
    "$SRCDIR"/go-tools/src/golang.org/x/tools

abinfo "Building extra Go tools ..."
pushd "$SRCDIR"/go-tools/src/golang.org/x/tools
"$GOROOT"/bin/go install \
    -v \
    -ldflags "-w -s -extldflags '-Wl,-z,relro -Wl,-z,now -specs=/usr/lib/autobuild3/specs/hardened-ld'" \
    "$SRCDIR"/go-tools/src/golang.org/x/tools/cmd/...
popd

abinfo "Installing extra Go tools ..."
pushd "$GOBIN"
for i in *; do
    install -Dvm755 "$GOBIN"/$i \
        "$PKGDIR"/usr/bin/$i
done
popd

abinfo "Renaming extra Go tools with possible name collision with existing packages ..."
for i in bundle stress; do
    mv -v "$PKGDIR"/usr/bin/$i \
        "$PKGDIR"/usr/bin/go-${i}
done

abinfo "Building gopls ..."
pushd "$SRCDIR"/go-tools/src/golang.org/x/tools/gopls
"$GOROOT"/bin/go install \
    -v \
    -ldflags "-w -s -extldflags '-Wl,-z,relro -Wl,-z,now -specs=/usr/lib/autobuild3/specs/hardened-ld'" \
    "$SRCDIR"/go-tools/src/golang.org/x/tools/gopls
popd

abinfo "Installing gopls ..."
install -Dvm755 "$GOBIN"/gopls \
    "$PKGDIR"/usr/bin/

# Adapted from Alpine Linux.
# Remove tests from /usr/lib/go/src to reduce package size,
# these should not be needed at run-time by any program.
find "$PKGDIR"/usr/lib/go/src \
    -type f -name "*_test.go" \
    -delete -print
find "$PKGDIR"/usr/lib/go/src \
    -type d -name "testdata" \
    -exec rm -rv {} \+

# Remove rc (plan 9) and bat scripts (windows) to reduce package
# size further. The bash scripts are actually needed at run-time.
find "$PKGDIR"/usr/lib/go/src \
    -type f -name "*.rc" \
    -delete -print
find "$PKGDIR"/usr/lib/go/src \
    -type f -name "*.bat" \
    -delete -print
