#!/usr/bin/make -f
include /usr/share/dpkg/architecture.mk
include /usr/share/dpkg/pkg-info.mk

export GOCACHE := $(CURDIR)/debian/.gocache
export GOPATH := $(CURDIR)/debian/.gopath
export GOFLAGS := -mod=mod -trimpath
export GOTOOLCHAIN := local
export CGO_ENABLED := 0
export GOARCH := $(DEB_HOST_ARCH)

ifeq ($(DEB_HOST_ARCH),arm64)
ARCH_DEPS := qemu-system-arm, qemu-efi-aarch64
else
ARCH_DEPS := qemu-system-x86, ovmf
endif

%:
	dh $@

override_dh_auto_configure:

override_dh_auto_build:
	cd src && go build -ldflags "-s -w -X main.version=$(DEB_VERSION)" -o ../build/vm-reactor .

override_dh_auto_test:

override_dh_auto_install:
	install -D -m 0644 extra/fail2ban-vm-reactor.conf debian/vm-reactor/etc/fail2ban/filter.d/vm-reactor.conf

override_dh_auto_clean:
	rm -rf build debian/.gocache debian/.gopath

override_dh_fixperms:
	dh_fixperms
	chmod 0755 debian/vm-reactor/usr/lib/vm-reactor/ansible-inventory

override_dh_strip:

override_dh_dwz:

override_dh_installsystemd:
	dh_installsystemd --name=vm-reactor vm-reactor.service
	dh_installsystemd --name=vm-reactor-guests --no-stop-on-upgrade --no-restart-after-upgrade vm-reactor-guests.service

override_dh_gencontrol:
	dh_gencontrol -- -Vvm-reactor:Depends="$(ARCH_DEPS)"
