#
# gmake makefile for HyperbolicApplet
#
# This should work under Linux (redhat 7.3 thru 9) and cygwin.
# You may have to add your java root to POSSIBLE_JAVAROOTS though.
#

# prefer 1.1, then progressively newer APIs...
# (but progressively older revisions within a given API)
# XXX is this necessary now that I use the "target" thing?
# XXX obviously this should do a more general search
POSSIBLE_JAVAROOTS = \
        c:/jdk1.1.8 \
        c:/jdk1.3.1_01 \
        c:/jdk1.3.1_02 \
        c:/j2sdk1.4.0_01 \
        c:/j2sdk1.4.1_01 \
        /usr/java/jdk1.2.2 \
        /usr/java/jdk1.3.1_08 \
        /usr/java/jdk1.3.1_03 \
        /usr/java/jdk1.3.1 \
        /usr/java/j2sdk1.4.2_01 \
        /usr/java/j2sdk1.4.2 \
        /usr/java/j2sdk1.4.0 \
        /usr/local/java \
        $(NULL)

# Set JAVAROOT to the first existing one of the above...
JAVAROOT := $(shell \
                for d in $(POSSIBLE_JAVAROOTS); do \
                   if [ -f $$d/bin/javac ]; then \
                       echo $$d; \
                       break; \
                   fi \
                done \
            )

ifeq ($(JAVAROOT), )
       # (no tab in the following)
       $(error ERROR: no suitable JAVAROOT found)
endif


# XXX on redhat 7.3,, when jikes doesn't exist, this works
# but shows garbage?
JIKES := $(shell ((jikes -version) 2>&1) | grep -v -q " not found" && echo jikes)

# SEP is ":" on linux, ";" on Windows
SEP := $(shell \
            if [ -d c:/ ]; then \
                echo ";"; \
            else \
                echo ":"; \
            fi \
        )

CLASSPATH=.

# Use jikes if it exists, since it's hella fast
ifneq ($(JIKES), )    # if jikes exists...
    #CLASSPATH = ${JAVAROOT}/jre/lib/rt.jar${SEP}/usr/lib/netscape/java/classes/java40.jar
    CLASSPATH := ${CLASSPATH}${SEP}${JAVAROOT}/jre/lib/rt.jar
    JAVAC = $(JIKES) -g +P +M -source 1.1 -target 1.1 -classpath "${CLASSPATH}"
    # -g means debug
    # +P means pedantic
    # +M means generate .u files showing dependencies (should match below)
    # -deprecation warns about use of deprecated features
else
    CLASSPATH := ${CLASSPATH}${SEP}${JAVAROOT}/jre/lib/rt.jar
    JAVAC = $(JAVAROOT)/bin/javac -g -target 1.1 -classpath "${CLASSPATH}"
    # -g means debug
    # -deprecation warns about use of deprecated features
endif

# make sure javacpp and javarenumber are executable,
# since jar fails to make it so on extraction
DUMMY := $(shell chmod +x javacpp javarenumber 2>&1)

# XXX the -DMAIN following should probably be removed for production version
CPPFLAGS = -DMAIN

CLASSES= \
        HyperbolicApplet.class \
        HyperbolicDrawUtils.class \
        PickableSchwarzPolygon.class \
        HyperbolicUtils.class \
        MyMath.class \
        VecMath.class \
        Arrays.class \
        Isometry2HashTable.class \
        Isometry2.class \
        Isometry.class \
        Complex.class \
        MyGraphics.class \
        PSMyGraphics.class \
        GraphicsAntiAliasingSetter.class \
        ${NULL}
JAVAFILES= ${CLASSES:.class=.java}
TARGETJAVAFILES= ${TARGETCLASS}.java Isometry.java
# XXX Isometry.java is named explicitly since it's not used by the main program (yet) but we want to include it anyway 

TARGETCLASS = HyperbolicApplet
JAR_DEPENDS_ON = $(CLASSES)      macros.h index.php README todo.txt Makefile javacpp javarenumber
JAR_CONTAINS = *.class *.prejava macros.h index.php README todo.txt Makefile javacpp javarenumber


.PHONY: all
all: ${TARGETCLASS}.jar

.SUFFIXES: .java .prejava .class
.prejava.java:
	javacpp ${CPPFLAGS} $*.prejava
#.prejava.class:
#	javacpp ${CPPFLAGS} ${JAVAC} $*.prejava
#	javarenumber $*.class
# XXX added Isometry.java since it's currently not used so jikes won't find it, but we want to include it anyway
${TARGETCLASS}.class: ${JAVAFILES}
	#${JAVAC} ${TARGETJAVAFILES}
	#javacpp ${JAVACPPFLAGS} ${JAVAC} ${TARGETJAVAFILES:.java=.prejava} # XXX jeez, clean this up
	javacpp ${JAVACPPFLAGS} ${JAVAC} ${JAVAFILES:.java=.prejava} # XXX jeez, clean this up
${TARGETCLASS}.jar: $(JAR_DEPENDS_ON)
	${JAVAROOT}/bin/jar -cfm ${TARGETCLASS}.jar META-INF/MANIFEST.MF ${JAR_CONTAINS}
# Separate renumber target since renumbering all the subclass files
# on every recompile is slow :-(.  Usually I run "make renumber"
# after an exception, and then run it again so I will get
# a stack trace with line numbers from the .prejava files instead of
# the .java files.
${TARGETCLASS}.jar.is_renumbered: $(JAR_DEPENDS_ON)
	./javarenumber -v -1 *.class
	${JAVAROOT}/bin/jar -cfm ${TARGETCLASS}.jar META-INF/MANIFEST.MF ${JAR_CONTAINS}
	touch $@

.PHONY: renumber
renumber: ${TARGETCLASS}.jar.is_renumbered

${JAVAFILES}: macros.h

SENDFILES = index.php HyperbolicApplet.jar
.PHONY: send
send: renumber
	rrr 'sh -c "scp $(SENDFILES) hatch@www.plunk.org:public_html/${TARGETCLASS}/."'

.PHONY: clean
clean:
	rm -f core *.jar *.class *.java.lines *.java
