#!/usr/bin/make -f

UPSTREAM_GIT := https://github.com/purpleidea/mgmt
include /usr/share/openstack-pkg-tools/pkgos.make

export DH_OPTIONS
export DH_GOPKG := github.com/purpleidea/mgmt
export DH_GOLANG_INSTALL_ALL := 1
export DH_GOLANG_EXCLUDES := examples bindata test
export GOPATH = ${CURDIR}/_build:/usr/share/gocode
export GO111MODULE := off
export GOCACHE=/tmp/gocache
export BUILDDIR=_build/src/github.com/purpleidea/mgmt
export HOME=/tmp
export PROGRAM=mgmt
#unexport GOROOT

export SVERSION=$(shell dpkg-parsechangelog -SVersion | sed -e 's/^[[:digit:]]*://' -e 's/[-].*//' -e 's/~git.*//' -e 's/~/.0/' -e 's/+dfsg1//' -e 's/+ds1//' | head -n 1)

override_dh_clean:
	dh_clean
	rm -f lang/interpolate/parse.generated.go lang/parser/lexer.nn.go lang/parser/y.go lang/parser/y.output
	rm -rf .config

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

override_dh_auto_build:
	#@echo "Generating kind_stringer..."
	#cd lang/types ; HOME=$(CURDIR) RES_OPTIONS=attempts:0 stringer -type=Kind -trimprefix=Kind -output=kind_stringer.go

	cd $(BUILDDIR) && \
	GOTAGS=noconsul $(MAKE) build PROGRAM="$(PROGRAM)" && \
	$(MAKE) $(PROGRAM) PROGRAM=$(PROGRAM)

	#$(MAKE) -C lang build
	#$(MAKE) -C lang interpolate/parse.generated.go

	#@echo "Generating funcs..."
	#RES_OPTIONS=attempts:0 go build `find lang/funcs/funcgen/ -maxdepth 1 -type f -name '*.go' -not -name '*_test.go'`
	# -templates=lang/funcs/funcgen/templates/generated_funcs.go.tpl

#	dh_auto_build -- -tags noconsul

override_dh_auto_test:
	@echo "Tests are disabled for now"

override_dh_auto_install:
	dh_auto_install -- --no-source

override_dh_golang:
# Workaround a dh-golang bug. dh_golang would invoke `go list` and in dh_golang, there is no
# Provision yet to actually pass any tags from d/rules. It also does not honour DH_GOLANG_EXCLUDES for
# listing go mods (via go list).
	mv $(BUILDDIR)/engine/resources/consul_kv.go $(BUILDDIR)/engine/resources/consul_kv.go_
	dh_golang -O--buildsystem=golang -O--builddirectory=_build -- -tags noconsul
	mv $(BUILDDIR)/engine/resources/consul_kv.go_ $(BUILDDIR)/engine/resources/consul_kv.go
