#
# Makefile fragment to build and install the java plugin

######################################################################
# Java

ifeq ($(JCVALID),yes)
JAVA_BUILD=build-plugins-java
JAVA_INSTALL=install-plugins-java
JAVA_PLUGINS=plugins/node.d.java/*
endif

BUILD_DIR        ?= build

JAVA_SRCDIR      := plugins/javalib
JAVA_DSTDIR      := $(BUILD_DIR)/plugins/javalib

JAVA_FILES       := $(wildcard plugins/javalib/org/munin/plugin/jmx/*.java)
JAVA_CLASSES     := $(patsubst %.java,%.class,$(JAVA_FILES))

JC     := javac
JFLAGS  = -g -sourcepath $(JAVA_SRCDIR) -d $(JAVA_DSTDIR)

INSTALL ?= install


.PHONY: build-plugins-java

build-plugins-java: $(JAVA_DSTDIR)/munin-jmx-plugins.jar

$(JAVA_DSTDIR)/munin-jmx-plugins.jar: $(JAVA_DSTDIR)/org/munin/plugin/jmx
	jar cf $@ -C $(JAVA_DSTDIR) org/munin/plugin/jmx

$(JAVA_DSTDIR)/org/munin/plugin/jmx: $(JAVA_FILES)
	mkdir -p $(JAVA_DSTDIR)
	$(JC) $(JFLAGS) $^

install-plugins-java: $(JAVA_DSTDIR)/munin-jmx-plugins.jar
	mkdir -p $(JAVALIBDIR)
	$(INSTALL) -m 0644 $(JAVA_DSTDIR)/munin-jmx-plugins.jar $(JAVALIBDIR)/
	mkdir -p $(LIBDIR)/plugins
	for p in $(BUILD_DIR)/plugins/node.d.java/*; do               \
	    if test -f "$$p" ; then                            \
		echo Installing $$p;                           \
		$(INSTALL) -m 0755 $$p $(LIBDIR)/plugins/;     \
	    fi                                                 \
	done
