#!/usr/bin/make -f
#DH_VERBOSE=1
export DH_OPTIONS
export DH_ALWAYS_EXCLUDE=.gitignore

export DH_GOPKG := github.com/danos/yang
export DH_GOLANG_INSTALL_ALL := 1
export DH_GOLANG_GO_GENERATE := 1

GOBUILDDIR := _build

# Uncomment to enable race detection
# This should only be done for local test builds; tests run very slow
# See https://golang.org/doc/articles/race_detector.html#Runtime_Overheads
#GORACE=-race

# If the cover tool is present, use it in tests
GOCOVER=$(if $(shell go doc cmd/cover >/dev/null 2>&1 && echo true),-cover,)

%:
	dh $@ --buildsystem=golang --with=golang --builddirectory=$(GOBUILDDIR)

override_dh_strip:

override_dh_auto_build: vet
	dh_auto_build -- $(GORACE)

override_dh_auto_install:
	dh_auto_install --destdir=debian/tmp

override_dh_auto_test:
	dh_auto_test -- $(GORACE) $(GOCOVER)

# We do not vet vendor code.
# Unlike for build, where it seems dh_golang uses the DH_GO_GENERATE flag,
# it appears that we need to run go generate explicitly here now (in Go1.11
# and/or previous dh_golang versions we didn't).
# Additionally, we need to filter the list from dirname or we end up with
# having to run go generate in 2 different locations and it still complains
# about a missing definition in an export_for_test.go file.
vet:
	if go doc cmd/vet >/dev/null 2>&1; then \
		cd $(CURDIR)/$(GOBUILDDIR)/src/github.com/danos/yang/xpath/grammars/expr; \
		go generate;cd -; \
		cd $(CURDIR)/$(GOBUILDDIR)/src/github.com/danos/yang/xpath/grammars/leafref; \
		go generate;cd -; \
		cd $(CURDIR)/$(GOBUILDDIR)/src/github.com/danos/yang/xpath/grammars/path_eval; \
		go generate;cd -; \
		GOPATH=$(CURDIR)/$(GOBUILDDIR) go vet -composites=false $$(find . -type f -name \*.go | \
		xargs dirname | sort -u | grep $(GOBUILDDIR) | grep -v "/vendor/"); \
	fi
