#!/usr/bin/make -f

# DH_VERBOSE := 1

export DEB_LDFLAGS_MAINT_APPEND = -Wl,--as-needed

# CamiTK version from d/changelog)
CHANGELOG_VERSION := $(shell dpkg-parsechangelog | sed -n -e 's/^Version: //p')
FULL_VERSION := $(shell echo '$(CHANGELOG_VERSION)' | sed -e 's/^[[:digit:]]*://' -e 's/[~-].*//')
VER_MAJOR := $(shell echo '$(FULL_VERSION)' | cut -f1 -d".")
VER_MINOR := $(shell echo '$(FULL_VERSION)' | cut -f2 -d".")
VER_SHORT=$(VER_MAJOR).$(VER_MINOR)

# package aliases (see d/control)
pkg_lib = libcamitk$(VER_MAJOR)
pkg_qpb = libqtpropertybrowser$(VER_MAJOR)
pkg_dev = libcamitk$(VER_MAJOR)-dev
pkg_qpb_dev = libqtpropertybrowser$(VER_MAJOR)-dev
pkg_data = libcamitk$(VER_MAJOR)-data
pkg_doc = libcamitk$(VER_MAJOR)-doc
pkg_imp = camitk-imp
pkg_asm = camitk-actionstatemachine

# dpkg-shlibdeps needs to know about camitk extension directories to manage inner-dependencies
# between extensions (i.e., mml component extension depends on physicalmodel component extensions)
PKG_LIB_DIR := $(CURDIR)/debian/${pkg_lib}/usr/lib/camitk-${VER_SHORT}
# for component extensions:
LD_LIBRARY_PATH := $(LD_LIBRARY_PATH):$(PKG_LIB_DIR)/components/
# for action extensions: 
LD_LIBRARY_PATH := $(LD_LIBRARY_PATH):$(PKG_LIB_DIR)/actions/
# for private libraries:
LD_LIBRARY_PATH := $(LD_LIBRARY_PATH):$(PKG_LIB_DIR)/
# during test, the build dir path is also needed
LIB_BUILD_DIR := $(CURDIR)/camitk-build/lib
PRIVATE_LIB_BUILD_DIR := $(LIB_BUILD_DIR)/camitk-${VER_SHORT}
LD_LIBRARY_PATH := $(LD_LIBRARY_PATH):$(LIB_BUILD_DIR):$(PRIVATE_LIB_BUILD_DIR)/components/:$(PRIVATE_LIB_BUILD_DIR)/actions/:$(PRIVATE_LIB_BUILD_DIR)/
# export for all d/rules subshell (test and dpkg-shlibdeps included)
export LD_LIBRARY_PATH

# CMake flags are of two types:
# - generic (to tell cmake to build proper binaries)
# - CamiTK specific options
# Notes:
# - if you would like a more verbose camitk at run-time, you can add -DCAMITK_LOG_LEVEL=2
# - translation is not ready yet, so disable for now
CMAKE_EXTRA_FLAGS = \
	-DCMAKE_SKIP_RPATH:BOOL=TRUE \
	-DCMAKE_INSTALL_RPATH_USE_LINK_PATH:BOOL=FALSE \
	-DCMAKE_BUILD_TYPE:STRING=None \
	\
	-DCEP_IMAGING:BOOL=TRUE \
	-DCEP_MODELING:BOOL=TRUE \
	\
	-DAPIDOC_SDK:BOOL=TRUE \
	-DCAMITK_DISABLE_TRANSLATION=TRUE

%:
	# For debian build, do not authorize parallel build (too much RAM needed for Imaging CEP)
	dh $@ --builddirectory=camitk-build
	# For local personal build, parallel is ok as there is enough RAM for heavy-templated Imaging CEP
	#dh $@ --parallel --builddirectory=camitk-build  

override_dh_auto_configure:
	dh_auto_configure -- $(CMAKE_EXTRA_FLAGS)

# let's be smart with doc
override_dh_auto_build-indep:
	$(MAKE) -C camitk-build apidoc
	# remove the javascript search field
	rm -f camitk-build/share/camitk-$(VER_SHORT)/apidoc/html/jquery.js

# No tests needed for docs
override_dh_auto_test-indep:

# a selection of relevant tests
override_dh_auto_test:
	# Use the CamiTK test suite
	# Note: all tests require an X server, xvfb-run is needed to have a virtual one
	# Another way: xvfb-run --auto-servernum $(MAKE) -C camitk-build ARGS="-V" test
	(cd camitk-build && xvfb-run --auto-servernum ctest -V --timeout 300)

