#!/usr/bin/make -f
# -*- makefile -*-

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

# Don't use Debian-provided flags for now; they slow down the build
CFLAGS :=
CXXFLAGS :=
LDFLAGS :=

%:
	dh $@ 

override_dh_auto_configure:
	mkdir build
	cd build && py3_ver=`python3 -c "import sys; print('%d.%d' % sys.version_info[:2])"` \
	         && cmake .. -DCMAKE_BUILD_TYPE=Release \
	        -DCGAL_DIR=/usr/lib/`uname -m`-linux-gnu/cmake/CGAL/ \
	        -DCMAKE_INSTALL_PYTHONDIR=/usr/lib/python$${py3_ver}/dist-packages \
	        -DCMAKE_INSTALL_PREFIX=/usr \
	        -DCMAKE_INSTALL_DOCDIR=/usr/share/doc/imp \
	        -DIMP_USE_SYSTEM_RMF=on -DIMP_USE_SYSTEM_IHM=on \
	        -DIMP_DISABLED_MODULES=scratch

override_dh_auto_build:
	$(MAKE) -C build

override_dh_install:
	$(MAKE) -C build DESTDIR=$(CURDIR)/debian/tmp install
	# Make sure all Python applications use the system Python in /usr/bin
	perl -pi -e 's@^#!.*python.*$$@#!/usr/bin/python3@' debian/tmp/usr/bin/*
	# Don't distribute example application or dependency
	rm -rf debian/tmp/usr/bin/imp_example_app \
	       debian/tmp/usr/lib/*/libexample* \
	       debian/tmp/usr/include/example*
	dh_install
	dh_missing --fail-missing

execute_after_dh_install:
	# Run basic tests on the installation
	TOPDIR=`pwd`/debian/tmp \
	  && cd tools/nightly-tests/test-install \
	  && py3_ver=`python3 -c "import sys; print('%d.%d' % sys.version_info[:2])"` \
	  && scons path="$${TOPDIR}/usr/bin" \
	           libpath="$${TOPDIR}/usr/lib/`uname -m`-linux-gnu" \
	           cpppath="$${TOPDIR}/usr/include" \
	           pypath="$${TOPDIR}/usr/lib/python$${py3_ver}/dist-packages" \
	           datapath="$${TOPDIR}/usr/share/IMP/" \
	           examplepath="$${TOPDIR}/usr/share/doc/imp/examples/" \
	           python=python3 mock_config=ubuntu-`lsb_release -c -s` \
	           cxxflags="${CXXFLAGS} -I/usr/include/hdf5/serial/ -I/usr/include/eigen3" \
	  && find "$${TOPDIR}" -depth -name __pycache__ -exec rm -rf \{\} \;

override_dh_compress:
	# Don't compress example files, since then they won't work!
	dh_compress -Xexamples
