###############################################################################
# libbrlapi - A library providing access to braille terminals for applications.
#
# Copyright (C) 2006-2024 by
#   Samuel Thibault <Samuel.Thibault@ens-lyon.org>
#   Sébastien Hinderer <Sebastien.Hinderer@ens-lyon.org>
#
# libbrlapi comes with ABSOLUTELY NO WARRANTY.
#
# This is free software, placed under the terms of the
# GNU Lesser General Public License, as published by the Free Software
# Foundation; either version 2.1 of the License, or (at your option) any
# later version. Please see the file LICENSE-LGPL for details.
#
# Web Page: http://brltty.app/
#
# This software is maintained by Dave Mielke <dave@mielke.cc>.
###############################################################################

include $(SRC_TOP)bindings.mk

JAVA_OK = @JAVA_OK@
JAVAC = @JAVAC@
JAVADOC = @JAVADOC@
JAR = @JAR@

JAVA_JAR = @JAVA_JAR@
JAVA_JAR_DIR = @JAVA_JAR_DIR@
JAVA_JAR_DESTDIR = $(INSTALL_ROOT)

JAVA_JNI = @JAVA_JNI@
JAVA_JNI_DIR = @JAVA_JNI_DIR@
JAVA_JNI_HDR = @JAVA_JNI_HDR@
JAVA_JNI_INC = @JAVA_JNI_INC@
JAVA_JNI_FLAGS = @JAVA_JNI_FLAGS@
JAVA_JNI_DESTDIR = $(INSTALL_ROOT)

all: jar jni doc

JAVA_SOURCES = Constants.java $(SRC_DIR)/*.java $(SRC_DIR)/*/*.java

JAVA_JNI_FILE = $(LIB_PFX)$(API_NAME)_java.$(LIB_EXT)
jni: $(JAVA_JNI_FILE)

$(JAVA_JNI_FILE): bindings.$O | $(API_NAME)
	$(MKLIB:<name>=$(JAVA_JNI_FILE).$(API_VERSION)) $@ bindings.$O $(API_LDFLAGS)

bindings.$O:
	$(CC) $(LIBCFLAGS) $(JAVA_JNI_FLAGS) -o $@ -c $(SRC_DIR)/bindings.c

JAVA_JAR_FILE = $(API_NAME).jar
jar: $(JAVA_JAR_FILE)

$(JAVA_JAR_FILE): classes.made
	$(JAR) cfm $@ $(SRC_DIR)/manifest -C classes .

JAVAC_LINT_OPTIONS = cast,deprecation,fallthrough,finally,overrides,rawtypes,unchecked

classes.made: $(JAVA_SOURCES)
	$(INSTALL_DIRECTORY) classes
	$(JAVAC) -Xlint:$(JAVAC_LINT_OPTIONS) -d classes -classpath . $^
	touch $@

Constants.java: $(CONSTANTS_DEPENDENCIES)
	$(AWK) $(CONSTANTS_ARGUMENTS) >$@

doc: doc.made

doc.made: $(JAVA_SOURCES)
	$(INSTALL_DIRECTORY) doc
	$(JAVADOC) -quiet -d doc -Xdoclint:none -author -notimestamp -version -use $^
	touch $@

clean::
	-rm -f Constants.java classes.made $(JAVA_JAR_FILE) $(JAVA_JNI_FILE)
	-rm -f -r classes
	-rm -f doc.made
	-rm -f -r doc

install: install-jar install-jni

install-jar: install-jar-$(JAVA_JAR)

install-jar-yes: jar
	$(INSTALL_DIRECTORY) $(JAVA_JAR_DESTDIR)$(JAVA_JAR_DIR)
	$(INSTALL) $(JAVA_JAR_FILE) $(JAVA_JAR_DESTDIR)$(JAVA_JAR_DIR)

install-jar-no:
	@echo jar not installed

install-jni: install-jni-$(JAVA_JNI)

install-jni-yes: jni
	$(INSTALL_DIRECTORY) $(JAVA_JNI_DESTDIR)$(JAVA_JNI_DIR)
	$(INSTALL) $(JAVA_JNI_FILE) $(JAVA_JNI_DESTDIR)$(JAVA_JNI_DIR)

install-jni-no:
	@echo jni not installed

uninstall: uninstall-jar uninstall-jni

uninstall-jar: uninstall-jar-$(JAVA_JAR)

uninstall-jar-yes:
	-rm -f -- $(JAVA_JAR_DESTDIR)$(JAVA_JAR_DIR)/$(JAVA_JAR_FILE)

uninstall-jar-no:

uninstall-jni: uninstall-jni-$(JAVA_JNI)

uninstall-jni-yes:
	-rm -f -- $(JAVA_JNI_DESTDIR)$(JAVA_JNI_DIR)/$(JAVA_JNI_FILE)

uninstall-jni-no:

