#!/usr/bin/make -f

export DEB_BUILD_MAINT_OPTIONS=hardening=+bindnow future=+lfs
export DH_VERBOSE=1

POLFILE = "/etc/$(shell convert -version|sed -n '/^Version: /s@Version: ImageMagick \([[:digit:]]\+\)\..*@ImageMagick-\1@p')/policy.xml"

%:
	dh $@ --buildsystem=cmake+ninja --builddirectory=build

override_dh_auto_configure:
	dh_auto_configure -- \
	-DUSE_BUNDLED_SHAPELIB=OFF \
	-DUSE_BUNDLED_CATCH2=OFF \
	-DUSE_BUNDLED_FMT=OFF

override_dh_auto_build-indep:
	# just build the -doc package
ifeq ($(DEB_BUILD_ARCH),$(DEB_HOST_ARCH))
	# create HTML documentation for samples
	# modify the system policy preventing conversion of PDF files
	mkdir -p debian/tmp/ImageMagick
	sed -e '/<policy domain="coder" rights="none" pattern="PDF" .>/s@"none"@"read|write"@' "$(POLFILE)" > debian/tmp/ImageMagick/policy.xml
	XDG_CONFIG_HOME="$(shell pwd)/debian/tmp" cmake --build build -t samples-html
	rm -Rf debian/tmp/ImageMagick
endif
	cmake --build build -t thbook

override_dh_compress:
	dh_compress -X.pdf -X.th -X.th2 -Xthconfig

override_dh_auto_build-arch:
	dh_auto_build
	ln -sf loch.1 man/therion-viewer.7

override_dh_auto_install-arch:
	dh_auto_install
	rm debian/tmp/etc/*.new
	dh_installexamples -ptherion --exclude=.tcl --exclude=Makefile --exclude=CMakeLists.txt --exclude=thTMPDIR samples therion.ini

override_dh_auto_install-indep:
	cmake --install build --prefix $(CURDIR)/debian/tmp/usr --component th-docs

#don't run tests in arch-all build because utest binary is not built
override_dh_auto_test-indep:
