#!/usr/bin/make -f

# removes noautodbgsym from DEB_BUILD_OPTIONS
JOBS=4
DEB_BUILD_OPTIONS = parallel=$(JOBS)
DEB_BUILD_ARCH := $(shell dpkg-architecture -qDEB_BUILD_ARCH)
DEB_BUILD_MULTIARCH := $(shell dpkg-architecture -qDEB_BUILD_MULTIARCH)

PLBASENAME := swi-prolog
PLBASE     := /usr/lib/$(PLBASENAME)/
JNIDIR     := /usr/lib/$(DEB_BUILD_MULTIARCH)/jni

CMAKE_OPTIONS = \
	-DSWIPL_PACKAGES_QT=OFF \
	-DCMAKE_INSTALL_PREFIX=/usr \
	-DSWIPL_INSTALL_DIR=$(PLBASENAME) \
	-DJAVA_COMPATIBILITY=ON \
	-DCMAKE_BUILD_TYPE=DEB \
	-DJNIDIR=$(JNIDIR)

# List of architectures where -java package should be built.
JAVA_ARCHS = $(shell grep-dctrl -PX swi-prolog-java -nsArchitecture debian/control)

ifeq ($(filter $(DEB_BUILD_ARCH),$(JAVA_ARCHS)),)
    CMAKE_OPTIONS  += -DSWIPL_PACKAGES_JAVA=OFF
else
    JAVA_COMPONENTS = Java_interface
endif

NOX_COMPONENTS = \
	Core_system \
	Core_packages \
	Archive_interface \
	Commandline_editors \
	Perl_regex \
	YAML_support \
	OpenSSL_interface \
	Documentation \
	Examples
X_COMPONENTS = \
	Graphics_subsystem
ODBC_COMPONENTS = \
	ODBC_interface
BDB_COMPONENTS = \
	BerkeleyDB_interface

ifeq ($(shell dpkg-architecture -qDEB_BUILD_ARCH_OS),linux)
    NOX_COMPONENTS += TIPC_networking
else
    CMAKE_OPTIONS  += -DSWIPL_PACKAGES_TIPC=OFF
endif

# install_component component deb_pkg
# env -C build doesn't work on Ubuntu 16.04 or older
define install_component
	cd build && env DESTDIR=$(CURDIR)/debian/$(2) \
	cmake -DCMAKE_INSTALL_COMPONENT=$(1) \
	      -P cmake_install.cmake

endef

# install_components plog_pkg_list deb_pkg
define install_components
	$(foreach component,$(1),$(call install_component,$(component),$(2)))
endef

%:
	dh $@ --parallel

override_dh_auto_configure:
	mkdir build && cd build && cmake $(CMAKE_OPTIONS) ..

override_dh_auto_build:
	cd build && $(MAKE) -j $(JOBS) < /dev/null

override_dh_auto_clean:
	rm -rf build

override_dh_auto_install:
	$(call install_components,$(NOX_COMPONENTS),swi-prolog-nox)
	$(call install_components,$(X_COMPONENTS),swi-prolog-x)
	$(call install_components,$(JAVA_COMPONENTS),swi-prolog-java)
	$(call install_components,$(ODBC_COMPONENTS),swi-prolog-odbc)
	$(call install_components,$(BDB_COMPONENTS),swi-prolog-bdb)

	# This file is generated by swi-prolog-nox postinst (updated by triggers)
	rm debian/swi-prolog-nox/$(PLBASE)/library/INDEX.pl

	# Remove empty directories
	find debian/ -depth -type d -empty -exec rm -rfv {} \;

	# mv $(CURDIR)/debian/swi-prolog-nox/$(PLBASE)/lib/$(DEB_BUILD_ARCH)/libswipl.* $(CURDIR)/debian/swi-prolog-nox/usr/lib/

override_dh_install:

override_dh_builddeb:
	dh_builddeb -- -Zxz

# tell the tests NOT to try to access http://www.swi-prolog.org.  This
# is used in some of the HTTP tests
USE_PUBLIC_NETWORK_TESTS=false
export USE_PUBLIC_NETWORK_TESTS

# we exclude the JPL test (-E jpl:) because embedded Java in Linux is
# currently non-functional. See
# https://stackoverflow.com/questions/44763387/jni-createjavavm-stack-corruption-in-recent-ubuntu-16-04

override_dh_auto_test:
	-cd build && ctest -V -E jpl: -j $(JOBS)

.PHONY: override_dh_auto_configure override_dh_auto_build override_dh_auto_clean override_dh_auto_install
.PHONY: override_dh_install override_dh_builddeb override_dh_auto_test
