#!/usr/bin/make -f
#export DH_VERBOSE = 1

export GOCACHE=$(shell mktemp -d /tmp/gocache-XXXX)
export GOFLAGS=-ldflags=-X=github.com/ubuntu/adsys/internal/consts.Version=$(shell dpkg-parsechangelog -S Version) --mod=vendor -buildmode=pie

# Install all content to embed
export DH_GOLANG_INSTALL_ALL := 1

# Skip integration tests when building package: they need docker images.
export ADSYS_SKIP_INTEGRATION_TESTS=1

%:
	dh $@ --buildsystem=golang --with=golang,apport

override_dh_auto_clean:
	dh_auto_clean
	# create the vendor directory when building the source package
	[ -d vendor/ ] || go mod vendor

override_dh_auto_install:
	dh_auto_install -- --no-source
	# install in /sbin
	mv debian/adsys/usr/bin/ debian/adsys/sbin
	rmdir debian/adsys/usr/
	# create adsysctl command
	ln -s adsysd debian/adsys/sbin/adsysctl
	# Workaround file system race making debian/adsys/lib/ a file while it’s a directory
	mkdir debian/adsys/lib
	# run go generate to install assets, but don’t regenerate them
	GENERATE_ONLY_INSTALL_TO_DESTDIR=$(CURDIR)/debian/adsys go generate -tags tools $(GOFLAGS) ./...
	# remove admxgen helper from installation
	rm debian/adsys/sbin/admxgen
