#! /usr/bin/make -f
%:
	dh $@

DEB_HOST_GNU_TYPE  ?= $(shell dpkg-architecture -qDEB_HOST_GNU_TYPE)
DEB_BUILD_GNU_TYPE ?= $(shell dpkg-architecture -qDEB_BUILD_GNU_TYPE)

dtmp    = debian/man-db

include /usr/share/hardening-includes/hardening.make

# Cf. #497653
ifeq (,$(filter noopt,$(DEB_BUILD_OPTIONS)))
CFLAGS = -O2 -g
else
CFLAGS = -g
endif
CFLAGS += $(HARDENING_CFLAGS)
LDFLAGS := $(HARDENING_LDFLAGS)

# --libexecdir still needed due to #541458
override_dh_auto_configure:
	CFLAGS="$(CFLAGS)" LDFLAGS="$(LDFLAGS)" dh_auto_configure -- \
		    --libexecdir=\$${libdir} \
	            --enable-setuid --enable-undoc='man 7 undocumented' \
	            --with-device=latin1 --enable-mb-groff \
	            --with-config-file=/etc/manpath.config \
	            --with-browser=www-browser --with-pager=pager \
	            --with-col=col --with-vgrind=vgrind --with-grap=grap \
	            --with-compress=compress --with-bzip2=bzip2 \
		    --with-lzma=lzma \
		    --with-sections='1 n l 8 3 2 3posix 3pm 3perl 5 4 9 6 7'

override_dh_auto_build:
	[ ! -e po/fr.gmo ] || mv po/fr.gmo po/fr.gmo.safe
	set -e; for preserve in man/po4a/po/*.pot man/po4a/po/*.po; do \
		[ ! -e "$$preserve.safe" ] || continue; \
		cp -a "$$preserve" "$$preserve.safe"; \
	done
	dh_auto_build
	$(MAKE) -C po fr.gmo
	sed -f man/fr/replace.sed -e 's,%program%,manconv,g' \
		man/fr/man1/manconv.man1 > man/fr/man1/manconv.1

override_dh_auto_install:
	dh_auto_install
	install -m 644 man/fr/man1/manconv.1 $(dtmp)/usr/share/man/fr/man1/

override_dh_auto_clean:
	dh_auto_clean
	set -e; for preserve in man/po4a/po/*.pot man/po4a/po/*.po; do \
		[ -e "$$preserve.safe" ] || continue; \
		mv "$$preserve.safe" "$$preserve"; \
	done
	[ ! -e po/fr.gmo.safe ] || mv po/fr.gmo.safe po/fr.gmo
	rm -f man/fr/man1/manconv.1

override_dh_clean:
	dh_clean -Xxmalloc.c.orig

ifeq ($(DEB_BUILD_GNU_TYPE),$(DEB_HOST_GNU_TYPE))
override_dh_installman:
	PATH="$(CURDIR)/$(dtmp)/usr/bin:$$PATH" dh_installman
endif

override_dh_fixperms:
	dh_fixperms
	chown man:root $(dtmp)/var/cache/man
	chmod 2755     $(dtmp)/var/cache/man
