#!/usr/bin/make -f

# Disable the build target so that build-arch and build-indep (or binary-arch
# and binary-indep) can do the building instead.
build: ;
.PHONY: build

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

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

%:
	dh $@

build-doc: build-stamp
	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
	# clean up auto-generated tex output
	rm -rf doc/examples/tex

override_dh_clean:
	dh_clean
	$(MAKE) -C doc clean
	rm -rf doc-plastex
	rm -rf $(CURDIR)/tmp-home

override_dh_auto_build-arch: build-stamp

build-stamp:
	sed -i 's,^GV_COMMAND=/bin/false,GV_COMMAND=/usr/bin/gv\nGV_OPT=--,' Makefile
	$(MAKE) USRDIR=/usr
	touch build-stamp

override_dh_auto_build-indep: build-doc

override_dh_auto_install-arch:
	$(MAKE) install USRDIR=/usr DESTDIR=$(CURDIR)/debian/tmp
	sh debian/mkicons $(CURDIR)/debian/pyxplot

override_dh_auto_install-indep:
	$(MAKE) install-doc USRDIR=/usr DESTDIR=$(CURDIR)/debian/tmp

override_dh_installdocs:
	dh_installdocs --link-doc pyxplot

override_dh_compress:
	dh_compress --all -X.pdf

.PHONY: override_dh_clean override_dh_installdocs override_dh_compress build-doc
