#!/usr/bin/make -f

## needs "mockery":
export DH_GOLANG_GO_GENERATE := 1

export DH_GOLANG_INSTALL_EXTRA := \
    command/connect/ca/set/test-fixtures \
    test

export DH_GOLANG_EXCLUDES := test/integration

%:
	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 . -type f -name '*.pb.go' -delete -printf 'removed %p\n'            ## delete generated .pb.go files
	-find vendor -type d -empty -delete -print
	$(RM) -r test/integration

override_dh_auto_configure:
	dh_auto_configure
	
	mkdir -v -p _build/src/github.com/keybase/
	ln -sv /usr/share/gocode/src/golang.org/x/crypto  _build/src/github.com/keybase/go-crypto
	
	mkdir -v -p _build/src/github.com/SermoDigital/
	ln -sv /usr/share/gocode/src/gopkg.in/square/go-jose.v1  _build/src/github.com/SermoDigital/jose

LIBSELF:=github.com/hashicorp/consul
override_dh_auto_build:
#	## https://salsa.debian.org/onlyjob/notes/wikis/golang
#	## "go generate" in "vendor":
#	DH_GOLANG_EXCLUDES="vendor/github.com/hashicorp/go-discover vendor/github.com/hashicorp/vault vendor/github.com/jefferai/jsonx" \
#	DH_GOPKG="$(LIBSELF)/vendor" \
#	DH_GOLANG_GO_GENERATE=1 \
#	dh_auto_build -v
#	
	export GOPATH=$(CURDIR)/_build \
        && $(MAKE) -C _build/src/$(LIBSELF) --makefile=$(CURDIR)/GNUmakefile proto
	
	dh_auto_build -v

## 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:
	PATH="$(CURDIR)/_build/bin:$${PATH}" \
        DH_GOLANG_EXCLUDES="${DH_GOLANG_EXCLUDES} api agent/cache agent/checks agent/connect agent/consul command/tls" \
        dh_auto_test -v --max-parallel=4 -- -short -failfast -timeout 5m

override_dh_install:
	## Do not install "github.com/hashicorp/consul/vendor" to -dev package.
	dh_install  -X/src/github.com/hashicorp/consul/vendor  -Xcertgen

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

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