# make packages
override_dh_install:
	# $(CURDIR) is the current source dir
	# camitk dynamic library
	dh_install -p$(pkg_lib) --autodest debian/tmp/usr/lib/libcamitkcore.so.*
	dh_install -p$(pkg_lib) --autodest debian/tmp/usr/lib/camitk-$(VER_SHORT)/lib*.so.*
	dh_install -p$(pkg_lib) --autodest debian/tmp/usr/lib/camitk-$(VER_SHORT)/actions/lib*.so.*
	dh_install -p$(pkg_lib) --autodest debian/tmp/usr/lib/camitk-$(VER_SHORT)/components/lib*.so.*
	# camitk-config is part of libcamitk
	dh_install -p$(pkg_lib) --autodest debian/tmp/usr/bin/camitk-config
	dh_install -p$(pkg_lib) --autodest debian/tmp/usr/share/pixmaps/camitk-config.xpm
	dh_installman -p$(pkg_lib) debian/tmp/usr/share/man/man1/camitk-config.1  

	# separate packaging for qtpropertybrowser (can be used independently from camitk)
	dh_install -p$(pkg_qpb) debian/tmp/usr/lib/libqtpropertybrowser.so.* usr/lib

	# camitk developer package
	# camitk so and headers and static libs 
	dh_install -p$(pkg_dev) debian/tmp/usr/lib/libcamitkcore.so usr/lib 
	dh_install -p$(pkg_dev) --autodest debian/tmp/usr/lib/camitk-$(VER_SHORT)/lib*.so
	dh_install -p$(pkg_dev) --autodest debian/tmp/usr/lib/camitk-$(VER_SHORT)/actions/lib*.so
	dh_install -p$(pkg_dev) --autodest debian/tmp/usr/lib/camitk-$(VER_SHORT)/components/lib*.so
	dh_install -p$(pkg_dev) --autodest debian/tmp/usr/lib/camitk-$(VER_SHORT)/lib*.a
	dh_install -p$(pkg_dev) --autodest debian/tmp/usr/include/camitk-${VER_SHORT}/libraries/camitkcore
	dh_install -p$(pkg_dev) --autodest debian/tmp/usr/include/camitk-${VER_SHORT}/libraries/cepcoreschema
	dh_install -p$(pkg_dev) --autodest debian/tmp/usr/include/camitk-${VER_SHORT}/libraries/cepgenerator
	dh_install -p$(pkg_dev) --autodest debian/tmp/usr/include/camitk-${VER_SHORT}/actions
	dh_install -p$(pkg_dev) --autodest debian/tmp/usr/include/camitk-${VER_SHORT}/components
	dh_install -p$(pkg_dev) --autodest debian/tmp/usr/include/camitk-${VER_SHORT}/libraries/pml
	dh_install -p$(pkg_dev) --autodest debian/tmp/usr/include/camitk-${VER_SHORT}/libraries/lml
	dh_install -p$(pkg_dev) --autodest debian/tmp/usr/include/camitk-${VER_SHORT}/libraries/mml
	dh_install -p$(pkg_dev) --autodest debian/tmp/usr/include/camitk-${VER_SHORT}/libraries/monitoring
	dh_install -p$(pkg_dev) --autodest debian/tmp/usr/include/camitk-${VER_SHORT}/libraries/monitoringgui
	dh_install -p$(pkg_dev) --autodest debian/tmp/usr/share/camitk-$(VER_SHORT)/cmake/*.cmake 
	dh_install -p$(pkg_dev) --autodest debian/tmp/usr/share/camitk-$(VER_SHORT)/cmake/macros/*.cmake 
	# wizard
	dh_install -p$(pkg_dev) --autodest debian/tmp/usr/bin/camitk-wizard
	dh_installman -p$(pkg_dev) debian/tmp/usr/share/man/man1/camitk-wizard.1
	dh_install -p$(pkg_dev) --autodest debian/tmp/usr/share/applications/camitk-wizard.desktop
	dh_install -p$(pkg_dev) --autodest debian/tmp/usr/share/pixmaps/camitk-wizard.xpm
	# cepgenerator
	dh_install -p$(pkg_dev) --autodest debian/tmp/usr/bin/camitk-cepgenerator
	dh_installman -p$(pkg_dev) debian/tmp/usr/share/man/man1/camitk-cepgenerator.1
	# test programs
	dh_install -p$(pkg_dev) --autodest debian/tmp/usr/bin/camitk-testactions
	dh_installman -p$(pkg_dev) debian/tmp/usr/share/man/man1/camitk-testactions.1
	dh_install -p$(pkg_dev) --autodest debian/tmp/usr/bin/camitk-testcomponents
	dh_installman -p$(pkg_dev) debian/tmp/usr/share/man/man1/camitk-testcomponents.1

	# qtpropertybrowser so and headers (includes are put directly in usr/include as package should be generic/independant from CamiTK)
	dh_install -p$(pkg_qpb_dev) debian/tmp/usr/lib/libqtpropertybrowser.so usr/lib
	dh_install -p$(pkg_qpb_dev) debian/tmp/usr/include/camitk-${VER_SHORT}/libraries/qtpropertybrowser usr/include

	# api doc
	dh_install -p$(pkg_doc) --autodest debian/tmp/usr/share/camitk-$(VER_SHORT)/apidoc

	# all test data
	dh_install -p$(pkg_data) --autodest debian/tmp/usr/share/camitk-$(VER_SHORT)/testdata

	# imp
	dh_install -p$(pkg_imp) --autodest debian/tmp/usr/bin/camitk-imp
	dh_install -p$(pkg_imp) --autodest debian/tmp/usr/share/applications/camitk-imp.desktop
	dh_install -p$(pkg_imp) --autodest debian/tmp/usr/share/pixmaps/camitk-imp.xpm
	dh_installman -p$(pkg_imp) debian/tmp/usr/share/man/man1/camitk-imp.1

	# asm
	dh_install -p$(pkg_asm) --autodest debian/tmp/usr/bin/camitk-actionstatemachine
	dh_install -p$(pkg_asm) --autodest debian/tmp/usr/share/pixmaps/camitk-actionstatemachine.xpm
	dh_installman -p$(pkg_asm) debian/tmp/usr/share/man/man1/camitk-actionstatemachine.1

override_dh_builddeb:
	dh_builddeb -- -Z xz

# see http://lists.debian.org/debian-mentors/2012/07/msg00124.html
get-orig-source:
	mkdir -p ../tarballs
	PERL_LWP_SSL_VERIFY_HOSTNAME=0 uscan --rename --verbose --force-download --destdir=../tarballs
