#!/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


%:
	dh $@ -Bbuild

override_dh_auto_configure:
	dh_auto_configure -Bbuild -- -DCMAKE_C_FLAGS="$(CFLAGS)" -DCMAKE_CXX_FLAGS="$(CFLAGS) " \
				-DCMAKE_SHARED_LINKER_FLAGS="-Wl,--no-undefined,--as-needed $(LDFLAGS) " \
				-DCMAKE_MODULE_LINKER_FLAGS="-Wl,--no-undefined,--as-needed $(LDFLAGS) " \
				-DCMAKE_EXE_LINKER_FLAGS="-Wl,--no-undefined,--as-needed $(LDFLAGS) " \
				-DCMAKE_BUILD_TYPE:STRING=RelWithDebInfo \
				-DCMAKE_SKIP_RPATH:BOOL=ON -DHAS_FLTO_FLAG:BOOL=OFF

	sh debian/build-doc.sh

override_dh_auto_test:
	$(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_install:
	dh_install
	# check if we are building libsysactivity-doc
	if [ -d debian/libsysactivity-doc ] ; then \
		rm debian/libsysactivity-doc/usr/share/doc/libsysactivity-doc/api/jquery.js ; \
		ln -s ../../../javascript/jquery/jquery.js debian/libsysactivity-doc/usr/share/doc/libsysactivity-doc/api/jquery.js ; \
	fi

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

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