#!/usr/bin/make -f
#export DH_VERBOSE=1

export DEB_BUILD_MAINT_OPTIONS = hardening=+all

%:
	dh $@

include /usr/share/dpkg/default.mk
VER_FULL = $(DEB_VERSION_UPSTREAM)

FORMATS = \
	aperio \
	argos \
	dicom \
	hamamatsu \
	huron \
	leica \
	mirax \
	philips \
	sakura \
	trestle \
	ventana \
	zeiss \
	generic-tiff

%.html:
	wget -O $@ https://openslide.org/formats/$(@:%.html=%)/

properties.html:
	wget -O $@ https://openslide.org/docs/$(@:%.html=%)/

%.xml: %.html
	xsltproc --nonet --output $<.dummy.xml --html debian/process.xsl $<
	sed -i -e 's/BEGIN SECTION/<refsection>/g' -e 's@END SECTION@</refsection>@g' $<.dummy.xml
	xmllint --nonet --output $@ --format $<.dummy.xml
	rm $<.dummy.xml

debian/openslide-formats.3: properties.xml $(FORMATS:%=%.xml) debian/openslide-formats.3.xml
	(cd debian && sed -e 's@VER_FULL@$(VER_FULL)@g' openslide-formats.3.xml > openslide.tmp.xml)
	(cd debian && xsltproc --xinclude openslide.tmp.xml)
	@echo 'I: Run `debian/rules clean-man` to remove downloaded artifacts.'

clean-man:
	rm -vf $(FORMATS:%=%.xml) $(FORMATS:%=%.html)
	rm -vf debian/openslide.tmp.xml properties.html properties.xml
.PHONY: clean-man

# Prevent inconsistencies between the manual page and the current
# openslide version.
MAN_VER = $(shell sed -n 's/.\\"    Source: OpenSlide //p' debian/openslide-formats.3)
execute_before_dh_update_autotools_config:
	@ if [ "$(MAN_VER)" != "$(VER_FULL)" ] \
	; then	echo 'E: debian/openslide-formats.3 documents openslide $(MAN_VER).' >&2 \
	;	echo 'E: However, the current version shipped is $(VER_FULL).' >&2 \
	;	echo 'E: Please run the following to refresh the manual:' >&2 \
	;	echo 'E: 	$$ debian/rules debian/openslide-formats.3' >&2 \
	;	echo 'E: Note it needs internet and a couple of XSL utilities.' >&2 \
	; 	exit 1 \
	; fi

override_dh_install:
	dh_install
	find debian/tmp -name lib*.la -delete
