#!/usr/bin/make -f

ifneq (,$(filter noopt,$(DEB_BUILD_OPTIONS)))
	CFLAGS += -O0
else
	CFLAGS += -O2
endif

# make sure $HOME is set and writable so the latex tools work
export HOME = $(CURDIR)/tmp-home
DUMMY := $(shell mkdir -p $(HOME))

%:
	dh $@

doc:
	convert -background none doc/tick.svg doc/tick.png
	convert -background none doc/cross.svg doc/cross.png
	$(MAKE) -C doc
	cp -r doc doc-plastex
	# Work around some plastex unhappiness
	cp debian/pyxplot-plastex.tex doc-plastex
	sed -i -r 's/(\\begin\{enumerate\})\[\(i\)\]/\1/' doc-plastex/*.tex
	cd doc-plastex; TEXINPUTS= plastex -d html --disable-image-cache --title="PyXPlot Users' Guide" pyxplot-plastex.tex
	rm -rf doc-plastex/html/.cache # --disable-image-cache does not seem to work
	# plastex leaves __init__.py files around
	find doc-plastex/html '(' -name __init__.py -or -name '__init__.py[co]' ')' -delete

override_dh_clean:
	dh_clean
	$(MAKE) -C doc clean
	rm -rf doc/html doc/pyxplot.lof doc/pyxplot.paux doc/tick.png doc/cross.png
	rm -rf doc-plastex
	rm -f Makefile pyxplot.1 pyxplot_watch.1
	rm -rf $(CURDIR)/tmp-home

override_dh_auto_build:
	sed -i 's,^GV_COMMAND=/bin/false,GV_COMMAND=/usr/bin/gv\nGV_OPT=--,' Makefile
	make USRDIR=/usr

override_dh_auto_install: doc
	make install USRDIR=/usr DESTDIR=$(CURDIR)/debian/tmp
	sh debian/mkicons $(CURDIR)/debian/pyxplot

override_dh_installdocs:
	dh_installdocs --link-doc pyxplot

override_dh_compress:
	dh_compress --all -X.pdf

.PHONY: override_dh_clean override_dh_auto_build override_dh_auto_install override_dh_installdocs override_dh_compress doc
