################################################################
# @configure_input@
#
# Build JPL.  Building JPL for Unix currently relies on the following
# assumptions:
#
#	* $JAVA_HOME points to the JDK top directory
#	* $PATH includes $JAVA_HOME/bin
#	* ELF Dynamic Linker semantics
#
# Author: Jan Wielemaker, based on shell-scripts from Paul Singleton.
################################################################

PACKAGE=jpl
PKGCFLAGS=@JPLCFLAGS@
PKGLDFLAGS=@JPLLDFLAGS@

include ../Makefile.defs

LIBS=@LIBS@

JAVA_HOME="@JAVA_HOME@"
JAVAC="@JAVAC@"
JAVACFLAGS=-source 1.4 -target 1.4
JAVA="@JAVA@"
JUNIT="@JUNIT@"
JAVALIBS="@JAVALIBS@"

LIBPL=		$(srcdir)/jpl.pl
LIBJPL=		@LIBJPL@.@SO@
TARGETS=	$(LIBJPL)
OBJ=		src/c/jpl.o

# YAP has some extra hacks that need to be compiled in.
ifeq (@PROLOG_SYSTEM@,yap)
src/c/jpl.o: $(srcdir)/src/c/jpl.c $(srcdir)/src/c/hacks.c
		$(CC) -c $(CFLAGS) $(srcdir)/src/c/jpl.c -o src/c/jpl.o
endif

all:		$(TARGETS) jpl.jar exjava-compile jpl_doc

# linking order counts here: otherwise libjpl.so will not remember
# it needs libYap.so
@LIBJPL@.@SO@:	$(OBJ)
		$(LD) $(LDSOFLAGS) -o $@ $(OBJ) $(LIBS) $(JAVALIBS) $(LIBPLEMBED)
		if [ -r @LIBJPL@.@SO@ ]; then \
		  rm -f @LIBJPL@.jnilib && ln -s @LIBJPL@.@SO@ @LIBJPL@.jnilib ; \
		fi

jpl.jar::
		(cd src/java && $(MAKE) jpl_jar)
jpl_doc::
		(cd src/java && $(MAKE) jpl_doc)

################################################################
# Verify the package
################################################################

check:		check_pl check_java

check_pl:	jpltest.jar
		$(PL) -q -f test_jpl.pl -g run_tests,halt -t 'halt(1)'
check_java:	jpltest.jar
		CLASSPATH=$(JUNIT):jpl.jar:jpltest.jar $(JAVA) junit.textui.TestRunner jpl.test.TestJUnit

jpltest.jar:
		(cd src/java && $(MAKE) test_jar)

################################################################
# Installation
################################################################

DOCDIRS=	$(srcdir) $(srcdir)/java_api \
		$(srcdir)/java_api/javadoc $(srcdir)/java_api/javadoc/jpl \
		$(srcdir)/java_api/javadoc/jpl/class-use \
		$(srcdir)/java_api/javadoc/jpl/fli \
		$(srcdir)/java_api/javadoc/jpl/fli/class-use \
		$(srcdir)/java_api/javadoc/resources \
		$(srcdir)/prolog_api \
		$(srcdir)/prolog_api/overview
DOCFILES=	$(shell cd $(srcdir)/docs && find . -name '*.html' -o -name '*.gif' -o -name '*.jpg')

EXPL=		$(PKGEXDIR)/jpl/prolog
EXPLS=		jpl_colour_choose_demo.pl \
		jpl_jlist_demo.pl \
		jpl_midi_demo.pl \
		jpl_table_demo.pl \
		jpl_text_entry_demo.pl \
		jpl_versions_demo.pl
EXJAVA=		$(PKGEXDIR)/jpl/java
EXJAVAS=	Exceptions Exceptions2 Family FamilyMT Test Test2 Time \
		Versions Zahed SemWeb


install:	$(TARGETS) $(LIBPL) expl-install exjava-install
		mkdir -p $(DESTDIR)$(SOLIBDIR)
		for f in $(TARGETS); do \
		  $(INSTALL_PROGRAM) "$$f" "$(DESTDIR)$(SOLIBDIR)"; \
		done
ifeq (@SO@,dylib)
		(cd $(DESTDIR)$(SOLIBDIR) && ln -sf @LIBJPL@.@SO@ @LIBJPL@.jnilib)
endif
		mkdir -p $(DESTDIR)$(PLLIBDIR)
		mkdir -p $(DESTDIR)$(PLLIBDIR)/jpl
		for f in $(LIBPL); do \
		  $(INSTALL_DATA) $$f $(DESTDIR)$(PLLIBDIR); \
		done
ifeq (@PROLOG_SYSTEM@,yap)
		$(INSTALL_DATA) jpl_paths.yap $(DESTDIR)$(PLLIBDIR)
