#!/usr/bin/make -f

# Avoid unneeded library dependencies reported by dpkg-shlibdeps.
DEB_LDFLAGS_MAINT_APPEND := -Wl,--as-needed
DEB_BUILD_MAINT_OPTIONS := hardening=+all
include /usr/share/dpkg/buildflags.mk
include /usr/share/dpkg/pkg-info.mk

%:
	dh $@ --with python3

override_dh_auto_configure:
	dh_auto_configure -- --enable-gc=system	\
		--with-latex=/usr/share/texmf/tex/latex		\
		--with-context=/usr/share/texmf/tex/context/third \
		$(foreach v,CFLAGS CPPFLAGS CXXFLAGS LDFLAGS,'$(v)=$($(v))')

# Avoid a warning every time asy is run if HOME is unset.
export ASYMPTOTE_HOME := $(CURDIR)/debian/tmp_asymptote_home
override_dh_auto_configure: | $(ASYMPTOTE_HOME)
$(ASYMPTOTE_HOME):
	mkdir $(ASYMPTOTE_HOME)

override_dh_auto_build:
	make asy man
	make all html
	# fix location of images in info document
	cat doc/png/asymptote.info | sed -e 's!image src="\([^"]*\)"!image src="/usr/share/doc/asymptote-doc/html/\1"!g' > doc/png/asymptote.info.fixed
	mv doc/png/asymptote.info.fixed doc/png/asymptote.info

override_dh_auto_install:
	make install-asy DESTDIR=$(CURDIR)/debian/tmp
	# fix xasy absolute link
	ln -sf ../share/asymptote/GUI/xasy.py debian/tmp/usr/bin/xasy
	# fix permissions
	chmod 0644 debian/tmp/usr/share/asymptote/GUI/icons_rc.py
	dh_installtex -pasymptote

override_dh_auto_test:
	: do nothing here, otherwise make tries to compile prc/test.cc

override_dh_compress:
	dh_compress -X.pdf

override_dh_missing:
	dh_missing --fail-missing

override_dh_clean:
	dh_clean
	rm --force doc/latexusage.pdf doc/latexusage.dvi doc/TeXShopAndAsymptote.dvi doc/CAD.dvi
	rm --force doc/*.aux doc/*.cp doc/*.cps doc/*.idx doc/*.ins doc/*.log doc/*.toc doc/asymptote.fn doc/asymptote.ky doc/asymptote.pg doc/asymptote.tp doc/asymptote.vr asy.list
	rm --force doc/asyRefCard.pdf doc/asyRefCard.dvi
	rm -fr $(ASYMPTOTE_HOME)

#
# download current release based on the changelog entry from sf
# and remove the generated files similarily to the clean target
# code from the calibre packaging
get-orig-source:
	set -e; \
	mkdir -p debian/orig; cd debian/orig; \
	wget -O - https://downloads.sourceforge.net/project/$(DEB_SOURCE)/$(DEB_VERSION_UPSTREAM)/$(DEB_SOURCE)-$(DEB_VERSION_UPSTREAM).src.tgz | gzip -cd | tar x; \
	D=`pwd`; \
	cd $(DEB_SOURCE)*; \
	rm --force doc/latexusage.pdf doc/latexusage.dvi doc/TeXShopAndAsymptote.dvi doc/CAD.dvi; \
	rm --force doc/*.aux doc/*.cp doc/*.cps doc/*.idx doc/*.ins doc/*.log doc/*.toc asy.list; \
	rm --force doc/asyRefCard.pdf doc/asyRefCard.dvi;\
	cd ..;\
	tar c . | gzip -9 > ../../../$(DEB_SOURCE)_$(DEB_VERSION_UPSTREAM).orig.tar.gz; \
	cd ../..; rm -r debian/orig
