#!/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 (release/...)
# If this should make any sense the local upstream branch must track upstream's
# master or whatever other source branch.
gitver = $(shell [ -x /usr/bin/git ] && git describe --tags --match 'v[0-9].[0-9]*.[0-9]*' $$(git merge-base -a HEAD master) | sed -e 's,^v,,' -e 's/-/+git/')

export DH_VERBOSE = 1

hostarch=$(shell dpkg-architecture -qDEB_HOST_ARCH)
# guarantee the same precision on x86 platforms for tests
ifneq (,$(findstring z$(hostarch)z,zamd64z zi386z))
    CFLAGS+=-mmmx -msse -msse2 -mfpmath=sse
endif

# On non-amd64 tests segfault with openjpeg, so we enable it only on amd64
USE_OPENJPEG=$(shell dpkg --print-architecture | grep -q amd64 && echo ON || echo OFF)

# Not exactly sure on where/when it would build, but we see it fail
# on jessie with g++ 4:4.9.2-2
USE_JPEGLS = $(shell dpkg --compare-versions `dpkg -l g++ | awk '/^ii/{print $3;}'` ge 4:4.10 && echo ON || echo OFF)

export  http_proxy=http://127.0.0.1:9/
export  https_proxy=http://127.0.0.1:9/

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

override_dh_auto_configure:
	g++ --version  # Just FOI
	dh_auto_configure -- \
	  -DUSE_STATIC_RUNTIME:BOOL=OFF \
	  -DUSE_SYSTEM_TURBOJPEG:BOOL=ON \
      -DUSE_OPENJPEG:BOOL=$(USE_OPENJPEG) \
	  -DUSE_SYSTEM_ZLIB:BOOL=ON \
	  -DUSE_JPEGLS:BOOL=$(USE_JPEGLS) \
	  -DBATCH_VERSION:BOOL=ON \
      -DCMAKE_C_FLAGS:STRING="${CFLAGS}" \
      -DCMAKE_CXX_FLAGS:STRING="${CFLAGS}" \
      -DCMAKE_VERBOSE_MAKEFILE:BOOL=ON


override_dh_auto_install:
	dh_auto_install
	# Run tests using built/installed dcm2niix
	debian/run_tests || :

override_dh_clean:
	dh_clean
	# remove stuff we don't carry in the upstream tarball
	rm -rf dcm2laz qtGui wxWidgets xcode *.command console/unused dcm_qa*/Out

# make orig tarball from repository content
get-orig-source:
	gbp buildpackage --git-verbose -S

# check that DSC patches still apply
maint-check-dsc-patches:
	@for p in debian/patches/*-dsc-patch; \
		do echo "check $$p"; \
		patch -p1 --dry-run < $$p || exit 1 ; \
	done
