#!/usr/bin/make -f

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

# magic debhelper rule
%:
	dh $@ --with python2,autoreconf

include /usr/share/mpi-default-dev/debian_defaults
#ARCH_DEFAULT_MPI_IMPL=lam
MPI_INC=/usr/lib/$(ARCH_DEFAULT_MPI_IMPL)/include

ifeq ($(ARCH_DEFAULT_MPI_IMPL),lam)
  CC:=mpicc.lam
else
  CC:=mpicc
endif

PYVERS:= `pyversions -s`
DEB_HOST_MULTIARCH ?= $(shell dpkg-architecture -qDEB_HOST_MULTIARCH)

override_dh_autoreconf:
	libtoolize -c -f
	dh_autoreconf --as-needed

override_dh_auto_configure:
	LDFLAGS="$(LDFLAGS) -Wl,--as-needed" dh_auto_configure -- --enable-pythonmodule --enable-silex --with-qt=/usr --enable-pythonmodule --enable-silex --with-qt-bin=/usr/bin --with-qt-lib=/usr/lib  --with-qt-include=/usr/include/qt4 --with-hdf5=/usr/include,/usr/lib CPPFLAGS=" -I$(MPI_INC) $(CPPFLAGS)"  CC=$(CC)  CFLAGS="$(CFLAGS)"

override_dh_auto_build:
	dh_auto_build
	set -e ; for p in $(PYVERS) ; do   \
		 LDFLAGS=" -Wl,--as-needed $(LDFLAGS)" dh_auto_configure -- \
			--enable-pythonmodule --enable-silex \
			--with-qt=/usr --with-qt-bin=/usr/bin --with-qt-lib=/usr/lib \
			--with-qt-include=/usr/include/qt4 \
			 --with-hdf5=/usr/include,/usr/lib PYTHON=$$p CPPFLAGS=" -I$(MPI_INC) $(CPPFLAGS)" CC=$(CC)  CFLAGS="$(CFLAGS)" ; \
		$(MAKE) -C tools/browser CXX=$(CC) ; \
		$(MAKE) -C tools/python ; \
		mkdir -p  debian/tmp/usr/lib/pyshared/$$p ;  \
		cp tools/python/.libs/Silo.so debian/Silo.so.$$p  ; \
	done

override_dh_auto_install:
	dh_auto_install
	# move the python modules to where dh_pysupport will spot them ...
	set -e; for p in $(PYVERS); do \
		mkdir -p debian/python-silo/usr/lib/$$p/site-packages ; \
		chrpath -d debian/Silo.so.$$p ; \
		cp debian/Silo.so.$$p debian/python-silo/usr/lib/$$p/site-packages/Silo.so ; \
	done
	dh_link -p libsiloh5-0 /usr/lib/${DEB_HOST_MULTIARCH}/libsiloh5.so.0.0.0 /usr/lib/${DEB_HOST_MULTIARCH}/libsiloh5.so.0
	dh_link -p libsilo-dev /usr/lib/${DEB_HOST_MULTIARCH}/libsiloh5.so.0.0.0 /usr/lib/${DEB_HOST_MULTIARCH}/libsiloh5.so
	mkdir -p debian/libsilo-dev/usr/lib/${DEB_HOST_MULTIARCH}/pkgconfig
	cp silo.pc debian/libsilo-dev/usr/lib/${DEB_HOST_MULTIARCH}/pkgconfig

override_dh_auto_clean:
	dh_clean
	rm -f debian/Silo.so.* config.log
	-( [ -f Makefile ] && make distclean)