endif
		$(INSTALL_DATA) jpl.jar $(DESTDIR)$(PLLIBDIR)/jpl
		$(PL) -f none -g make -t halt
		mkdir -p "$(DESTDIR)$(PLLIBDIR)"
		for f in $(LIBPL); do \
		  $(INSTALL_DATA) $$f $(DESTDIR)$(PLLIBDIR); \
		done
		$(INSTALL_DATA) jpl.jar $(DESTDIR)$(PLBASE)/lib
		$(MKINDEX)

ln-install::
		@$(MAKE) INSTALL_DATA='../ln-install' install

rpm-install:	install html-install

html-install::	expl-install exjava-install
		mkdir -p $(DESTDIR)$(PKGDOCDIR)/jpl
		@echo -n "Creating directories "
		@for d in $(DOCDIRS); do \
		    mkdir -p $(DESTDIR)$(PKGDOCDIR)/jpl/$$d; echo -n .; \
		done
		@echo "ok"
		@echo -n "Copying documentation files "
		@for f in $(DOCFILES); do \
		    $(INSTALL_DATA) docs/$$f $(DESTDIR)$(PKGDOCDIR)/jpl/$$f; echo -n .; \
		done
		@echo "ok"

expl-install::
		echo "Installing Prolog examples"
		mkdir -p $(DESTDIR)$(EXPL)
		@for f in $(EXPLS) README; do \
		    $(INSTALL_DATA) $(srcdir)/examples/prolog/$$f $(DESTDIR)$(EXPL)/$$f; \
		done

exjava-compile:	jpl.jar
		echo "Compiling Java examples"
		for d in $(EXJAVAS); do \
		    if [ ! -f examples/java/$$d/$$d.class ]; then \
		        echo $$d; \
			mkdir -p examples/java/$$d; \
		        (cd examples/java/$$d && $(JAVAC)  $(JAVACFLAGS) -d . -classpath "../../../jpl.jar" $(srcdir)/examples/java/$$d/$$d.java); \
		    fi; \
		done

exjava-install:	exjava-compile
		echo "Installing Java examples"
		mkdir -p "$(DESTDIR)$(EXJAVA)"
		$(INSTALL_DATA) $(srcdir)/examples/java/README "$(DESTDIR)$(EXJAVA)"
		$(INSTALL_DATA) $(srcdir)/examples/java/env.@CMDEXT@ "$(DESTDIR)$(EXJAVA)"
		for d in $(EXJAVAS); do \
		    mkdir -p $(DESTDIR)$(EXJAVA)/$$d; \
		    $(INSTALL_SCRIPT) $(srcdir)/examples/java/$$d/run.@CMDEXT@ "$(DESTDIR)$(EXJAVA)/$$d" ;\
		    $(INSTALL_DATA) $(srcdir)/examples/java/$$d/README "$(DESTDIR)$(EXJAVA)/$$d" ;\
		    $(INSTALL_DATA) $(srcdir)/examples/java/$$d/$$d.java "$(DESTDIR)$(EXJAVA)/$$d" ;\
		    plfile=`echo $$d.pl | tr A-Z a-z`; \
		    if [ -f $(scrdir)/examples/java/$$d/$$plfile ]; then \
			$(INSTALL_DATA) $(srcdir)/examples/java/$$d/$$plfile $(DESTDIR)$(EXJAVA)/$$d ;\
		    fi; \
		    $(INSTALL_DATA) examples/java/$$d/$$d*.class $(DESTDIR)$(EXJAVA)/$$d ;\
		done
		$(INSTALL_DATA) $(srcdir)/examples/java/SemWeb/test.rdf "$(DESTDIR)$(EXJAVA)/SemWeb"

pdf-install::
		mkdir -p $(DESTDIR)$(PKGDOCDIR)
		$(INSTALL) -m 644 $(DOC).pdf "$(DESTDIR)$(PKGDOCDIR)"

uninstall::
		(cd $(PLBASE)/lib/$(PLARCH) && rm -f $(TARGETS))
		(cd $(PLBASE)/library && rm -f $(LIBPL))
		$(PL) -f none -g make -t halt

################################################################
# Clean
################################################################

clean:
		rm -f $(OBJ) *~ *.o *% a.out core config.log
		rm -f TestJPL.class
		find examples/java -name '*.class' -delete
		(cd src/java && $(MAKE) clean)

distclean:	clean
		rm -rf autom4te.cache
		rm -f $(TARGETS) config.cache config.h config.status Makefile
		rm -f $(DOC).aux $(DOC).log $(DOC).out $(DOC).toc
		rm -rf html
		(cd src/java && $(MAKE) distclean)

