#!/usr/bin/make -f

VER_MAJOR = 4
VER_MINOR = 13
VER_REVISION = 3
VERSION = $(VER_MAJOR).$(VER_MINOR).$(VER_REVISION)
SOVERSION = $(VER_MAJOR).$(VER_MINOR)
PYVER    := $(shell py3versions -dv)

#ITK is not properly using system provided headers
export DEB_CPPFLAGS_MAINT_APPEND = -I/usr/include/nifti -g1

ENABLE_PYTHON=0

ifeq ($(DEB_HOST_ARCH),i386)
  ENABLE_FFTW=OFF
#(unsigned long) == (unsigned int) on 32 bit, since itk 4.10 or g++-5.4
  ENABLE_UNSIGNED_LONG_LONG=OFF
  # Tests are permanently failing, try force rounding to normal double
  # accuracy
  export DEB_CXXFLAGS_MAINT_APPEND=-ffloat-store
else
  ENABLE_FFTW=ON
  ENABLE_UNSIGNED_LONG_LONG=ON
endif

CMAKE_FLAGS = \
	-DBUILD_EXAMPLES:BOOL=ON \
	-DBUILD_SHARED_LIBS:BOOL=ON \
	-DBUILD_TESTING:BOOL=ON \
	-DExternalData_OBJECT_STORES:PATH=$(CURDIR)/data/.ExternalData/ \
	-DCMAKE_EXPORT_COMPILE_COMMANDS:BOOL=ON \
	-DCMAKE_SKIP_RPATH:BOOL=ON \
	-DCMAKE_VERBOSE_MAKEFILE:BOOL=ON \
	-DUSE_FFTWF=$(ENABLE_FFTW) \
	-DITK_FORBID_DOWNLOADS:BOOL=ON \
	-DITK_USE_FFTWD:BOOL=$(ENABLE_FFTW) \
	-DITK_USE_FFTWF:BOOL=$(ENABLE_FFTW) \
	-DITK_USE_STRICT_CONCEPT_CHECKING:BOOL=ON \
	-DITK_USE_SYSTEM_DCMTK:BOOL=ON \
	-DITK_USE_SYSTEM_DOUBLECONVERSION:BOOL=ON \
	-DITK_USE_SYSTEM_EXPAT:BOOL=ON \
	-DITK_USE_SYSTEM_FFTW:BOOL=$(ENABLE_FFTW) \
	-DITK_USE_SYSTEM_GDCM:BOOL=ON \
	-DITK_USE_SYSTEM_GOOGLETEST:BOOL=ON \
        -DGTEST_SRC=/usr/src/gtest \
	-DITK_USE_SYSTEM_MINC:BOOL=ON \
	-DITK_USE_SYSTEM_HDF5:BOOL=ON \
	-DITK_USE_SYSTEM_JPEG:BOOL=ON \
	-DITK_USE_SYSTEM_PNG:BOOL=ON \
	-DITK_USE_SYSTEM_TIFF:BOOL=OFF \
	-DITK_USE_SYSTEM_VXL:BOOL=OFF \
	-DITK_USE_SYSTEM_ZLIB:BOOL=ON \
	-DITK_USE_KWSTYLE:BOOL=OFF \
	-DModule_ITKDCMTK:BOOL=ON \
	-DModule_ITKDeprecated:BOOL=ON \
	-DModule_ITKIOPhilipsREC:BOOL=OFF \
	-DModule_ITKLevelSetsv4Visualization:BOOL=OFF \
        -DModule_BridgeNumPy:BOOL=OFF \
	-DModule_ITKReview:BOOL=ON \
	-DModule_ITKVideoBridgeOpenCV:BOOL=OFF \
	-DModule_ITKVideoBridgeVXL:BOOL=OFF \
	-DModule_ITKVtkGlue:BOOL=OFF \
	-DITKV3_COMPATIBILITY:BOOL=ON \
	-DVCL_INCLUDE_CXX_0X:BOOL=ON \
	-DITK_USE_SYSTEM_SWIG:BOOL=ON \
	-DITK_USE_SYSTEM_CASTXML:BOOL=ON \
	-DITK_WRAP_IMAGE_DIMS="2;3" \
	-DITK_WRAP_unsigned_long_long:BOOL=$(ENABLE_UNSIGNED_LONG_LONG) \
	-DITK_WRAP_double:BOOL=ON \
        -DITK_WRAP_vector_double:BOOL=ON \
        -DITK_WRAP_covariant_vector_double:BOOL=ON \
        -DITK_WRAP_complex_double:BOOL=ON \
        -DITK_WRAPPING:BOOL=$(ENABLE_PYTHON) \
        -DITK_WRAP_PYTHON:BOOL=$(ENABLE_PYTHON)

