#!/usr/bin/make -f

GUIDEDIR=$(CURDIR)/Users_Guide
DOCDIR=$(CURDIR)/debian/publican/usr/share/doc/publican

%:
	dh $@ --with linktree

override_dh_auto_configure:
	dh_auto_configure
	if [ ! -f META.yml.dist ]; then cp -f META.yml META.yml.dist; fi

override_dh_auto_build:
	dh_auto_build
	cd po && for po in *.po; do \
		msgfmt -o $${po%%.po}.mo $$po; \
	done
	cd $(GUIDEDIR) && \
	perl -CDAS -I $(CURDIR)/blib/lib $(CURDIR)/blib/script/publican build \
	   --formats=html-desktop --publish --langs=all \
	   --common_config="$(CURDIR)/blib/datadir" \
	   --common_content="$(CURDIR)/blib/datadir/Common_Content"
ifneq (,$(filter nocheck,$(DEB_BUILD_OPTIONS)))
	# The tests build files that we install as example, if they are
	# not run do it ourselves
	cd $(GUIDEDIR) && \
	perl -CDAS -I $(CURDIR)/blib/lib $(CURDIR)/blib/script/publican update_pot
	cd $(GUIDEDIR) && \
	perl -CDAS -I $(CURDIR)/blib/lib $(CURDIR)/blib/script/publican update_po \
		--langs=de-DE
endif

override_dh_auto_test:
ifeq (,$(filter nocheck,$(DEB_BUILD_OPTIONS)))
	LC_ALL=C dh_auto_test
endif

override_dh_auto_install:
	dh_auto_install
	cd po && for mo in *.mo; do \
		lang=$${mo%%.mo}; \
		install -m 644 -D $$mo $(CURDIR)/debian/publican/usr/share/locale/$$lang/LC_MESSAGES/publican.mo; \
	done

override_dh_installdocs:
	dh_installdocs
	mv $(GUIDEDIR)/publish $(DOCDIR)/Users_Guide

override_dh_compress:
	# Examples dir contain files that should not be compressed
	# otherwise publican will not recognize them
	dh_compress -X/examples/
	# Use hardlinks to deduplicate files and save space
	rdfind -makehardlinks true -outputname /dev/null debian/publican/usr/share/publican

override_dh_auto_clean:
	dh_auto_clean
	rm -f po/*.mo
	# Created and not cleaned by t/910.publican.Users_Guide.t
	rm -rf $(GUIDEDIR)/de-DE $(GUIDEDIR)/pot

	# Removed during build process.
	if [ -f META.yml.dist ]; then mv -f META.yml.dist META.yml; fi
