#!/usr/bin/make -f

export DH_VERBOSE=1
export DH_GOPKG := github.com/google/mtail

#export DH_GOLANG_EXCLUDES := $(DH_GOPKG)/test/acceptance
export DH_GOLANG_INSTALL_EXTRA := examples/ testdata/ vm/parser.y

BUILDDIR := $(CURDIR)/build

DEBVERS    ?= $(shell dpkg-parsechangelog -SVersion)
VERSION    ?= $(shell echo '$(DEBVERS)' | sed 's/^[[:digit:]]*://; s/[-].*//')
DEBPKGNAME ?= $(shell dpkg-parsechangelog -SSource)
DEBDATE    ?= $(shell dpkg-parsechangelog -SDate)

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

override_dh_auto_build:
	# Build parser.go
	cd $(BUILDDIR)/src/$(DH_GOPKG)/vm && go generate
	dh_auto_build

override_dh_auto_test:
	GOPATH=$(BUILDDIR) go test -v -timeout 60s $(DH_GOPKG)
ifeq ($(DEB_HOST_ARCH_CPU),amd64)
	GOPATH=$(BUILDDIR) go test -v -timeout 5m -race $(DH_GOPKG)
endif

override_dh_auto_install:

gen-orig-tgz:
	if [ ! -f ../$(DEBPKGNAME)_$(VERSION).orig.tar.gz ] ; then \
	    git archive --format=tar.gz --prefix=$(DEBPKGNAME)-$(VERSION)/ \
                upstream/$(subst ~,_,$(VERSION)) > \
                ../$(DEBPKGNAME)_$(VERSION).orig.tar.gz ; \
	fi
