#!/usr/bin/make -f

export DEB_BUILD_MAINT_OPTIONS = hardening=+all
# Ubuntu's dpkg adds -Wl,-Bsymbolic-functions by default; it binds
# libatalk's internal adf_pread calls at link time so the testsuite's
# LD_PRELOAD short-read injector never fires (Debian does not add it).
export DEB_LDFLAGS_MAINT_STRIP = -Wl,-Bsymbolic-functions

# build chroots have no running systemd; keep meson's install script
# from failing at "systemctl daemon-reload"
export SYSTEMD_OFFLINE = 1

# Debian 12 ships rustc 1.63; the stuffit FFI built there has no CLI
# binary (clap MSRV 1.74), so netatalk builds without StuffIt support.
IS_RUSTC163 := $(shell grep -qs "^12\." /etc/debian_version && echo yes)

MESON_FLAGS := \
	-Dwith-pkgconfdir-path=/etc/netatalk \
	-Dwith-statedir-path=/var/lib \
	-Dwith-lockfile-path=/run/netatalk \
	-Dwith-spooldir=/var/spool/netatalk \
	-Dwith-pam-config-path=/etc/pam.d \
	-Dwith-init-style=systemd \
	-Dwith-install-hooks=false \
	-Dwith-rpath=false \
	-Dwith-appletalk=true \
	-Dwith-cups-pap-backend=true \
	-Dwith-acls=true \
	-Dwith-cracklib=true \
	-Dwith-cups=true \
	-Dwith-kerberos=true \
	-Dwith-krbV-uam=true \
	-Dwith-gssapi=true \
	-Dwith-ldap=true \
	-Dwith-pam=true \
	-Dwith-quota=true \
	-Dwith-zeroconf=true \
	-Dwith-spotlight=true \
	-Dwith-spotlight-backends=cnid,localsearch,xapian \
	-Dwith-cnid-backends=dbd,mysql,sqlite \
	-Dwith-stuffit=true \
	-Dwith-tcp-wrappers=true \
	-Dwith-testsuite=true \
	-Dwith-docs-install-path=/usr/share/doc/netatalk \
	-Dwith-docs-l10n=true \
	-Dwith-tests=true

%:
	dh $@

override_dh_auto_configure:
	# stage a probe-friendly bdb tree: Debian's libdb lives in the
	# multiarch dir, but upstream's probe only checks <path>/lib.
	# One level above CURDIR keeps it outside the meson source tree;
	# realpath strips the ".." so meson never sees an absolute path
	# that string-prefixes into the source tree
	mkdir -p ../bdb-shim/include ../bdb-shim/lib
	ln -sf /usr/include/db.h ../bdb-shim/include/db.h
	for s in /usr/lib/$(DEB_HOST_MULTIARCH)/libdb*.so*; do \
		ln -sf "$$s" "../bdb-shim/lib/$$(basename $$s)"; done
ifneq ($(IS_RUSTC163),yes)
	# force the static archive: override the pkg-config entry with -l:
	# syntax so the .so devel symlink cannot win at link time
	mkdir -p pc-static
	sed 's|-lstuffit_ffi$$|-l:libstuffit_ffi.a -lpthread -ldl -lm|' \
		/usr/lib/$(DEB_HOST_MULTIARCH)/pkgconfig/stuffit-ffi.pc \
		> pc-static/stuffit-ffi.pc
	export PKG_CONFIG_PATH=$$PWD/pc-static:$$PKG_CONFIG_PATH
	dh_auto_configure -- -Dwerror=false -Dwith-bdb-path=$(realpath ../bdb-shim) $(MESON_FLAGS)
else
	dh_auto_configure -- -Dwerror=false -Dwith-bdb-path=$(realpath ../bdb-shim) $(filter-out -Dwith-stuffit=true,$(MESON_FLAGS)) \
		-Dwith-stuffit=false
endif

override_dh_auto_test:
	dh_auto_test --no-parallel

override_dh_auto_install:
	dh_auto_install
	# Webmin module (same content as the upstream .wbm release asset)
	mkdir -p debian/tmp/usr/share/webmin/netatalk
	cp -a contrib/webmin_module/. debian/tmp/usr/share/webmin/netatalk/
	rm debian/tmp/usr/share/webmin/netatalk/meson.build
	sed -i 's/@netatalk_version@/$(DEB_VERSION_UPSTREAM)/' \
		debian/tmp/usr/share/webmin/netatalk/module.info.in
	mv debian/tmp/usr/share/webmin/netatalk/module.info.in \
		debian/tmp/usr/share/webmin/netatalk/module.info
	# ja docs go to the -doc-ja package; move them out of debian/tmp so
	# netatalk-doc.install can claim the rest of the doc tree
	mkdir -p debian/netatalk-doc-ja/usr/share/doc/netatalk/manual
	mv debian/tmp/usr/share/doc/netatalk/manual/ja \
		debian/netatalk-doc-ja/usr/share/doc/netatalk/manual/ja

override_dh_installsystemd:
	# --no-start: units are enabled but only start at boot; an
	# unconfigured afpd should not start as a side effect of package
	# installation. The RPM packages do the same. -pnetatalk: the doc
	# packages have no units and dh_installsystemd errors on them.
	# No positional unit args: debhelper 13.11 (Debian 12) treats them
	# as files under the package root, unlike 13.24 (Debian 13).
	dh_installsystemd --no-start -pnetatalk
	sed -i '/^\[Service\]/a RuntimeDirectory=netatalk' \
		debian/netatalk/usr/lib/systemd/system/netatalk.service
