#!/usr/bin/make -f

export DEB_BUILD_MAINT_OPTIONS = hardening=+all
export DESTDIR=$(CURDIR)/debian/tmp

# Verbose CMake
#export VERBOSE=1

# Workaround until https://bugs.debian.org/cgi-bin/bugreport.cgi?bug=953855 is fixed
#export DEB_CXXFLAGS_MAINT_APPEND = -DSPDLOG_FMT_EXTERNAL -DFMT_HEADER_ONLY

#PKG_SHORT_DESC := $(shell grep 'Description:' debian/control | tail -n 1 | cut -d':' -f2 | sed 's/^\s//')

ifeq ($(filter nocheck,$(DEB_BUILD_PROFILES)),)
  CMAKE_TEST_ARG := -DTILEDB_TESTS=1
else
  CMAKE_TEST_ARG := -DTILEDB_TESTS=0
endif

%:
ifeq ($(filter nodoc,$(DEB_BUILD_PROFILES)),)
	dh $@ --with sphinxdoc
else
	dh $@
endif

override_dh_auto_clean:
	dh_auto_clean
	rm -rf build/ doc/source/__pycache__/
	rm -f doc/source/_sidebar.rst.inc

override_dh_auto_configure:
# 	S3 support needs AWS C++ SDK which is not available in the archive
# 	HDFS support will dlopen libhdfs.so on runtime but can be built with
	dh_auto_configure -- \
	  -DCMAKE_INSTALL_PREFIX=/usr \
	  -DCMAKE_LIBRARY_PATH=$(DEB_HOST_MULTIARCH) \
	  -DTILEDB_S3=0 \
	  -DTILEDB_HDFS=0 \
	  -DTILEDB_TOOLS=0 \
          -DTILEDB_SERIALIZATION=1 \
	  -DTILEDB_WERROR=0 \
	  $(CMAKE_TEST_ARG)

override_dh_auto_build:
	dh_auto_build
ifeq ($(filter nodoc,$(DEB_BUILD_PROFILES)),)
	$(MAKE) -C obj-$(DEB_BUILD_GNU_TYPE) doc
	mkdir -p $(CURDIR)/build
	mv obj-$(DEB_BUILD_GNU_TYPE)/xml $(CURDIR)/build/
	PYTHONPATH=. http_proxy='127.0.0.1:9' sphinx-build -N -bhtml doc/source/ build/html
	rm -rf build/html/.doctrees
endif

override_dh_auto_test:
ifeq ($(filter nocheck,$(DEB_BUILD_PROFILES)),)
#	$(MAKE) -C obj-$(DEB_BUILD_GNU_TYPE)/tiledb tiledb_unit -j`nproc`
#	obj-$(DEB_BUILD_GNU_TYPE)/tiledb/test/tiledb_unit '*string*'
endif

override_dh_auto_install:
	DESTDIR=$(DESTDIR) $(MAKE) -C obj-$(DEB_BUILD_GNU_TYPE)/tiledb install
	chrpath --delete $(CURDIR)/debian/tmp//usr/lib/*/libtiledb.so.*.*
