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

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,vci,yang --builddirectory=$(GOBUILDDIR)

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

override_dh_auto_build: vet
	dh_auto_build -- $(GORACE)

vet:
	if go doc cmd/vet >/dev/null 2>&1; then \
		GO111MODULE=off GOPATH=$(CURDIR)/$(GOBUILDDIR) go vet \
		$$(find . -type f -name \*.go | xargs dirname | sort -u | \
		sed 's/.\//github.com\/danos\/config\/g'); \
	fi

override_dh_auto_install:
	dh_auto_install --destdir=debian/tmp -- --no-source

docs:
	godoc configd/cmd/configd > $(DOCDIR)/configd.txt
#	GOPATH=$(CURDIR)/$(GOBUILDDIR) godepgraph -s -i eng.vyatta.net/audit,eng.vyatta.net/natsort,eng.vyatta.net/vci/conf brocade.com/vyatta/yang/analyse | dot -Tsvg >| yang_analyse.svg

override_dh_strip:

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

override_dh_systemd_enable:
	dh_systemd_enable --name=configd

override_dh_systemd_start:
	dh_systemd_start --name=configd --no-start --no-restart-on-upgrade

override_dh_installinit:
	dh_installinit --name=configd --no-start --no-restart-on-upgrade -- defaults 01 99
