#!/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)
builddir = debian/build/flavor-
FLAVORS = release asan
CONFIG_asan = -DCMAKE_BUILD_TYPE=Debug -DSANITIZE=address -DENABLE_LTO=OFF -DENABLE_FULL_DEBUG=ON
CONFIG_release = -DCMAKE_BUILD_TYPE=RelWithDebInfo -DENABLE_LTO=ON

ENABLE_LUAJIT := -DENABLE_LUAJIT=ON -DLUA_ROOT=/luajit-build

# Can be filled by a build system
EXTRA_ARGS :=
ENABLE_JEMALLOC := -DENABLE_JEMALLOC=ON
ENABLE_HYPERSCAN := -DENABLE_HYPERSCAN=ON

ifneq ($(filter $(DEB_HOST_ARCH),arm64),)
	ENABLE_JEMALLOC := -DENABLE_JEMALLOC=OFF
	ENABLE_HYPERSCAN := -DENABLE_HYPERSCAN=ON -DHYPERSCAN_ROOT_DIR=/vectorscan
endif

%:
	dh $@ --with systemd --parallel

override_dh_auto_configure: $(patsubst %,configure_%,$(FLAVORS))
configure_%:
	mkdir -p $(builddir)$*
	cd $(builddir)$* && cmake ../../../  -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_INSTALL_PREFIX:PATH=/usr \
		-DCMAKE_BUILD_WITH_INSTALL_RPATH=ON \
		-DNO_TARGET_VERSIONS=1 \
		-DENABLE_PCRE2=ON \
		-DENABLE_LIBUNWIND=ON \
		-DWANT_SYSTEMD_UNITS=ON \
		-DENABLE_LIBCXX=AUTO \
		$(ENABLE_HYPERSCAN) \
		$(ENABLE_LUAJIT) \
		$(ENABLE_JEMALLOC) \
		$(CONFIG_$*) \
		$(EXTRA_ARGS)

override_dh_auto_build: $(patsubst %,build_%,$(FLAVORS))
build_%:
	dh_auto_build --sourcedirectory=$(builddir)$*

override_dh_strip: $(patsubst %,strip_%,$(FLAVORS))
strip_%:
	if [ "$*" = "asan" ]; then \
		dh_strip -prspamd-asan --dbg-package=rspamd-asan-dbg ; \
	else \
		dh_strip -prspamd --dbg-package=rspamd-dbg ; \
	fi

override_dh_makeshlibs:
	dh_makeshlibs -Xrspamd-actrie

override_dh_auto_install: $(patsubst %,install_%,$(FLAVORS))
install_%:
	if [ "$*" = "asan" ]; then \
		cd $(builddir)$* && make install DESTDIR=../../../debian/rspamd-asan ; \
		cd ../../../debian/rspamd-asan &&  mkdir -p var/lib/rspamd \
																								 var/log/rspamd \
																								 etc/rspamd/local.d \
																								 etc/rspamd/override.d  ; \
	else \
		cd $(builddir)$* && make install DESTDIR=../../../debian/rspamd ; \
		cd ../../../debian/rspamd &&  mkdir -p var/lib/rspamd \
																								 var/log/rspamd \
																								 etc/rspamd/local.d \
																								 etc/rspamd/override.d  ; \
	fi

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