#!/usr/bin/make -f

LDFLAGS:=$(shell dpkg-buildflags --get LDFLAGS)

# attempt to fix/workaround #502083
AS_NEEDED_BROKEN = zarmelz zarmebz
ifeq (,$(findstring z$(DEB_HOST_ARCH)z,$(AS_NEEDED_BROKEN)))
LDFLAGS += -Wl,--as-needed -Wl,--no-undefined
endif

# The CMake flags for Debian:
# - Choose Qt3 explicitely (necessary as building fails with Qt4 present).
# - Search for VTK instead of a fixed path to be compatible with post-etch
#   versions and Ubuntu.

# add Debian-specific include paths
CXXFLAGS += -I$(CURDIR)/fsl/libprob \
            -I/usr/include/newmat \
            -I/usr/include/qwt-qt4 \
            -I/usr/include/nifti
CFLAGS += $(CXXFLAGS)
export CXXFLAGS
export CFLAGS


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


override_dh_auto_configure:
	dh_auto_configure -- \
      -DCMAKE_EXE_LINKER_FLAGS:STRING="$(LDFLAGS)" \
      -DCMAKE_MODULE_LINKER_FLAGS:STRING="$(LDFLAGS)" \
      -DCMAKE_SHARED_LINKER_FLAGS:STRING="$(LDFLAGS)" \
      -DVTK_DIR:PATH=$$(dirname $$(find /usr/lib -name VTKConfig.cmake|head -n1)) \
      -DDESIRED_QT_VERSION=4 \
      -DQT_MOC_EXECUTABLE=/usr/bin/moc-qt4 \
      -DQT_UIC_EXECUTABLE=/usr/bin/uic-qt4 \
      -DQWT_DIR=/usr \
      -DFSL_DIR=$(CURDIR)/fsl \
      -DFSL_BUILD:BOOL=ON


override_dh_auto_install::
	dh_auto_install
	# make fsl icon
	convert doc/fslview/fsl-logo.png -resize x32 -crop 32x32+5+0 \
		debian/tmp/fslview.xpm


override_dh_auto_test::
	# disabled because tests rely on FSL environment which is provided by a
	# non-free package
	# but we at least fire up the binary
	debian/run_tests ./obj*/bin/fslview 10 ctrl+q


# do not compress the html docs as they are used as online help sources
# for FSLView via the Qt-Assistant
override_dh_compress:
	dh_compress -X.html -X.png -X.adp -X.jpg
