#!/usr/bin/make -f

include /usr/share/dpkg/pkg-info.mk

## avoid re-running mockery during package build, avoids FTBFS #1008396
export DH_GOLANG_GO_GENERATE := 0

export DH_GOLANG_INSTALL_EXTRA := \
    command/connect/ca/set/test-fixtures \
    build-support/functions \
    build-support/scripts/functions.sh \
    build-support/scripts/proto-gen.sh \
    go.mod \
    test

export DH_GOLANG_EXCLUDES := \
	test/integration \
	vendor/github.com/hashicorp/go-discover \
	vendor/github.com/hashicorp/vault \
	vendor/github.com/jefferai/jsonx \
	vendor/github.com/linode/linodego

# this needs to be addressed upstream
export GODEBUG=x509sha1=1
export HOME=$(CURDIR)/debian/tmp

ifneq (,$(filter $(DEB_BUILD_ARCH), mipsel mips64el))
    MINUS:=-
endif

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

override_dh_clean:
	dh_clean
	## Remove Files-Excluded (when built from checkout or non-DFSG tarball):
	$(RM) -rv `perl -0nE 'say $$1 if m{^Files\-Excluded\:\s*(.*?)(?:\n\n|Files:|Comment:)}sm;' debian/copyright`
	-find vendor -type d -empty -delete -print
	$(RM) -r test/integration

export DH_GOLANG_TEST_EXCLUDES := \
	${DH_GOLANG_EXCLUDES} \
	api \
	agent \
	command/agent \
	command/connect/envoy \
	command/exec \
	command/debug \
	command/tls \
	command/flags \
	command/monitor

## Quoting upstream as per ".github/CONTRIBUTING.md":
## > The test suite may fail if over-parallelized, so if you are seeing
## > stochastic failures try `GOTEST_FLAGS="-p 2 -parallel 2".
## Note that "--max-parallel=2" translates to "-p 2" which is not the same as "-parallel 2".
override_dh_auto_test:
	$(MINUS)PATH="$(CURDIR)/_build/bin:$${PATH}" \
        DH_GOLANG_EXCLUDES="$(DH_GOLANG_TEST_EXCLUDES)" \
        dh_auto_test -v --max-parallel=2 -- -short -failfast -timeout 8m

override_dh_install:
	dh_install  -Xcertgen

override_dh_installsystemd:
	dh_installsystemd --no-enable --no-start

override_dh_installinit:
	dh_installinit --no-enable --no-start
