#!/usr/bin/make -f

# Uncomment this to turn on verbose mode.
#export DH_VERBOSE=1

%:
	dh $@ --with python2 

CFLAGS += -fPIC
VERSION:=2.12.9

DEB_HOST_MULTIARCH ?= $(shell dpkg-architecture -qDEB_HOST_MULTIARCH)

# --enable-opengl can't be used as lesstif not supported - missing Motif functionality

override_dh_auto_clean:
	find . -type l -exec rm {} \;
	find . -name '*.pyc' -exec rm {} \;
	find . -name '.libs' -exec rm -rf {} \;  || echo ".libs deleted already."
	find test \( -name '*.ps' -o -name '*.pdf' -o -name '*.svg' -o -name '*.png' \
	       -o -name '*.kmz' \)  -exec rm  {} \;
	rm -f config.log
	rm -rf share/magics/ttf src/terralib

override_dh_auto_configure:
	# Fix libtoolize; apply patch _after_ libtool has been updated to fix libtool brokenness.
	libtoolize -c -f
	(cd config && patch < ../debian/as-needed.patch)
	# link for font for tesing.
	mkdir -p share/fonts/truetype/ttf-dejavu/
	ln -sf /usr/share/fonts/truetype/ttf-dejavu/DejaVuSans.ttf share/fonts/truetype/ttf-dejavu/
	# Link terralib
	[ -e src/terralib ] || ln -sf  /usr/include/terralib src/terralib
	# Now build. LDFLAGS is a nasty hack past libtool breakage in install, where it fails
	# to find libraries _it_ created. And it makes stuff in the install step. bah.
	# autoreconf -if
	dh_auto_configure -- \
		--enable-bufr --enable-static --enable-cairo --with-emos-libraries=/usr/lib \
		--with-terralib=/usr  --enable-python \
		LDFLAGS="-L$(shell pwd)/src/.libs -Wl,--as-needed"  LIBS=" -lX11 -lm -lquadmath -lgfortran -lpng12 -lz"

override_dh_auto_test check:
	echo "Tests disabled; see README-testfailures.txt for details"
	# (ulimit -s 8192; export PYTHONPATH=$(shell pwd)/swig ; export MAGPLUS_HOME=$(shell pwd) ; $(MAKE) check )
	
override_dh_auto_install:
	chrpath -d swig/*.so
	dh_auto_install
	# call configure to rebuild swig/Makefile with correct python version.
	for pyvers in $(shell pyversions -r) ; do \
		dh_auto_configure -- \
			--enable-bufr --enable-static --enable-cairo --with-emos-libraries=/usr/lib \
			--with-terralib=/usr  \
			--enable-python PYTHON=$$pyvers \
			LDFLAGS="-L$(shell pwd)/src/.libs -Wl,--as-needed"  ; \
		$(MAKE) -C swig clean install PYTHON_INCLUDE_DIR=/usr/include/$$pyvers \
			DESTDIR=$(CURDIR)/debian/tmp \
			MAGPLUS_HOME=$(shell pwd) ; done 
	dh_python2 
	dh_numpy -p python-magics++
	# remove fonts that are present in dejavu-ttf
	rm -rf debian/libmagics++-data/usr/share/magics/ttf
	find debian -name '*.so' -exec chrpath -d {} \;
	# Deal with links for Multi-Arch:
	dh_link -p libmagics++-dev /usr/lib/$(DEB_HOST_MULTIARCH)/libMagPlus.so.3.0.0    /usr/lib/$(DEB_HOST_MULTIARCH)/libMagPlus.so
	dh_link -p libmagplus3     /usr/lib/$(DEB_HOST_MULTIARCH)/libMagPlus.so.3.0.0    /usr/lib/$(DEB_HOST_MULTIARCH)/libMagPlus.so.3
	mkdir -p libmagics++-dev/debian/usr/lib/$(DEB_HOST_MULTIARCH)/pkgconfig
