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

# a helper to provide ".exe" when it's appropriate
binary_extension() {
	if [ "$(go env GOOS)" = 'windows' ]; then
		echo -n '.exe'
	fi
}

BINARY_EXTENSION="$(binary_extension)"
BINARY_FULLNAME="$BINARY_NAME$BINARY_EXTENSION"

source "${MAKEDIR}/.go-autogen"

(
	export GOGC=${DOCKER_BUILD_GOGC:-1000}

	if [ "$(go env GOOS)/$(go env GOARCH)" != "$(go env GOHOSTOS)/$(go env GOHOSTARCH)" ]; then
		# must be cross-compiling!
		if [ "$(go env GOOS)/$(go env GOARCH)" = "linux/arm" ]; then
			# specify name of the target ARM architecture
			case "$(go env GOARM)" in
				5)
					export CGO_CFLAGS="-march=armv5t"
					export CGO_CXXFLAGS="-march=armv5t"
					;;
				6)
					export CGO_CFLAGS="-march=armv6"
					export CGO_CXXFLAGS="-march=armv6"
					;;
				7)
					export CGO_CFLAGS="-march=armv7-a"
					export CGO_CXXFLAGS="-march=armv7-a"
					;;
			esac
		fi
	fi

	if ! [ "$DOCKER_STATIC" = "1" ]; then
		# -buildmode=pie not supported when -race is enabled
		if [[ " $BUILDFLAGS " != *" -race "* ]]; then
			case "$(go env GOOS)/$(go env GOARCH)" in
				linux/mips* | linux/ppc64)
					# -buildmode=pie is not supported on Linux mips*, ppc64be
					# https://github.com/golang/go/blob/go1.24.3/src/internal/platform/supported.go#L188-L200
					;;
				*)
					BUILDFLAGS+=("-buildmode=pie")
					;;
			esac
		fi
	fi

	# XXX: Disable netgo on Windows and use Window's system resolver instead.
	#
	# go1.19 and newer added support for netgo on Windows (https://go.dev/doc/go1.19#net),
	# which won't ask Windows for DNS results, and hence may be ignoring
	# custom "C:\Windows\System32\drivers\etc\hosts".
	# See https://github.com/moby/moby/issues/45251#issuecomment-1561001817
	# https://github.com/moby/moby/issues/45251, and
	# https://go-review.googlesource.com/c/go/+/467335
	if [ "$(go env GOOS)" = "windows" ]; then
		BUILDFLAGS=("${BUILDFLAGS[@]/netgo/}")
	fi

	# only necessary for non-sandboxed invocation where TARGETPLATFORM is empty
	PLATFORM_NAME=$TARGETPLATFORM
	if [ -z "$PLATFORM_NAME" ]; then
		PLATFORM_NAME="$(go env GOOS)/$(go env GOARCH)"
		if [ -n "$(go env GOARM)" ]; then
			PLATFORM_NAME+="/v$(go env GOARM)"
		elif [ -n "$(go env GOAMD64)" ] && [ "$(go env GOAMD64)" != "v1" ]; then
			PLATFORM_NAME+="/$(go env GOAMD64)"
		fi
	fi

	if [ -n "${DOCKER_DEBUG}" ]; then
		GCFLAGS="all=-N -l"
	fi

	if [ "$(go env GOARCH)" = "arm" ] && [ "$(go env GOARM)" = "5" ]; then
		# cross-compiling for arm/v5 fails on go1.22; a fix is included for this
		# in go1.23 (https://github.com/golang/go/issues/65290), but for go1.22
		# we can set the correct option manually.
		CGO_CFLAGS+=" -Wno-atomic-alignment"
		export CGO_CFLAGS

		# Make sure libatomic is included on arm/v5, because clang does not auto-link it.
		# see https://github.com/moby/moby/pull/46982#issuecomment-2206992611
		export CGO_LDFLAGS="-latomic"
	fi

	echo "Building $([ "$DOCKER_STATIC" = "1" ] && echo "static" || echo "dynamic") $DEST/$BINARY_FULLNAME ($PLATFORM_NAME)..."
	if [ -n "$DOCKER_DEBUG" ]; then
		set -x
	fi
	./hack/with-go-mod.sh go build -mod=vendor -modfile=vendor.mod -o "$DEST/$BINARY_FULLNAME" "${BUILDFLAGS[@]}" -ldflags "$LDFLAGS $LDFLAGS_STATIC $DOCKER_LDFLAGS" -gcflags="${GCFLAGS}" "$GO_PACKAGE"
)

echo "Created binary: $DEST/$BINARY_FULLNAME"
