#!/usr/bin/make -f
# -*- makefile -*-

include /usr/share/dpkg/pkg-info.mk

# Uncomment this to turn on verbose mode.
#export DH_VERBOSE=1
export DEB_HOST_MULTIARCH ?= $(shell dpkg-architecture -qDEB_HOST_MULTIARCH)

export DEB_BUILD_MAINT_OPTIONS = hardening=+all
DPKG_EXPORT_BUILDFLAGS = 1
include /usr/share/dpkg/buildflags.mk

# For automating the d/*.links stuff.
export LIBPAPPSOMSPP_VERSION=$(DEB_VERSION_UPSTREAM)
export LIBPAPPSOMSPP_SOVERSION=0

ifneq (,$(filter $(DEB_HOST_ARCH_CPU), i386))
  export DEB_CXXFLAGS_MAINT_APPEND += -ffloat-store
endif


MAKE_TESTS=1
BUILD_TESTING=1

# In two different statements:
# ifneq (,$(filter nocheck,$(DEB_BUILD_OPTIONS)))
# 	MAKE_TESTS=0
# 	BUILD_TESTING=0
# endif
#
# ifeq ($(DEB_BUILD_ARCH),s390x)
# 	MAKE_TESTS=0
# 	BUILD_TESTING=0
# endif

# In one single statement:
ifneq (,$(or $(filter nocheck,$(DEB_BUILD_OPTIONS)), $(filter s390x,$(DEB_BUILD_ARCH))))
	MAKE_TESTS=0
	BUILD_TESTING=0
endif


%:
	dh $@ --buildsystem=cmake --builddirectory=build-dir-for-deb --no-parallel

override_dh_auto_configure:
	dh_auto_configure -- \
	  -DCMAKE_INSTALL_PREFIX=/usr \
		-DCMAKE_BUILD_TYPE=Release \
		-DCMAKE_SKIP_RPATH=0 \
		-DUSEPAPPSOTREE=0 \
		-DMAKE_TESTS=$(MAKE_TESTS) \
		-DBUILD_TESTING=$(BUILD_TESTING)

override_dh_auto_clean:
	dh_auto_clean
	rm -f tests/typst/data/*.json

override_dh_makeshlibs:
	dh_makeshlibs -V 'libpappsomspp-core$(LIBPAPPSOMSPP_SOVERSION) (>= $(shell dpkg-parsechangelog -SVersion))'
	dh_makeshlibs -V 'libpappsomspp-gui$(LIBPAPPSOMSPP_SOVERSION) (>= $(shell dpkg-parsechangelog -SVersion))'

override_dh_auto_build:
	dh_auto_build
	dh_auto_build -- doc

override_dh_auto_test:
	if [ $(MAKE_TESTS) = 1 ]; then \
		cd build-dir-for-deb && make catch2-tests; \
		cd ../ && dh_auto_test; \
	fi

