#!/usr/bin/make -f

# A network-facing daemon: also enable bindnow, so that full RELRO makes
# the GOT read-only once the binary and its modules have been loaded.
export DEB_BUILD_MAINT_OPTIONS = hardening=+all

include /usr/share/dpkg/architecture.mk
include /usr/share/dpkg/pkg-info.mk

I = debian/ircd-hybrid
I_ETC = $(I)/etc/ircd-hybrid

ifneq (,$(filter $(DEB_HOST_ARCH),alpha kfreebsd-i386 kfreebsd-amd64 hurd-i386))
	configure_options := --enable-poll
endif

%:
	dh $@

# Modules live in /usr/lib/ircd-hybrid/modules, so keep libdir out of the
# multiarch directory that dh_auto_configure would otherwise use.
override_dh_auto_configure:
	dh_auto_configure -- \
		--libdir=\$${prefix}/lib \
		--sysconfdir=/etc/ircd-hybrid \
		--with-tls=gnutls \
		$(configure_options) \
		--program-suffix=-hybrid \
		--without-included-ltdl \
		PACKAGE_VERSION=$(DEB_VERSION)

execute_after_dh_auto_build:
	mkdir -p debian/doc-pages
	@echo "Processing man pages: "
	@for i in $(wildcard debian/doc/*.sgml); do \
		TARGET="debian/doc-pages/`basename $$i | sed 's/\.sgml//'`"; \
		echo "$$i -> $$TARGET"; \
		docbook-to-man $$i > $$i.tmp; \
		sed -n "`grep -n '^.TH' $$i.tmp | awk -F : '{print $$1}'`~1p" $$i.tmp > $$TARGET; \
		rm -f $$i.tmp; \
	done

# Test the ban database converter, which postinst runs on upgrade; the
# same test runs as an autopkgtest against the installed script.
execute_after_dh_auto_test:
ifeq (,$(filter nocheck,$(DEB_BUILD_OPTIONS)))
	perl debian/tests/convert-ban-databases debian/scripts/convert-ban-databases
endif

execute_after_dh_auto_install:
	install -d debian/tmp/var/cache/ircd-hybrid

execute_before_dh_install:
	chmod +x debian/scripts/*

execute_after_dh_install-arch:
	# The reference configs are shipped as examples instead
	rm -f $(I_ETC)/reference*
	# Fixes lintian error non-empty-dependency_libs-in-la-file
	sed -i "/dependency_libs/ s/'.*'/''/" `find $(I) -name '*.la'`
	# LT_DLSEARCH_PATH records the build machine's ld.so search path, which
	# makes hybrid-dev unreproducible (it depends on, for example, whether
	# fakeroot is installed); only the bundled libltdl, which isn't built,
	# uses it
	sed -i '/^#define LT_DLSEARCH_PATH /d' debian/hybrid-dev/usr/include/ircd-hybrid-8/config.h
	! grep -q LT_DLSEARCH_PATH debian/hybrid-dev/usr/include/ircd-hybrid-8/config.h

override_dh_installdocs-arch:
	dh_installdocs -Xdoc/ircd. -Xline. -XMakefile -Xexample

execute_after_dh_fixperms-arch:
	chown irc:irc $(I)/var/log/ircd
	chown root:irc $(I_ETC)
	chmod 750 $(I_ETC)
	chown root:irc $(I_ETC)/ircd.conf
	chmod 640 $(I_ETC)/ircd.conf
	chown irc:irc $(I)/var/cache/ircd-hybrid
	chmod 700 $(I)/var/cache/ircd-hybrid
