#!/usr/bin/make -f

export JAVA_HOME = /usr/lib/jvm/default-java
DEB_HOST_MULTIARCH ?= $(shell dpkg-architecture -qDEB_HOST_MULTIARCH)
export DEB_BUILD_MAINT_OPTIONS = hardening=+all
include /usr/share/dpkg/buildflags.mk

# JLine ships prebuilt JNI binaries (excluded via d/copyright Files-Excluded);
# build libjlinenative.so from the C sources instead. The loader finds it on
# java.library.path (/usr/lib/$(DEB_HOST_MULTIARCH)/jni is on that path).
NATIVE_SRC = native/src/main/native
NATIVE_LIB = libjlinenative.so

%:
	dh $@

execute_before_dh_auto_build:
	$(CC) $(CPPFLAGS) $(CFLAGS) -shared -fPIC -fvisibility=hidden \
	  -I$(JAVA_HOME)/include -I$(JAVA_HOME)/include/linux \
	  $(NATIVE_SRC)/clibrary.c $(NATIVE_SRC)/jlinenative.c \
	  $(LDFLAGS) -o debian/$(NATIVE_LIB) -lutil

# Build the reactor minus groovy (libgroovy not packaged) and the
# graal/demo/curses/jansi-compat modules we do not ship. The jline bundle
# shades console/remote-ssh/remote-telnet/jansi-core, so they must build.
JLINE_MODULES = native,terminal,terminal-ffm,terminal-jansi,terminal-jna,terminal-jni,reader,builtins,console,console-ui,remote-ssh,remote-telnet,style,jansi-core,jline

override_dh_auto_build:
	dh_auto_build -- -pl $(JLINE_MODULES) -am package

override_dh_auto_test:
	# The loader needs the library we just built; /usr/lib/*/jni only holds
	# it once the package is installed, and the pom overrides argLine.
	# FfmTest needs a real pty, and JansiTerminalProviderTest needs the
	# native library of jansi rather than ours.
	LD_LIBRARY_PATH=$(CURDIR)/debian dh_auto_test -- -pl $(JLINE_MODULES) -am \
	  -Dsurefire.failIfNoSpecifiedTests=false \
	  -Dtest='!FfmTest,!JansiTerminalProviderTest#testIsSystemStream'

override_dh_auto_install:
	dh_auto_install
	# Install the JNI native library to the standard Debian JNI location.
	install -D -m644 debian/$(NATIVE_LIB) \
	  debian/libjline3-jni/usr/lib/$(DEB_HOST_MULTIARCH)/jni/$(NATIVE_LIB)

override_dh_auto_clean:
	dh_auto_clean
	rm -f debian/$(NATIVE_LIB)
