#!/usr/bin/make -f

TEXDOC = PyScanFCS_doc.tex
DOCDIR = doc
BIBDOC = PyScanFCS_doc.aux
PDF = PyScanFCS_doc.pdf
# see https://wiki.debian.org/ReproducibleBuilds/TimestampsInPDFGeneratedByLaTeX
DEB_DATE_RFC_2822 := $(shell dpkg-parsechangelog -S date)
DEB_DATE_RFC_3339 := $(shell date -u "--rfc-3339=seconds" -d "$(DEB_DATE_RFC_2822)")
PDFLATEX = pdflatex -synctex=1 -interaction=nonstopmode $(TEXDOC)
FAKETIME = faketime -f "$(DEB_DATE_RFC_3339)"

pkg := pyscanfcs
export DH_VERBOSE=1
export DEB_BUILD_MAINT_OPTIONS = hardening=+all
export PYBUILD_NAME=$(pkg)
export PYBUILD_INSTALL_ARGS_python3=--install-lib=/usr/lib/${PYBUILD_NAME}
export PYBUILD_TEST_PYTEST=1


%:
	dh $@ --with python3 --buildsystem=pybuild

override_dh_auto_clean:
	touch doc/PyScanFCS_doc.pdf
	dh_auto_clean

override_dh_auto_build:
	cd $(DOCDIR) && \
	(   $(FAKETIME) $(PDFLATEX) \
		bibtex $(BIBDOC) \
		$(FAKETIME) $(PDFLATEX) \
		$(FAKETIME) $(PDFLATEX) \
	)
	rm -f $(PDF); ln -s $(DOCDIR)/$(PDF)
	convert -background "rgba(255,255,255,0)" -geometry 32x32 \
		$(DOCDIR)/Images/PyScanFCS_icon.svg debian/pyscanfcs.xpm
	dh_auto_build

override_dh_install:
	dh_install
	mv $(CURDIR)/debian/$(pkg)/usr/bin/pyscanfcs \
           $(CURDIR)/debian/$(pkg)/usr/lib/$(pkg)/pyscanfcs_run
	cd $(CURDIR)/debian/$(pkg)/usr/bin/; \
        ln -s ../lib/$(pkg)/pyscanfcs_run pyscanfcs; cd -
	mv debian/pyscanfcs.xpm \
		debian/$(pkg)/usr/share/pixmaps
	chmod -R 644 debian/$(pkg)/usr/share/doc/$(pkg)/examples/misc

override_dh_compress:
	dh_compress -X$(PDF)

override_dh_installexamples:
	tar --sort=name \
            --mtime="@${SOURCE_DATE_EPOCH}" \
            --owner=root --group=root --numeric-owner \
            --mode=go=rX,u+rw,a-s \
            -cJf $(CURDIR)/debian/$(pkg)/usr/share/doc/$(pkg)/examples/tests.tar.xz  tests/
	dh_installexamples
