#!/usr/bin/make -f

DEB_HOST_ARCH_OS ?= $(shell dpkg-architecture -qDEB_HOST_ARCH_OS)

# Skip data_storage test - requires that /sys is mounted
ifeq ($(DEB_HOST_ARCH_OS),kfreebsd)
    # Also disable process and network on kFreeBSD
    # part of process is not implemented and not sure
    # about the network test.
    DISABLED_TESTS:=^data_storage|disk|process|network\$$
    ARCH=FreeBSD
else
    KERNEL_VERSION:=$(shell uname -r | cut -d '.' -f 1,2,3 | \
			 perl -ne 'print "ok" if m/^(\d+)\.(\d+)(?:\.(\d+))?/ && ($$1 > 2 || $$2 > 6 || $$3 >= 25);')
    # the process tests requires Linux >= 2.6.25
    ifeq ($(KERNEL_VERSION),ok)
        # temporary disabled the process test, since it currently
        # unreliable.
        DISABLED_TESTS:=^data_storage|disk|process\$$
    else
        DISABLED_TESTS:=^data_storage|disk|process\$$
    endif
    ARCH=Linux
endif

DEB_LDFLAGS_MAINT_APPEND=-Wl,--no-add-needed,--as-needed,--no-undefined

include /usr/share/dpkg/buildflags.mk

%:
	dh $@ -Bbuild

override_dh_auto_configure-indep:
	# Do nothing for indep

override_dh_auto_configure-arch:
	dh_auto_configure -Bbuild -- -DCMAKE_C_FLAGS="$(CFLAGS) $(CPPFLAGS)" \
				-DCMAKE_SHARED_LINKER_FLAGS="$(LDFLAGS)" \
				-DCMAKE_EXE_LINKER_FLAGS="$(LDFLAGS)" \
				-DCMAKE_BUILD_TYPE:STRING=RelWithDebInfo \
				-DCMAKE_SKIP_RPATH:BOOL=ON -DHAS_FLTO_FLAG:BOOL=OFF

# This override is not strictly required, but without it debhelper
# will call cmake for the indep build (which is a bit wasteful :P)
override_dh_auto_build-arch:
	dh_auto_build

override_dh_auto_build-indep:
	sh debian/build-doc.sh

override_dh_auto_test-indep:
	# Do nothing for indep

override_dh_auto_test-arch:
	$(MAKE) -C build -f test/Makefile all
	cd build && LD_LIBRARY_PATH="$(CURDIR)/build/src/$(ARCH)" ctest -V --force-new-ctest-process -E "$(DISABLED_TESTS)"

override_dh_strip-arch:
	dh_strip --dbg-package=libsysactivity1-dbg

override_dh_auto_clean-indep:
	# Remove the doc-directories
	rm -fr doc/*/
