LIB := httpserver
APP := launch
VENDOR := unternet.net
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))
$(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)/*unternet* $(SIMULATOR)/*.dmp /tmp
siminstall:
	cp $(codfiles) \
	 $(SIMULATOR)
simulator:
	cd $(SIMULATOR) && ./7290.bat
simtest: simclean siminstall simulator
