#!/usr/bin/make -f
export DH_OPTIONS
export DH_GOPKG := github.com/danos/ifmgrd

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=golang,systemd --builddirectory=$(GOBUILDDIR)

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

override_dh_auto_build: vet
	dh_auto_build -- $(GORACE)

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

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

override_dh_systemd_enable:
	dh_systemd_enable --name=ifmgrd
	dh_systemd_enable --name=ifmgrctl-hook

override_dh_systemd_start:
	dh_systemd_start --name=ifmgrd