pkg_lib = libinsighttoolkit$(SOVERSION)
pkg_dev = libinsighttoolkit$(VER_MAJOR)-dev
pkg_examples = insighttoolkit$(VER_MAJOR)-examples
pkg_python = insighttoolkit$(VER_MAJOR)-python3


%:
	dh $@ --builddir=BUILD

override_dh_auto_configure-indep:

override_dh_auto_configure-arch: pre-build
	dh_auto_configure -- $(CMAKE_FLAGS)

override_dh_auto_build-indep:

pre-build:
	echo "Available disk space (~50GB of space is needed for the build):"
	df -h .

override_dh_auto_test-indep:

override_dh_auto_test:
	LD_LIBRARY_PATH=`pwd`/BUILD/lib:`pwd`/BUILD/Testing/Temporary dh_auto_test || \
    ( echo "Test log begin =========:"  \
    && cat `pwd`/BUILD/Testing/Temporary/LastTest.log \
    && echo "Test log end =========:" \
    && exit 1 )

override_dh_lintian:
	echo "#ITK embeds various patched libraries" > debian/$(pkg_lib).lintian-overrides
	echo "$(pkg_lib): embedded-library" > debian/$(pkg_lib).lintian-overrides
	echo "$(pkg_lib): package-name-doesnt-match-sonames" >> debian/$(pkg_lib).lintian-overrides
	dh_lintian

override_dh_install-indep:

override_dh_install-arch:
	# pkg_lib
	dh_install -p$(pkg_lib) -XJava -XPython -XTcl debian/tmp/usr/lib/lib*.so.* usr/lib
	cp CMake/InsightValgrind.supp Insight$(SOVERSION)Valgrind.supp
#	cp Modules/ThirdParty/VNL/src/vxl/config/valgrind.supp Insight$(SOVERSION)VxlValgrind.supp
	dh_install -p$(pkg_lib) Insight$(SOVERSION)*.supp usr/lib/valgrind
	#
	# pkg_dev
	dh_installman -p$(pkg_dev) debian/insighttoolkit.3
	dh_install -p$(pkg_dev) -XJava -XPython -XTcl debian/tmp/usr/lib/lib*.so usr/lib

	dh_install -p$(pkg_dev) --autodest debian/tmp/usr/include/ITK-$(SOVERSION)/*
	dh_install -p$(pkg_dev) --autodest debian/tmp/usr/bin/itkTestDriver

ifeq ($(ENABLE_PYTHON),1)
	# remove wrapping related cmake files, and tools. They should not be needed.
	rm -r debian/tmp/usr/lib/cmake/ITK-$(SOVERSION)/WrapITK
endif
	dh_install -p$(pkg_dev) --autodest debian/tmp/usr/lib/cmake
#	dh_install -p$(pkg_dev) Wrapping/WrapITK /usr/src
        #
        # pkg_python
ifeq ($(ENABLE_PYTHON),1)
	dh_install -p$(pkg_python) debian/tmp/usr/lib/python*/* usr/lib/python$(PYVER)/
endif
	# remove build directory to save space
	rm -rf BUILD debian/tmp

override_dh_installexamples:
	# pkg_examples
	dh_installexamples -p$(pkg_examples) Examples/*
ifneq (,$(filter $(pkg_examples), $(shell dh_listpackages)))
	find debian/$(pkg_examples)/usr/share/doc/$(pkg_examples)/examples \
	    -type f -print0 | xargs -0 chmod 644
endif

override_dh_clean:
	# Modules/ThirdParty/pygccxml/src/pygccxml leaves build artifacts around
	find  Modules/ThirdParty/pygccxml/src -name __pycache__ -execdir rm -r '{}' +
	dh_clean

control-file:
	sed -e "s/@VER_MAJOR@/$(VER_MAJOR)/g" \
	    -e "s/@SOVERSION@/$(SOVERSION)/g" \
	    < debian/control.in > debian/control.tmp
	[ -e debian/control ] \
	  && cmp -s debian/control debian/control.tmp \
	  && rm -f debian/control.tmp && exit 0; \
	  mv debian/control.tmp debian/control

TARBALLDIR = "../tarballs"
get-orig-source:
	uscan --watchfile debian/data/watch --force-download --no-symlink --destdir $(TARBALLDIR)
	uscan --force-download --rename --destdir $(TARBALLDIR)
	cd $(TARBALLDIR) && mv `ls InsightData-$(VERSION).tar.gz` insighttoolkit$(VER_MAJOR)_$(VERSION)-dfsg2.orig-data.tar.gz
