AIBO3DCACHE:=.aibo3dcache
ifneq ($(MAKECMDGOALS),clean)
AIBO3D:=$(shell if [ ! -r "$(AIBO3DCACHE)" ] ; then javac org/tekkotsu/aibo3d/Java3DTest.java > /dev/null 2>&1; echo $$? > "$(AIBO3DCACHE)"; fi; cat "$(AIBO3DCACHE)" )
endif

SRCS:=$(shell find . -name "*.java")

OBJS:=${SRCS:.java=.class}

CXX=javac
CURDIR:=$(shell pwd | sed 's/.*\///')

# SEP is to use ';' on windows and ':' on unix because windows is mentally deficient
SEP:=$(shell if [ "`uname`" = "CYGWIN" -o "`uname`" \> "CYGWIN" -a "`uname`" \< "CYGWIO" ] ; then echo ";" ; else echo ":" ; fi )
CXXFLAGS=-deprecation -classpath ".$(SEP)./ftp.jar$(SEP)./jargs.jar" 

.PHONY: all clean build clearbuildlist

all: clearbuildlist build

org/tekkotsu/aibo3d/%.class: org/tekkotsu/aibo3d/%.java
	@if [ $(AIBO3D) -eq 0 ] ; then \
		printf " $<" >> tmp_buildList.txt; \
	fi;

%.class: %.java
	@printf " $<" >> tmp_buildList.txt;

clearbuildlist:
	@rm -f tmp_buildList.txt;

tmp_buildList.txt: ${OBJS}
#	@echo "Build list constructed...";

build: tmp_buildList.txt
	@if [ -r tmp_buildList.txt ] ; then \
		cat README; \
		if [ $(AIBO3D) -ne 0 ] ; then \
			echo " *************************************************************************"; \
			echo " **     Java3D package not found.  Skipping Aibo3D...                   **"; \
			echo " **     More information: http://java.sun.com/products/java-media/3D/   **"; \
			echo " **     (Status cached for faster recompiles - make clean to clear)     **"; \
		else \
			echo " **     (Using cached Java3D status - make clean to clear cache)        **"; \
		fi;	\
		$(if $(shell which $(CXX)), \
			if [ `cat tmp_buildList.txt | wc -c` -gt 400 ] ; then \
				list=' [...]'; \
			else \
				list="`cat tmp_buildList.txt`"; \
			fi; \
			echo "Compiling$$list ..."; $(CXX) $(CXXFLAGS) `cat tmp_buildList.txt`; , \
			printf "  ***** WARNING: You don't have java... skipping TekkotsuMon *****\n"; \
		) \
	else \
		echo "TekkotsuMon: Nothing to be done."; \
	fi;

clean:
	rm -f tmp_buildList.txt "$(AIBO3DCACHE)"
	find . -name "*.class" -exec rm \{\} \;
	find . -name "*~" -exec rm \{\} \;
