# to assign a java source to a (library or application) package named blather:
#  place a comment // PACKAGE=blather; # somewhere in the file
# remember the name of the app is always preceded by lib: library_application
# to continue with my previous example, the app might be: blather_prate
DESCRIPTION := subclassible example of blackberry httpfilter
LIB := httpfilter
APP := launch
VENDOR := jcomeau.com
PACKAGEBASE := $(shell python -c "parts = '$(VENDOR)'.split('.'); \
	parts.reverse(); print '_'.join(parts)")
JAVASRC := $(shell python -c "import os; \
	parts = os.getcwd().split(os.path.sep); \
	print os.path.sep.join(parts[0:parts.index('java') + 1])")
TARGETDIR := $(subst $(JAVASRC)/,,$(PWD))
CLASSBASE := $(subst /,.,$(TARGETDIR))
LIBNAME := $(PACKAGEBASE)_$(LIB)
APPNAME := $(LIBNAME)_$(APP)
RELEASEDIR := /cygdrive/c/Documents\ and\ Settings/jcomeau/My\ Documents
BLACKBERRY := /cygdrive/c/Program\ Files/Research\ In\ Motion
JDE40 := $(BLACKBERRY)/Blackberry\ JDE\ 4.0
RAPC := $(JDE40)/bin/rapc.exe
SIMULATOR := $(JDE40)/simulator
JRELIB := $(JDE40)/lib/net_rim_api.jar
JRELIB_WINDOWS := $(shell cygpath -w $(JRELIB))
CLASSPATH := "$(JRELIB_WINDOWS);."
JAVAC := javac -g -deprecation -classpath $(CLASSPATH)
JAVA := java -classpath $(CLASSPATH)
NEWVERSION := $(shell python -c "print '%.2f' % ($(VERSION) + 0.01)")
sources := $(wildcard *.java)
classes := $(sources:.java=.class)
# rapc files must be made as a separate step if needed
rapcfiles := $(wildcard *.rapc)
codfiles := $(rapcfiles:.rapc=.cod)
# "dynamic", recursively expanded variables
VERSION = $(shell ls -t releases | head -n 1)
export  # tells 'make' to export all vars to environment
classes: $(classes)
%.class: %.java
	cd $(JAVASRC) && $(JAVAC) $(addprefix $(TARGETDIR)/, $(sources))
# insert comment PACKAGE=browser in java source to assign it to browser app
%.cod: %.rapc
	$(RAPC) import="$(JRELIB_WINDOWS)" \
	 $(shell grep -iq '^midp-library-flags:' $< && \
	 echo "library=$*" || echo "codename=$*") $< \
	 $(grep -rl PACKAGE=$(patsubst $(PACKAGEBASE)_%,%,$<) $(sources))
test: $(classes)
	cd $(JAVASRC) && $(JAVA) $(CLASSBASE).Test
testparse: $(classes)
	cd $(JAVASRC) && $(JAVA) $(CLASSBASE).UrlParser
checkenv:
	set
backup:
	tar cvfj /tmp/bbtest.$$$$.tbz . \
	--exclude "*.debug" --exclude "*RCS*" && \
	scp /tmp/bbtest.$$$$.tbz zero:backup/laptop/
ls:
	cd $(JAVASRC) && ls -al $(TARGETDIR)
bbls:
	cd $(BLACKBERRY) && ls -alR
jar: Launch.class
	echo Main-Class: $(CLASSBASE)/Launch > manifest
	cd $(JAVASRC) && jar cmf $(TARGETDIR)/manifest \
	 $(TARGETDIR)/$(LIBNAME).jar $(TARGETDIR)/*.class
	cp $(LIBNAME).jar ~/docs
clean:
	rm -f *.class *.cod *.debug *.cso *.csl *.jad *.rapc
$(APPNAME).rapc: application.rapc.template
	template=$$(cat application.rapc.template) && \
	 eval "echo \"$$template\"" > $(APPNAME).rapc
$(LIBNAME).rapc: library.rapc.template
	template=$$(cat library.rapc.template) && \
	 eval "echo \"$$template\"" > $(LIBNAME).rapc
simclean:
	-mv -f $(SIMULATOR)/*jcomeau* $(SIMULATOR)/*.dmp /tmp
siminstall:
	cp $(codfiles) \
	 $(SIMULATOR)
simulator:
	cd $(SIMULATOR) && ./7290.bat
simtest: simclean siminstall simulator
