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

srcpkg = $(shell LC_ALL=C dpkg-parsechangelog | grep '^Source:' | cut -d ' ' -f 2,2)
debver = $(shell LC_ALL=C dpkg-parsechangelog | grep '^Version:' | cut -d ' ' -f 2,2 )
upstreamver = $(shell echo $(debver) | cut -d '-' -f 1,1 )

# this figures out the last merge point from 'master' into the Debian branch and
# then described this commit relative to the last release tag
# If this should make any sense the local master branch must track upstream's
# master or whatever other source branch.
gitver = $(shell [ -x /usr/bin/git ] && git describe --tags --match '*\.*\.*' $$(git merge-base -a HEAD master) | sed -e 's/_/./g' -e 's/-/+git/')


# one ring to rule them all ...
%:
	dh $@ --buildsystem=cmake

# this enables everything that upstream considers to be ready for production
# currently tests only get built but do not run as the test suite assumes
# an already installed package -- upstream is working on a fix
CMAKE_FLAGS=-DCMAKE_INSTALL_PREFIX:PATH=/usr \
            -DPYTHON_VERSIONS:LIST="$$(pyversions -s | sed -e 's/ /;/g')" \
            -DBUILD_SHARED_LIBS:BOOL=ON \
            -DCMAKE_SHARED_LINKER_FLAGS="-Wl,--no-undefined" \
            -DCMAKE_EXE_LINKER_FLAGS="-Wl,--no-undefined" \
            -DISIS_APPS_CALC:BOOL=ON \
            -DISIS_BUILD_TESTS:BOOL=OFF \
            -DISIS_BUILD_TOOLS:BOOL=ON \
            -DISIS_BUILD_PYTHON_EXT:BOOL=ON \
            -DISIS_IOPLUGIN_DICOM:BOOL=ON \
            -DISIS_IOPLUGIN_GZ:BOOL=ON \
            -DISIS_IOPLUGIN_NIFTI:BOOL=OFF \
            -DISIS_IOPLUGIN_NIFTI_SA:BOOL=ON \
            -DISIS_IOPLUGIN_NULL:BOOL=ON \
            -DISIS_IOPLUGIN_PNG:BOOL=ON \
            -DISIS_IOPLUGIN_RAW:BOOL=ON \
            -DISIS_IOPLUGIN_TAR:BOOL=ON \
            -DISIS_IOPLUGIN_VISTA:BOOL=ON \
            -DISIS_IOPLUGIN_FLIST:BOOL=ON \
            -DISIS_IOPLUGIN_PROCESS:BOOL=ON \
            -DISIS_ITK:BOOL=OFF \
            -DISIS_QT4:BOOL=ON \
            -DISIS_USE_LIBOIL:BOOL=ON \
            -DISIS_RUNTIME_LOG:BOOL=ON \
            -DISIS_BUILD_PYTHON_EXT:BOOL=ON \
            -DISIS_DEBUG_LOG:BOOL=OFF \
            -DMANPAGES:BOOL=ON \
            -DGIT_REVISION="Debian-$(debver)"

override_dh_auto_test:
	# don't work ATM

override_dh_auto_configure:
	dh_auto_configure -- $(CMAKE_FLAGS)
# TODO compile for mutliple python versions
# dh_auto_configure --builddirectory=build-$*-dbg -- \
#        -DCMAKE_BUILDTYPE:STRING="Debug" \
#        -DCMAKE_SKIP_RPATH=true \
#        -DCMAKE_PYTHON_VERSION=$*-dbg \
#        -DCMAKE_PYTHONLIB_VERSION="$*_d;$*" \

# ignore the plugins, as they are for private consumption by the binaries in
# this package only
override_dh_makeshlibs:
	dh_makeshlibs -XlibisisImageFormat

get-orig-source:
	# orig tarball, turn directory into something nicer
	git archive --format=tar --prefix=$(srcpkg)-$(gitver)/ HEAD | \
		gzip -9 > $(srcpkg)_$(gitver).orig.tar.gz
