#
# Makefile created at Fri Oct 16 13:31:34 1998, by mmake
#

# Programs (with common options):
SHELL		= /bin/sh
RM              = rm -f
MV              = mv -f
SED		= sed
XARGS		= xargs
CAT		= cat
FIND            = find
CPP		= cpp -C -P

INSTALL         = install
INSTALL_PROG    = $(INSTALL) -m $(MODE_PROGS)
INSTALL_FILE    = $(INSTALL) -m $(MODE_FILES)
INSTALL_DIR     = $(INSTALL) -m $(MODE_DIRS) -d

# Install modes 
MODE_PROGS      = 755
MODE_FILES      = 644
MODE_DIRS       = 755

# Build programs
JAVAC           = javac
JAVADOC         = javadoc
JAR             = jar

# Build flags
JAVAC_FLAGS     = 
JAVADOC_FLAGS   = -version -author
JAR_FLAGS       = cvf0
JIKES_DEP_FLAG	= +M


# Where to start installing the class files
CLASS_LIBDIR	= 

# The directory to install the jar file in
JAR_LIBDIR	= 

# The directory to install html files generated by javadoc
DOCDIR          = ./doc


# The dependency graph file
MAKEFILE_DEPEND	= makefile.dep

# The name of the jar file to install
JAR_FILE        = 

# 
# The VERSION variable below should be set to a value 
# that will be tested in the .xjava code. 
# 
VERSION		= JAVA1_0


# ------------------------------------------------------------------- #


# Packages we should compile
PACKAGES = \
	tildeslash




# A marker variable for the top level directory
TOPLEVEL        = .

# Subdirectories with java files:
JAVA_DIRS	= $(subst .,/,$(PACKAGES)) $(TOPLEVEL)

# All the .xjava source files:
XJAVA_SRC	= $(foreach dir, $(JAVA_DIRS), $(wildcard $(dir)/*.xjava))

# All the xjava files to build
XJAVA_OBJS	= $(XJAVA_SRC:.xjava=.java)

# All the .java source files:
JAVA_SRC	= $(foreach dir, $(JAVA_DIRS), $(wildcard $(dir)/*.java))
JAVA_SRC	:= $(JAVA_SRC) $(XJAVA_OBJS)

# Dependency files:
DEPEND_OBJS      = $(JAVA_SRC:.java=.u)

# Objects that should go into the jar file. (find syntax)
JAR_OBJS         = \( -name '*.class' -o -name '*.gif' -o -name "*.au" \)

# All the .java source files we should build:
JAVA_OBJS	= $(XJAVA_OBJS:.java=.class) $(JAVA_SRC:.java=.class)

# All the java .class files we should install (including inner classes):
JAVA_INSTALL_OBJS= $(foreach dir, $(JAVA_DIRS), $(wildcard $(dir)/*.class))

# ------------------------------------------------------------------- #


# Courtesy reint@sys.sol.no:
# Used in $(foreach ...) to check exit value and add a newline in the
# shell commands.  Keep exactly as it is, including the empty line!
define check-exit
|| exit 1

endef

define check-exit-or-clean
|| { $(RM) $@; exit 1; }

endef

# -----------
# Build Rules
# -----------

%.java: %.xjava
	$(CPP) -D$(VERSION) $< $@

%.class: %.java
	$(JAVAC) $(JAVAC_FLAGS) $<

%.jar: $(JAVA_OBJS)
	$(FIND) $(TOPLEVEL) $(JAR_OBJS) -print | $(XARGS) \
	$(JAR) $(JAR_FLAGS) $(JAR_FILE) 

%.u: %.java
	$(JAVAC) $(JIKES_DEP_FLAG) $<


# -------
# Targets
# -------

.PHONY : clean all doc jar install src


all:	src

src:	$(JAVA_OBJS)

help:
	@echo "Usage: make [ src | jar | install | doc | clean | depend ]"

install:
	@echo "Run 'make install-jar' to install a jar file, or run "
	@echo "'make install-classes' to install all classes"


ifneq ($(strip $(JAR_FILE)),)
jar:  $(JAR_FILE)

install-jar: $(JAR_FILE)
	$(INSTALL_DIR) $(JAR_LIBDIR)
	$(INSTALL_FILE) $(JAR_FILE) $(JAR_LIBDIR)
else
install-jar jar: 
	@echo "You must specify a name for the jar file before running"
	@echo "install-jar or jar. See the JAR_FILE variable in the Makefile"
endif


ifneq ($(strip $(CLASS_LIBDIR)),)
install-classes:: $(JAVA_OBJS)
	$(INSTALL_DIR) $(CLASS_LIBDIR)
	$(foreach dir, $(JAVA_DIRS), \
		$(INSTALL_DIR) $(CLASS_LIBDIR)/$(dir) $(check-exit))
	$(foreach file, $(JAVA_INSTALL_OBJS), \
		$(INSTALL_FILE) $(file) $(CLASS_LIBDIR)/$(file) \
	$(check-exit))
else
install-classes: 
	@echo "Install dir for classes is not defined. See CLASS_LIBDIR"
endif


ifeq ($(findstring jikes,$(JAVAC)),jikes)
depend: $(DEPEND_OBJS)
	( $(CAT) $(DEPEND_OBJS) | $(SED) '/java/d' > $(MAKEFILE_DEPEND); \
	  $(RM) $(DEPEND_OBJS); )
else
depend: 
	@echo "You must use the jikes compiler to create a dependency graph"
endif


ifneq ($(strip $(PACKAGES)),)
doc:	$(JAVA_SRC)
	$(JAVADOC) -d $(DOCDIR) $(JAVADOC_FLAGS) $(PACKAGES)
else
doc:
	@echo "You must put your source files in package(s) to run"\
	       "make doc"
endif


clean::
	$(FIND) . \( -name '*~' -o -name '#*' \) -exec $(RM) {} \;
	$(FIND) . -name '*.class' -exec $(RM) {} \;


ifeq ($(shell test -d $(DOCDIR) && echo "true"),true)
clean::
	$(FIND) $(DOCDIR) -name '*.html' -exec $(RM) {} \;
endif

ifneq ($(strip $(XJAVA_SRC)),)
clean::
	$(RM) $(XJAVA_OBJS)
endif



# ----------------------------------------
# Include the dependency graph if it exist
# ----------------------------------------
DEPEND	= $(shell test -r $(MAKEFILE_DEPEND) && echo "true")
ifeq ($(DEPEND),true)
	include $(MAKEFILE_DEPEND)
endif

