#!/usr/bin/make -f

# get './pkg/config/containers.conf' installed
export DH_GOLANG_INSTALL_ALL := 1

# disable LTO, cf. https://wiki.debian.org/ToolChain/LTO
export DEB_BUILD_MAINT_OPTIONS := optimize=-lto

# avoid installing test utilities
export DH_GOLANG_EXCLUDES := netavark/testplugin

BUILDTAGS := containers_image_openpgp,systemd

APPARMOR_PROFILE_TEMPLATE := pkg/apparmor/apparmor_linux_template.go
APPARMOR_PROFILE_TEMPLATE_BUILD_SHA256 := $(shell sha256sum $(CURDIR)/$(APPARMOR_PROFILE_TEMPLATE) | cut -d ' ' -f 1)
# In pkg/apparmor/apparmor.go, AppArmorSuffix == "-apparmor1"
# Remember to apply the patches before getting the hash.
APPARMOR_PROFILE_TEMPLATE_SHA256 := a25f5c6a58f4464207b64dfd30b6a4b912fca8a9314009044027e292548a1686

%:
	dh $@ --builddirectory=_build --buildsystem=golang --with=golang

override_dh_auto_build:
	# Check whether the AppArmor profile template was modified, if yes, do not let the build
	# succeed and notify that the AppArmorSufix needs updating.
ifeq ($(APPARMOR_PROFILE_TEMPLATE_SHA256), $(APPARMOR_PROFILE_TEMPLATE_BUILD_SHA256))
	@echo "The AppArmor profile template is unchanged."
else
	@echo "The file $(APPARMOR_PROFILE_TEMPLATE) changed, and the AppArmorSuffix variable"
	@echo "in pkg/apparmor/apparmor.go needs to be updated accordingly to make this"
	@echo "update effective."
	exit 1
endif
	dh_auto_build -v  --builddirectory=_build -- -tags "$(BUILDTAGS)"

override_dh_auto_install:
	dh_auto_install --builddirectory=_build --buildsystem=golang
	make -C docs docs GOMD2MAN=go-md2man
	make -C docs install install DESTDIR=$(CURDIR)/debian/golang-github-containers-common PREFIX=/usr

override_dh_auto_test:
	DH_GOLANG_EXCLUDES="pkg/auth pkg/ssh pkg/cgroupv2 pkg/machine pkg/config pkg/cgroups libimage pkg/secrets/passdriver libnetwork/cni libnetwork/netavark" \
	dh_auto_test

execute_after_dh_clean:
	make -C docs clean
