#!/usr/bin/make -f
#DH_VERBOSE=1
export DH_OPTIONS
export DH_GOPKG := github.com/danos/opd
export DH_GOLANG_EXCLUDES := opstress
export DH_GOLANG_INSTALL_ALL := 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,)

PACKAGE=vyatta-opd
PKGDIR=$(CURDIR)/debian/vyatta-opd
VYATTADIR=$(PKGDIR)/opt/vyatta
BINDIR=$(VYATTADIR)/bin
SBINDIR=$(VYATTADIR)/sbin

DEVPKGDIR=$(CURDIR)/debian/vyatta-opd-dev
DEVDOCDIR=$(CURDIR)/doc
DEVDOCPKGDIR=vyatta/pkg/
DEVPKGDOC=$(DEVDOCDIR)/pkg
DEVDOCPKGS=cmd/cmdclient cmd/cmdrunner rpc tmpl tmpl/parse tmpl/tree

SYSTEMD_DEV_VER := $(shell dpkg-query -W -f='$${Version}' golang-github-coreos-go-systemd-dev)
SYSTEMD_DEV_NEW := $(shell dpkg --compare-versions $(SYSTEMD_DEV_VER) ge 17 && echo "1" || echo "0")

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

override_dh_auto_configure:
	if [ "$(SYSTEMD_DEV_NEW)" = "1" ]; then \
		sed -i "s/activation.Listeners(true)/activation.Listeners()/g" cmd/opd/opd.go; \
	fi
	dh_auto_configure

override_dh_auto_build: vet
	make -C src/client
	dh_auto_build -- $(GORACE)

# We do not vet vendor code
vet:
	if go doc cmd/vet >/dev/null 2>&1; then \
		go tool vet $$(find . -type f -name \*.go | xargs dirname | \
			sort -u | grep -v "/vendor/"); \
	fi

# TODO: docs target needs an overhaul
docs:
	mkdir -p $(DEVPKGDOC)
	for i in $(DEVDOCPKGS); do \
		mkdir -p $(DEVPKGDOC)/$$i; \
		godoc $(DEVDOCPKGDIR)/$$i > $(DEVPKGDOC)/$$i/doc.txt; \
	done
	godoc cmd/opd > $(DOCDIR)/opd.txt
	godoc cmd/opc > $(DOCDIR)/opc.txt

override_dh_strip:

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