#
# gmake makefile for OneRepMaxApplet
#
# 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.3 -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= \
         MyGraphics.class \
        OneRepMaxApplet.class \
        ${NULL}
JAVAFILES= ${CLASSES:.class=.java}
TARGETJAVAFILES= ${TARGETCLASS}.java

TARGETCLASS = OneRepMaxApplet
JAR_DEPENDS_ON = $(CLASSES)      macros.h index.php Makefile javacpp javarenumber
JAR_CONTAINS = *.class *.prejava macros.h index.php Makefile javacpp javarenumber


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

.SUFFIXES: .java .prejava .class
.prejava.class:
	javacpp ${CPPFLAGS} ${JAVAC} $*.prejava
	#javarenumber $*.class
${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 the program 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 OneRepMaxApplet.jar
.PHONY: send sendtest
send: renumber
	#rrr 'sh -c "scp $(SENDFILES) hatch@www.plunk.org:public_html/${TARGETCLASS}/."'
	sh -c "scp $(SENDFILES) hatch@www.plunk.org:public_html/${TARGETCLASS}/."
sendtest: renumber
	#rrr 'sh -c "scp $(SENDFILES) hatch@www.plunk.org:public_html/tmp/${TARGETCLASS}/."'
	sh -c "scp $(SENDFILES) hatch@www.plunk.org:public_html/tmp/${TARGETCLASS}/."

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