#!/usr/bin/make -f

include /usr/share/dpkg/architecture.mk

BUILDFLAGS :=
ifeq (,$(filter $(DEB_BUILD_ARCH), mipsel mips64el riscv64))
BUILDFLAGS += -buildmode=pie
endif

export DH_GOLANG_EXCLUDES := contrib/notify

%:
	dh $@ --builddirectory=_build --buildsystem=golang --with=golang

override_dh_auto_configure:
	# We're building `gotest.tools/gotestum`, and we depend on `gotest.tools`.
	# That's a bit problematic, and here's why:
	# - dh_auto_configure first creates symlinks to build dependencies, hence:
	#     _build/src/gotest.tools -> /usr/share/gocode/src/gotest.tools
	# - then dh_auto_configure creates the directory for the sources we build:
	#     _build/src/gotest.tools/gotestsum
	# - hence overriding the initial symlink to gotest.tools...
	# So instead of one symlink to gotest.tools, we need symlinks to every
	# packages that gotest.tools provide.
	mkdir -pv _build/src/gotest.tools
	for file in /usr/share/gocode/src/gotest.tools/*; do \
	    ln -sv $$file _build/src/gotest.tools/; \
	done
	dh_auto_configure

override_dh_auto_build:
	dh_auto_build -- $(BUILDFLAGS)

override_dh_auto_install:
	dh_auto_install -- --no-source
