# Makefile for java packages

# Copyright 1997,1998 Alexandre Oliva <oliva@dcc.unicamp.br>

# This file is free software; you can redistribute it and/or modify it
# under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.

# This program is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
# General Public License for more details.

# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.

# You may want to specify the location of these programs

AWK     = awk
JAVAC	= guaranac
JAVADOC	= javadoc -author -version

# DOCDIR specifies where the documentation should be stored
DOCDIR= $(ROOTDIR)/docs/
# ROOTDIR should be maintained by the dependency manager

all:

CLASSPATH=$(ROOTDIR):$(ROOTDIR)/tutorial-src
SETCLASSPATH=CLASSPATH=$(CLASSPATH)$${CLASSPATH+:$$CLASSPATH}

.SUFFIXES: .class .java

.java.class:
	$(SETCLASSPATH) $(JAVAC) -d $(ROOTDIR) $< 

.PHONY: all doc depend clean distclean maintclean
.PHONY: norec.all rec.depend norec.depend
.PHONY: norec.clean norec.distclean norec.maintclean

norec.all:

all: norec.all
	ls -1 */Makefile 2>/dev/null | while read f; do \
	  (cd `dirname $$f`; $(MAKE) $@); \
	done

doc:
	if [ ! -d $(DOCDIR) ]; then mkdir $(DOCDIR); fi
	$(SETCLASSPATH) $(JAVADOC) -d $(DOCDIR) ` \
	    cd $(ROOTDIR) && \
	    find . -name \*.java -print | sed 's%^\\./%%' | \
	    while read f; do echo \`dirname $$f\`; done | uniq | \
	    sed s%/%.%g`

depend:
	@ case $(ROOTDIR) in \
	   .) rootdir=..;; \
	   *) rootdir=../$(ROOTDIR);; \
	  esac; \
	  find * -type d -prune ! -name CVS ! -name RCS ! -name SCCS -print | \
	  while read f; do \
	    if [ ! -f $$f/Makefile ]; then \
	      { $(AWK) 'noprint == 0 { print; } \
	                /^# $(DEPLINE)/ { noprint = 1; }' <Makefile; \
	        echo; echo ROOTDIR=$$rootdir; echo; \
	        echo "norec.all: depend"; \
	        echo "	if [ ! -f Makefile.new ]; then $$(MAKE); fi"; \
	      } >$$f/Makefile.new && \
	      mv $$f/Makefile.new $$f/Makefile || exec false; \
	    fi; \
	    (cd $$f && $(MAKE) `grep '^ROOTDIR=' Makefile` depend) || exec false; \
	  done
	$(MAKE) norec.depend

norec.depend:
	rm -f Makefile.new
	@ \
	{ $(AWK) 'noprint == 0 { print; } \
	          /^# $(DEPLINE)/ { noprint = 1; }' <Makefile; \
	  echo; echo ROOTDIR=$(ROOTDIR); echo; \
	  ls -1 ./*.java 2>/dev/null | \
	  while read f; do \
	    echo Computing dependencies for $$f... >&2; \
	    $(SETCLASSPATH) $(JAVAC) -verbose $$f 2>&1 | \
	    $(AWK) ' \
	      /^\[parsed / && classname == "" { \
	        classname = substr($$2, 1, length($$2)-5) ".class"; \
	        print "CLASSES += " classname; \
	        print classname ": " $$2; \
	        next; \
	      } \
	      /^\[parsed / { \
	        print classname ": " $$2; \
	        next; \
	      } \
	      /^\[loaded [^ $$]*\.class / && index($$2, "$(ROOTDIR)/") == 1 { \
	        print classname ": " \
	          substr($$2, 1, length($$2)-6) ".java"; \
	        next; \
	      } \
	      /^\[wrote / && $$2 == classname "]" { \
		next; \
	      } \
	      /^\[wrote / { \
		split(substr($$2, 1, length($$2)-1), clsa, "\$$"); \
		for (i in clsa) { cls = (i == 1 ? "" : cls"$$$$") clsa[i]; } \
	        print "CLASSES += " cls; \
		print cls ": " classname; \
		print "	rm -f " classname " && $$(MAKE) " classname; \
	        next; \
	      } \
	      /^\[.*\]$$/ { next; } \
	      { print | "cat >&2"; } \
	    '; \
	  done; \
	  echo; echo "norec.all: $$(CLASSES)"; \
	} >Makefile.new
	mv Makefile Makefile.bak
	mv Makefile.new Makefile

norec.clean:
	rm -f *~ '#'* core Makefile.bak Makefile.new

clean: norec.clean
	ls -1 */Makefile 2>/dev/null | while read f; do \
	  (cd `dirname $$f`; $(MAKE) $@); \
	done

norec.distclean: norec.clean
	rm -f $(CLASSES)
	rm -rf $(DOCDIR)
	@ \
	{ $(AWK) 'noprint == 0 { print; } \
	          /^# $(DEPLINE)/ { noprint = 1; }' <Makefile; \
	  echo; echo ROOTDIR=$(ROOTDIR); echo; \
	  echo "norec.all: depend"; \
	  echo "	if [ ! -f Makefile.new ]; then $$(MAKE); fi"; \
	} >Makefile.new
	mv Makefile Makefile.bak
	mv Makefile.new Makefile
	rm -f Makefile.new Makefile.bak

distclean: 
	ls -1 */Makefile 2>/dev/null | while read f; do \
	  (cd `dirname $$f`; $(MAKE) $@); \
	done
	$(MAKE) norec.distclean

norec.maintclean: norec.distclean
	@ if [ "$(ROOTDIR)" != "." ] && [ -f ../Makefile ] && \
	  $(AWK) 'noprint == 0 { print; } \
	          /^# $(DEPLINE)/ { noprint = 1; }' \
	    <Makefile >Makefile.mine && \
	  $(AWK) 'noprint == 0 { print; } \
	          /^# $(DEPLINE)/ { noprint = 1; }' \
	    <../Makefile >Makefile.parent  && \
	  diff Makefile.mine Makefile.parent >/dev/null 2>&1; then \
	  rm -f Makefile; \
	fi
	rm -f Makefile.mine Makefile.parent

maintclean: norec.distclean
	ls -1 */Makefile 2>/dev/null | while read f; do \
	  (cd `dirname $$f`; $(MAKE) $@); \
	done
	$(MAKE) norec.maintclean

DEPLINE = Do not remove this line, makedepend depends on it

# Do not remove this line, makedepend depends on it

ROOTDIR=.

CLASSES += example1.class
example1.class: example1.java
example1.class: ./tutorial-src/MetaLogger.java
CLASSES += example1$$myMetaObject.class
example1$$myMetaObject.class: example1.class
	rm -f example1.class && $(MAKE) example1.class
CLASSES += example1$$myObject.class
example1$$myObject.class: example1.class
	rm -f example1.class && $(MAKE) example1.class
CLASSES += test_ops.class
test_ops.class: test_ops.java
CLASSES += test_ops_base.class
test_ops_base.class: test_ops.class
	rm -f test_ops.class && $(MAKE) test_ops.class
CLASSES += test_ops_i.class
test_ops_i.class: test_ops.class
	rm -f test_ops.class && $(MAKE) test_ops.class

norec.all: $(CLASSES)
