#!/usr/bin/make -f

export DH_GOLANG_GO_GENERATE := 1
export DH_GOLANG_EXCLUDES := benchmark

%:
	dh $@

# Re-generate all non-source files.
#
# In theory 'export DH_GOLANG_GO_GENERATE:=1' should suffice, but for
# some reason it does not re-generate the files generated by code
# under _gen/.  So we need to do that part manually.
#
# The complexity below is mostly to confirm that the re-generated
# files are identical to the shipped sources.  Otherwise we could
# introduce a problem if we would use different re-generated source
# code files due to some tool reason (e.g., build dependency changes).
#
# Two files cannot be reproduced still, so we exclude them for now:
# https://github.com/klauspost/reedsolomon/issues/346
execute_before_dh_auto_configure:
	find|grep galois_gen_amd64.go
	sha256sum `rgrep -l 'DO NOT EDIT' . | grep -v -e '/_gen/' -e '/debian/rules' -e 'galois_gen_amd64.s' -e 'galois_gen_switch_nopshufb_arm64.go' | sed -e 's,./,,'` | tee $(CURDIR)/debian/regen-check.SHA256SUMS
	test `cat $(CURDIR)/debian/regen-check.SHA256SUMS | wc -l` = 7
	rm -v `cut -f3 -d" " $(CURDIR)/debian/regen-check.SHA256SUMS`
	env GO111MODULE=off GOPATH=/usr/share/gocode GOCACHE=$(CURDIR)/_build/.gocache go generate -x -v
	env GO111MODULE=off GOPATH=/usr/share/gocode GOCACHE=$(CURDIR)/_build/.gocache go generate -x -v _gen/gen.go
	sha256sum -c $(CURDIR)/debian/regen-check.SHA256SUMS
	rm -v $(CURDIR)/debian/regen-check.SHA256SUMS

override_dh_auto_test:
ifneq (,$(filter $(DEB_HOST_ARCH), armhf armel i386 ppc64el riscv64))
	# Skip some time consuming tests, to avoid FTBFS issue
	# on reproducible-builds.org CI tests.
	dh_auto_test $(DH_BUILD_OPTS) -- -short
else
	dh_auto_test $(DH_BUILD_OPTS) -- -timeout 25m
endif
