#!/usr/bin/make -f

export DEB_BUILD_MAINT_OPTIONS = hardening=+all reproducible=+fixfilepath
export DEB_LDFLAGS_MAINT_APPEND = -Wl,--as-needed
export ASAN_OPTIONS=detect_leaks=0

DEB_HOST_ARCH ?= $(shell dpkg-architecture -qDEB_HOST_ARCH)

ifneq ($(filter $(DEB_HOST_ARCH),amd64 armel armhf i386 mips mips64el mipsel ppc64 ppc64el),)
	ENABLE_LUAJIT := ON
else
	ENABLE_LUAJIT := OFF
endif

# Can be filled by a build system
EXTRA_ARGS :=

ifneq ($(filter $(DEB_HOST_ARCH),amd64 i386),)
	ENABLE_HYPERSCAN := -DENABLE_HYPERSCAN=ON
else
	ENABLE_HYPERSCAN :=
endif
%:
	dh $@ --with systemd --parallel

override_dh_auto_configure:
	dh_auto_configure -- -DCONFDIR=/etc/rspamd \
		-DMANDIR=/usr/share/man \
		-DRUNDIR=/run/rspamd \
		-DDBDIR=/var/lib/rspamd \
		-DLOGDIR=/var/log/rspamd \
		-DSHAREDIR=/usr/share/rspamd \
		-DLIBDIR=/usr/lib/rspamd \
		-DINCLUDEDIR=/usr/include \
		-DSYSTEMDDIR=/lib/systemd/system \
		-DCMAKE_BUILD_TYPE=None \
		-DCMAKE_BUILD_WITH_INSTALL_RPATH=ON \
		-DENABLE_LTO=ON \
		-DDEBIAN_BUILD=1 \
		-DENABLE_JEMALLOC=ON \
		-DENABLE_PCRE2=ON \
		-DENABLE_LUAJIT=$(ENABLE_LUAJIT) \
		-DENABLE_LIBUNWIND=ON \
		-DWANT_SYSTEMD_UNITS=ON \
		-DENABLE_STATIC_LIBCXX=OFF \
		-DENABLE_LIBCXX=OFF \
		$(ENABLE_HYPERSCAN) \
		$(EXTRA_ARGS)

override_dh_strip:
	dh_strip --dbg-package=rspamd-dbg

override_dh_makeshlibs:
	dh_makeshlibs -Xrspamd-actrie

override_dh_auto_install:
	dh_auto_install --destdir=debian/rspamd

override_dh_auto_clean:
	dh_auto_clean
	rm -f src/modules.c

override_dh_fixperms:
	dh_fixperms
	chmod 750 $(CURDIR)/debian/rspamd/var/lib/rspamd
	chmod 750 $(CURDIR)/debian/rspamd/var/log/rspamd
