#!/usr/bin/make -f

# To enable all, uncomment following line
DEB_BUILD_MAINT_OPTIONS:= hardening=+allx
DEB_CFLAGS_MAINT_APPEND:= -Wall -pedantic
export DEB_BUILD_MAINT_OPTIONS
export DEB_CFLAGS_MAINT_APPEND

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

%:
	dh $@  --with python2,autoreconf

CFLAGS += -fPIC

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

# Don't use emoslib on mips
WITH_EMOSLIB:= --with-emos-libraries=/usr/lib --enable-bufr
ifeq ($(ARCH), mips)
  WITH_EMOSLIB:=
endif

override_dh_auto_clean:
	find . -type l -exec rm {} \;
	find . \( -name '*.pyc' -o -name '*.o' -o -name '*.a' \)  -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
	$(MAKE) -k distclean || echo "Already distcleaned"

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-static --enable-cairo $(WITH_EMOSLIB) \
		--enable-python --enable-opengl \
		LDFLAGS="$(LDFLAGS) -L$(shell pwd)/src/.libs -Wl,--as-needed"  LIBS=" -lX11 -lpng12 -lz" \
		MAGPLUS_HOME=$(shell pwd)
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.
	# (cd swig/Magics && ln -sf /usr/share/pyshared/instant/swig/numpy.i )
	for pyvers in $(shell pyversions -r) ; do \
                ver=`echo $$pyvers | sed -e 's/python//g' ` ; \
		dh_auto_configure -- \
			--enable-static --enable-cairo --enable-opengl $(WITH_EMOSLIB) \
			--enable-python PYTHON=$$pyvers PYTHON_VERSION=$$ver \
			LDFLAGS="$(LDFLAGS) -L$(shell pwd)/src/.libs -Wl,--as-needed" ; \
		$(MAKE) -C python/Magics 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
	find debian -name '_Magics.so.0' -delete  # Bogus symlinks
	# Hack. dh_python2 doesn't know multarch yet.
	mv debian/python-magics++/usr/lib/${DEB_HOST_MULTIARCH}/p* debian/python-magics++/usr/lib
	rmdir debian/python-magics++/usr/lib/${DEB_HOST_MULTIARCH}
	dh_python2 
	for pyvers in $(shell pyversions -r) ; do \
    		dh_python2 /usr/lib/$$pyvers/site-packages/Magics ; \
	done
	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 
