#!/usr/bin/make -f
include /usr/share/dpkg/pkg-info.mk
include /usr/share/dpkg/architecture.mk
export DEB_BUILD_MAINT_OPTIONS = hardening=+all
include /usr/share/dpkg/buildflags.mk

export JAVA_HOME = /usr/lib/jvm/default-java

# Upstream version without the +ds repack suffix.
UPVER = $(firstword $(subst +, ,$(DEB_VERSION_UPSTREAM)))
# Upstream computes the version and the jar timestamps from git with the
# nisse Maven extension (.mvn, excluded from the orig); give them here.
# (debian/patches/0002 also writes the version into the POMs that are
# installed.)
BUILD_DATE = $(shell date -u -d @$(SOURCE_DATE_EPOCH) +%Y-%m-%dT%H:%M:%SZ)
MVN_ARGS = -Dnisse.jgit.dynamicVersion=$(UPVER) -Dnisse.jgit.dateIso8601=$(BUILD_DATE)

# Only the modules the Scala 3 REPL uses, and what they need.
JLINE_MODULES = native,terminal,terminal-jni,reader

# The JNI library, built from its C sources (the prebuilt ones are
# excluded from the orig). It goes to a jline4 subdirectory of the JNI
# directory, where the loader looks first (debian/patches/0001), because
# libjline3-jni installs a library with the same name.
NATIVE_SRC = native/src/main/native
NATIVE_DIR = usr/lib/$(DEB_HOST_MULTIARCH)/jni/jline4

%:
	dh $@

override_dh_auto_configure:
	dh_auto_configure -- $(MVN_ARGS)

override_dh_auto_build-arch:
	mkdir -p debian/native
	$(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/libjlinenative.so -lutil

override_dh_auto_build-indep:
	dh_auto_build -- $(MVN_ARGS) -pl $(JLINE_MODULES) package

override_dh_auto_test:

override_dh_auto_install-arch:
	install -D -m644 debian/native/libjlinenative.so \
	  debian/libjline4-jni/$(NATIVE_DIR)/libjlinenative.so

override_dh_auto_install-indep:
	dh_auto_install -- $(MVN_ARGS)

execute_after_dh_auto_clean:
	rm -rf debian/native
