#
# You may need to alter some of these variables to make jniglk compile
#

# Where your copy of the JDK is installed (I used v1.1.8)
JDK_ROOT     = /usr/local/jdk
# The name of the JDK machine-specific headers directory
JDK_PLATFORM = linux
# All JDK include directories
JDK_INCLUDE  = -I$(JDK_ROOT)/include -I$(JDK_ROOT)/include/$(JDK_PLATFORM)
# Path to JDK libraries directory
JDK_LIBS     = -L$(JDK_ROOT)/lib/i686/native_threads -ljava 
# Directory to install jniglk to
INSTALLBIN   = /usr/local/bin
# Directory to install our libraries to
INSTALLLIB   = /usr/local/lib
# A BSD compatible install program
INSTALL      = install
# Program to update shared libraries. If this is not necessary on your system
# just leave this line blank.
LDCONFIG     = ldconfig
# Other libraries we need to link with
LIBS         = -L. -L/usr/X11R6/lib -lXpm -lX11 -ljpeg -lpng
# Other include directories we need to search
INCLUDES     =
# The compiler
CC	     = gcc
# System-specific C flags
SYS_CFLAGS   = -g -Wall
# Compiler flag to build DLLs
DLL_CFLAGS   = -fpic
# Compiler flag to link DLLs
DLL_LDFLAGS   = -shared
#
# Below this line you should only need to change things if you aren't
# using Sun's JDK.
#
##############################################################################
#
# The Java compiler
JAVAC         = $(JDK_ROOT)/bin/javac
# The Java archiver
JAR           = $(JDK_ROOT)/bin/jar
# The JNI header builder
JAVAH         = $(JDK_ROOT)/bin/javah -jni
# The Java documentation tool
JAVADOC       = $(JDK_ROOT)/bin/javadoc
# Where the Java system classes live
JAVA_CLASSDIR = $(JDK_ROOT)/lib/classes.zip
#
# Below this line you shouldn't need to change anything.
#
##############################################################################
#
# Remove command
RM	  = rm -f
# All includes
INCLUDE   = -I./xglk $(INCLUDES) $(JDK_INCLUDE)
# All C flags
CFLAGS    = $(SYS_CFLAGS) $(INCLUDE)

# Package name with _s in it 
CLASSHPRE = org_ifarchive_glk
# Package name with .s in it 
CLASSROOT = org.ifarchive.glk
# Package name with /s in it 
CLASSDIR  = org/ifarchive/glk
# Java classes to build
CLASSES	  = $(CLASSDIR)/Glk.class \
	    $(CLASSDIR)/Event.class \
	    $(CLASSDIR)/Window.class \
	    $(CLASSDIR)/Stream.class \
	    $(CLASSDIR)/StreamResult.class \
	    $(CLASSDIR)/FileRef.class \
	    $(CLASSDIR)/SoundChannel.class \
	    $(CLASSDIR)/Reference.class \
	    $(CLASSDIR)/InterruptListener.class \
	    $(CLASSDIR)/ObjectRegistry.class \
	    $(CLASSDIR)/ArrayRegistry.class \
	    $(CLASSDIR)/OpaqueObject.class \
	    $(CLASSDIR)/GlkArray.class \
	    $(CLASSDIR)/StreamReader.class \
	    $(CLASSDIR)/StreamWriter.class \
	    $(CLASSDIR)/UniversalUnion.class \
	    $(CLASSDIR)/Prototype.class \
	    $(CLASSDIR)/Function.class \
	    $(CLASSDIR)/IntConstant.class \
	    $(CLASSDIR)/BlorbException.class \
	    Model.class \
	    MultiWin.class
# The DLL to build
DLL       = libjniglk.so
# The debug version of the DLL
GDLL      = libjniglk_g.so
# Objects in the DLL
OBJECTS   = $(CLASSHPRE)_Glk.o
# Objects in the executable program
EOBJECTS  = jniglk.o
# Libraries to use when building the executable
ELIBS	  = $(JDK_LIBS) -lxglk $(LIBS)
# The executable program to build
EXE       = jniglk
# The JAR file to build
JARFILE   = jniglk.jar
# The machine-generated header for native functions.
HFILE     = $(CLASSHPRE)_Glk.h
# Directory to tar up into
TARDIR    = jniglk-1.0

all:	libxglk.so $(CLASSES) $(DLL) $(GDLL) $(EXE) $(JARFILE)

install:	all
	$(INSTALL) -m 755 $(EXE) $(INSTALLBIN)
	$(INSTALL) -m 644 $(JARFILE) $(INSTALLLIB)
	$(INSTALL) -m 755 libxglk.so $(INSTALLLIB)
	$(INSTALL) -m 755 $(DLL) $(INSTALLLIB)
	$(INSTALL) -m 755 $(GDLL) $(INSTALLLIB)
	$(LDCONFIG)

libxglk.so:	xglk/libxglk.so
	cp xglk/libxglk.so .

xglk/libxglk.so:
	cd xglk && make

$(JARFILE):	$(CLASSES)
	$(JAR) c0f $@ $(CLASSDIR)/*.class

doc:	docs/packages.html

docs/packages.html:	$(CLASSES)
	$(JAVADOC) -d docs -sourcepath $(JAVA_CLASSDIR):.:$(CLASSDIR) $(CLASSROOT)

tar:	doc
	rm -f $(TARDIR)	
	rm -f $(TARDIR).tar.gz
	ln -sf . $(TARDIR)
	tar chf $(TARDIR).tar \
	$(TARDIR)/README \
	$(TARDIR)/$(CLASSDIR)/*.java \
	$(TARDIR)/$(CLASSDIR)/*.class \
	$(TARDIR)/*.class \
	$(TARDIR)/*.java \
	$(TARDIR)/Makefile \
	$(TARDIR)/$(CLASSHPRE)_Glk.c \
	$(TARDIR)/jniglk.c \
	$(TARDIR)/docs \
	$(TARDIR)/xglk.diffs \
	$(TARDIR)/xglk/Makefile \
	$(TARDIR)/xglk/README \
	$(TARDIR)/xglk/PREFS \
	$(TARDIR)/xglk/TODO \
	$(TARDIR)/xglk/*.h \
	$(TARDIR)/xglk/*.bm \
	$(TARDIR)/xglk/*.c
	gzip $(TARDIR).tar

$(DLL):	$(OBJECTS) Makefile 
	$(CC) $(DLL_LDFLAGS) -o $@ $(OBJECTS) $(LIBS)

$(GDLL):	$(OBJECTS) Makefile 
	$(CC) -g $(DLL_LDFLAGS) -o $@ $(OBJECTS) $(LIBS)

$(EXE):		$(EOBJECTS) Makefile libxglk.so
	$(CC) $(CFLAGS) -o $@ $(EOBJECTS) $(ELIBS)

%.class:	%.java
	$(JAVAC) -d . $<

$(HFILE):	$(CLASSDIR)/Glk.class
	$(JAVAH) $(CLASSROOT).`basename $< .class`
	touch $@


%.o:	%.c %.h
	$(CC) -c -o $@ $(CFLAGS) $(DLL_CFLAGS) $<

jclean:	
	$(RM) $(CLASSDIR)/*.class *.class 

clean:
	$(RM) *.o *.so $(CLASSDIR)/*.class *.class $(HFILE)
	cd xglk && make clean
