#!/usr/bin/make -f
# -*- makefile -*-

export DEB_BUILD_MAINT_OPTIONS = hardening=+pie


ifneq (,$(filter $(DEB_HOST_ARCH), armhf armel m68k mips mipsel powerpc powerpcspe sh4))
  export DEB_LDFLAGS_MAINT_APPEND=-latomic
endif

REALFILE = \
	bin/ds-logpipe.py \
	bin/logconv.pl \
	share/man/man1/ds-logpipe.py.1 \
	share/man/man1/logconv.pl.1 \

%:
	dh $@ --with python3 --builddir build/

override_dh_auto_clean:
	dh_auto_clean
	rm -f aclocal.m4 config.* ltmain.sh m4/libtool.m4 m4/lt*.m4
	rm -f ldap/servers/snmp/ldap-agent.conf
	rm -rf src/lib389/build src/lib389/lib389.egg-info
	find src/lib389/ -name '__pycache__' -exec rm -rf '{}' ';'
	rm -f src/lib389/man/*.8

override_dh_auto_configure:
	dh_auto_configure -- \
		--with-openldap \
		--with-systemd \
		--with-systemdsystemunitdir=/lib/systemd/system \
		--with-systemdsystemconfdir=/etc/systemd/system \
		--with-systemdgroupname=dirsrv.target \
		--with-tmpfiles-d=/etc/tmpfiles.d \
		--enable-autobind \
		--enable-cmocka \
		--enable-icu \
		--enable-perl

override_dh_auto_build:
	(cd src/lib389 && python3 setup.py build)
	dh_auto_build

override_dh_auto_install:
	(cd src/lib389 && python3 setup.py install --install-layout=deb --root ../../debian/tmp)

	dh_auto_install --max-parallel=1

override_dh_install:
	# lets do the renaming here afterall, instead of in 389-ds-base.install
	for file in $(REALFILE); do mv -f $(CURDIR)/debian/tmp/usr/$$file \
		$(CURDIR)/debian/tmp/usr/`echo $$file | \
		sed -s 's/\.pl//;s/\.py//'`; \
		done
	# purge .la files
	find $(CURDIR)/debian/tmp -name "*.la" -type f -exec rm -f "{}" \;

	mkdir -p $(CURDIR)/debian/tmp/etc/systemd/system/dirsrv.target.wants

	# fix the manpage section, argparse-manpage hardcodes it as 1
	sed -i  "1s/\"1\"/\"8\"/" debian/tmp/usr/share/man/man8/dsconf.8
	sed -i  "1s/\"1\"/\"8\"/" debian/tmp/usr/share/man/man8/dscreate.8
	sed -i  "1s/\"1\"/\"8\"/" debian/tmp/usr/share/man/man8/dsctl.8
	sed -i  "1s/\"1\"/\"8\"/" debian/tmp/usr/share/man/man8/dsidm.8

	dh_install

override_dh_missing:
	dh_missing --fail-missing

override_dh_installsystemd:
	dh_installsystemd -p389-ds-base --no-enable dirsrv-snmp.service

override_dh_shlibdeps:
	dh_shlibdeps -l"debian/389-ds-base/usr/lib/$(DEB_HOST_MULTIARCH)/dirsrv" -a
