#
# gmake makefile for DualApplet
#
# This should work under Linux (redhat 7.3) and cygwin.
#
# XXX Note! Jikes 1.21 seems to be broken, but Jikes 1.18 seems to be okay.
# 1.21 gives this in Angel1.prejava, when trying to do SortStuff stuff:
#    jikes: symbol.h:1110: bool TypeSymbol::IsInner() const: Assertion `(! IsLocal() && ! Anonymous()) || (IsNested() && ! ACC_STATIC())' failed.
#

# prefer 1.1, then progressively newer...
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 \
        c:/j2sdk1.4.2_02 \
        /usr/local/jdk1.2.2 \
        /usr/java/jdk1.3.1 \
        /usr/java/jdk1.3.1_03 \
        /usr/java/j2sdk1.4.0 \
        /usr/java/j2sdk1.4.2 \
        /usr/java/j2sdk1.4.2_01 \
        /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
    # -source 1.1 is necessary due to a jikes bug
    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 ETCcontents.pl 2>&1)

TARGETCLASS = DualApplet
JAR_DEPENDS_ON = DualApplet.class MyPanel.class VecMath.class Arrays.class SortStuff.class VectorFunctionOfPositionAndNormal.class Minimizer.class ETC.class macros.h index.php todo.txt Makefile javacpp javarenumber ETCcontents.pl
JAR_CONTAINS = *.class *.prejava macros.h index.php todo.txt Makefile javacpp javarenumber ETCcontents.pl

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

${TARGETCLASS}.jar: $(JAR_DEPENDS_ON)
	rm -f ETCcontents.prejava ETCcontents*.class
	${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)
	rm -f ETCcontents.prejava ETCcontents*.class
	./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, on cygwin the time zone is missing, so it's not a fixed width
	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;' < 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...
# Note that no other classes depend on ETCcontents.class! (It is delay-loaded)
DualApplet.class: MyPanel.class VecMath.class Arrays.class SortStuff.class VectorFunctionOfPositionAndNormal.class Minimizer.class ETC.class macros.h
ETC.class: macros.h
ETCcontents.jar ETCcontents.class: ETC.class VecMath.class Arrays.class SortStuff.class macros.h
Minimizer.class: VecMath.class macros.h
VecMath.class: Arrays.class macros.h
VectorFunctionOfPositionAndNormal.class: VecMath.class macros.h

ETCcontents.html:
	curl http://faculty.evansville.edu/ck6/encyclopedia/ETC.html > $@
ETCcontents.prejava: ETCcontents.html ETCcontents.pl
	./ETCcontents.pl < ETCcontents.html > $@.temp
	chmod a-w $@.temp
	mv $@.temp $@
ETCcontents.jar: ETCcontents.pl # not ETCcontents.class, since that often gets removed.  Note we depend ETCcontents.jar on ETCcontents.class's dependencies above, to make sure they are done before executine sub-make, so sub-make doesn't try to do them which would cause collisions.
	${MAKE} ETCcontents.class
	${JAVAROOT}/bin/jar -cf $@ ETCcontents*.class

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

#.prejava.java:
#	./javacpp ${CPPFLAGS} $*.prejava
#.java.class:
#	${JAVAC} $*.java
#	./javarenumber -v 0 $*.class

.PHONY: clean
clean:
	rm -f *.java *.java.lines *.class *.u *.temp ETCcontents.prejava
clobber:
	rm -f *.java *.java.lines *.class *.u *.temp ETCcontents.prejava ETCcontents.html *.jar *.jar.is_renumbered index.php.is_stamped


#
# Targets only useful to Don...
#
# Don't bother sending class files, since only java 1.0 needs them
# and I'll never make this compatible with 1.0
SENDFILES=index.php ${TARGETCLASS}.jar ETCcontents.jar
.PHONY: send sendtest sendtest0
send: ${TARGETCLASS}.jar.is_renumbered ETCcontents.jar
	#rrr 'sh -c "scp $(SENDFILES) hatch@www.plunk.org:public_html/${TARGETCLASS}/."'
	sh -c "scp $(SENDFILES) hatch@www.plunk.org:public_html/${TARGETCLASS}/."
sendtest: ${TARGETCLASS}.jar.is_renumbered ETCcontents.jar
	sh -c "scp $(SENDFILES) hatch@www.plunk.org:public_html/${TARGETCLASS}test/."
sendtest0:
	sh -c "scp index.php hatch@www.plunk.org:public_html/${TARGETCLASS}test/."

