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

# prefer 1.1, then progressively newer APIs...
# 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.3.1 \
        /usr/java/jdk1.3.1_08 \
        /usr/java/jdk1.3.1_03 \
        /usr/java/j2sdk1.4.2 \
        /usr/java/j2sdk1.4.0 \
        /usr/local/java \
        $(NULL)

# Unfortunately, jdk1.1.2 seems to have a bug
# that prevents EfficientFrontierApplet from compiling...
IMPOSSIBLE_JAVAROOTS = \
        /usr/local/jdk1.2.2 \
        $(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)


TARGETCLASS = EfficientFrontierApplet
JAR_DEPENDS_ON = EfficientFrontierApplet.class RhoPanel.class MyPanel.class SortStuff.class Minimizer.class macros.h index.php todo.txt Makefile javacpp javarenumber
JAR_CONTAINS = *.class *.prejava macros.h index.php todo.txt Makefile javacpp javarenumber

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

${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 $@
index.php.is_stamped: index.php $(TARGETCLASS).class Makefile
# XXX -i erases the file on cygwin, so use a temp file instead
# XXX also, the time zone is not fixed width (e.g. "PDT", "GMT-8", and on cygwin "")
	perl -p -e 's/^\S\S\S \S\S\S .\d \d\d:\d\d:\d\d \S* \d\d\d\d\n/`date`/e;' -e "s/ GMT-8 / PDT /;" -e "s/ GMT-9 / PST /;" < index.php > index.php.temp
	mv index.php.temp index.php
	touch $@

.PHONY: renumber stamp
renumber: ${TARGETCLASS}.jar.is_renumbered
stamp: index.php.is_stamped

# Make sure modules are compiled in order of dependence...
EfficientFrontierApplet.class: MyGraphics.class RhoPanel.class MyPanel.class VecMath.class Arrays.class SortStuff.class Minimizer.class macros.h
MyGraphics.class: MyMath.class VecMath.class macros.h
Minimizer.class: VecMath.class macros.h
RhoPanel.class: VecMath.class macros.h
MyPanel.class: macros.h
VecMath.class: Arrays.class macros.h
Arrays.class: macros.h
SortStuff.class: macros.h

.SUFFIXES: .prejava .class
.prejava.class:
	./javacpp ${CPPFLAGS} ${JAVAC} $*.prejava
	./javarenumber -v 0 $*.class
# too slow... only do this in the production version
#	@./javarenumber -v -1 $*'$$'*.class

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


#
# Another target, only useful to Don...
#
SENDFILES=index.php ${TARGETCLASS}.jar *.class
.PHONY: send
send: ${TARGETCLASS}.jar.is_renumbered
#       (need to send all class files, for browsers using java 1.0)
	rrr 'sh -c "scp $(SENDFILES) hatch@www.plunk.org:public_html/${TARGETCLASS}"'

