#!/usr/bin/make -f

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

%:
	dh $@ --with python2,autoreconf

CFLAGS += -fPIC
VERSION:=2.14.5

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_autoreconf:
	# remove out of date files
	rm -f conf/config.guess conf/config.sub
	libtoolize -c -f
	dh_autoreconf --as-needed

override_dh_auto_configure:
	# 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 -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 \
                ver=`echo $$pyvers | sed -e 's/python//g' ` ; \
		dh_auto_configure -- \
			--enable-bufr --enable-static --enable-cairo --with-emos-libraries=/usr/lib \
			--with-terralib=/usr  \
			--enable-python PYTHON=$$pyvers PYTHON_VERSION=$$ver \
			LDFLAGS="-L$(shell pwd)/src/.libs -Wl,--as-needed"  ; \
		$(MAKE) -C swig clean install PYTHON_INCLUDE_DIR=/usr/include/$$pyvers \
			DESTDIR=$(CURDIR)/debian/python-magics++ \
			MAGPLUS_HOME=$(shell pwd) ; done 
	$(MAKE) -C tools prefix=$(shell pwd)/debian/tmp/usr install
	cp tools/mapgen_clip debian/tmp/usr/bin
	find debian -name '*.la' -delete
	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 debian/libmagics++-dev/usr/lib/$(DEB_HOST_MULTIARCH)/pkgconfig
	find debian -name '*.la' -delete 
