#-*-makefile-*-
######################################################################
# 
# Contains the shared make rules for the tools/java tree.
#
# In each directory, create a Makefile that includes the lines
#    ROOT = <path to this directory>
#    include $(ROOT)/Makefile.include
#
# By default the Makefile will compile all .java source code in the
# current directory. You may also specify the following flags in 
# your Makefile, *before* including Makefile.include:
#
#   SUBDIRS = dir1 dir2 ...
#     Specify a list of subdirectories that 'make' should descend into
#
#   INITIAL_TARGETS = target1 target2 ...
#     Specify build targets to be compiled before compiling Java classes
#
#   FINAL_TARGETS = target1 target2 ...
#     Specify build targets to be compiled after compiling Java classes
#
#   OTHER_CLEAN = target1 target2 ...
#     Specify other targets to be executed when 'make clean' is run
#
# NOTE: this Makefile requires GNU make, as well as a number of 
#       standard UNIX shell tools.
#
######################################################################

SRC = $(wildcard *.java)
JAVA = $(SRC)
CLASSES = $(JAVA:.java=.class)

all: here subdirs $(FINAL_TARGETS)

# figure out useful variables
PWD = $(shell pwd)

# set compiler command
ifeq ($(JAVAC)_x, _x)
JAVAC = javac
endif

# general rule for java files
%.class: %.java
	$(JAVAC) $<

ifeq ($(SUBDIRS)_x, _x)

subdirs: here

subdirs-clean: here-clean

else
subdirs: here
	@for i in $(SUBDIRS); do \
		if [ -d $$i ]; then \
			if [ -f $$i/Makefile ]; then \
				$(MAKE) -C $$i; \
			else \
				echo "***" no Makefile in directory: $(PWD)/$$i; \
			fi \
		else \
			echo "***" skipping missing directory: $(PWD)/$$i; \
		fi; \
	done

subdirs-clean: here-clean
	@for i in $(SUBDIRS); do \
		if [ -d $$i ]; then \
			if [ -f $$i/Makefile ]; then \
				$(MAKE) -C $$i clean; \
			else \
				echo "***" no Makefile in directory: $(PWD)/$$i; \
			fi \
		else \
			echo "***" skipping missing directory: $$i; \
		fi; \
	done

subdirs-install: 
	@for i in $(INSTALLDIRS); do \
		if [ -d $$i ]; then \
			if [ -f $$i/Makefile ]; then \
				$(MAKE) -C $$i install; \
			else \
				echo "***" no Makefile in directory: $(PWD)/$$i; \
			fi \
		else \
			echo "***" skipping missing directory: $$i; \
		fi; \
	done

endif

here: printdir $(INITIAL_TARGETS) $(JAVA) $(CLASSES) FORCE

printdir:
	@echo "... $(PWD)"; 

here-clean: FORCE
	@rm -f *.class *~ javacore*.txt
	@echo "cleaning $(PWD)" 

clean: here-clean subdirs-clean $(OTHER_CLEAN)

install: subdirs-install
# some phony targets - FORCE forces a command to be run on all dependencies,
# and .PHONY prevents badness if a phony target coincides with a filename

FORCE:

.PHONY: all $(SUBDIRS) $(SUBDIRSCLEAN) clean
