#
# Makefile created at Sun May  2 09:11:53 1999, by mmake
#


CLASSPATH := $(CLASSPATH):../lib/swing.jar
export CLASSPATH

# 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      = 555
MODE_FILES      = 444
MODE_DIRS       = 2755

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

# Build flags
JAVAC_FLAGS     = 
JAVADOC_FLAGS   = -version -author -use -header "<H2>TsBiff 1.3</H2>" \
                  -splitindex
JAR_FLAGS       = cvf0
JIKES_DEP_FLAG	= +M

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

# Prefix for every install directory
PREFIX		= ../

# Where to start installing the class files. Set this to an empty value
#  if you dont want to install classes
CLASS_DIR	=

# The directory to install the jar file in. Set this to an empty value
#  if you dont want to install a jar file
JAR_DIR	        = $(PREFIX)lib

# The directory to install html files generated by javadoc
DOC_DIR         = $(PREFIX)doc/api-docs

# The directory to install script files in
SCRIPT_DIR	= $(PREFIX)bin

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

# The name of the jar file to install
JAR_FILE        = tsbiff.jar

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

#
# Group the conditional directives
#
DEFINES		= 

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


# Packages we should compile
PACKAGES = \
	ts.mail \
	ts.ui \
	ts.config \
	ts.biff \
	ts \
	ts.event



# Directories with shell scripts
SCRIPTS = \
	script



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

# 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" \
		       -o -name '*.properties' \)

# The main classes 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))
# Escape inner class delimiter $
JAVA_INSTALL_OBJS:= $(subst $$,\$$,$(JAVA_INSTALL_OBJS))



# All the shell scripts source
SCRIPT_SRCS 	:= $(foreach dir, $(SCRIPTS), $(wildcard $(dir)/*.sh))
# All shell scripts we should install
SCRIPT_OBJS    	:= $(SCRIPT_SRCS:.sh=)


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


define check-exit
|| exit 1

endef


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

%.java: %.xjava
	$(CPP) $(DEFINES) $< $@

%.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


all::	$(JAVA_OBJS)

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


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


# Jar target
ifneq ($(strip $(JAR_FILE)),)
jar:  $(JAR_FILE)

ifneq ($(strip $(JAR_DIR)),)
install:: $(JAR_FILE)
	@echo "===> [Installing jar file, $(JAR_FILE) in $(JAR_DIR)] "
	$(INSTALL_DIR) $(JAR_DIR) $(check-exit)
	$(INSTALL_FILE) $(JAR_FILE) $(JAR_DIR) $(check-exit)
else
# Print a warning here if you like. (No jar install dir defined)
endif
else
# Print a warning here if you like. (No jar file defined)
endif




# Class install target
ifneq ($(strip $(CLASS_DIR)),)
install:: $(JAVA_OBJS)
	@echo "===> [Installing classes in $(CLASS_DIR)] "
	$(INSTALL_DIR) $(CLASS_DIR) $(check-exit)
	$(foreach dir, $(JAVA_DIRS), \
		$(INSTALL_DIR) $(CLASS_DIR)/$(dir) $(check-exit))
	$(foreach file, $(JAVA_INSTALL_OBJS), \
		$(INSTALL_FILE) $(file) $(CLASS_DIR)/$(file) \
	$(check-exit))
else
# Print a warning here if you like. (No class install dir defined)
endif




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




# Doc target
ifneq ($(strip $(PACKAGES)),)
doc:	$(JAVA_SRC)
	@echo "===> [Installing java documentation in $(DOC_DIR)] "
	$(INSTALL_DIR) $(DOC_DIR) $(check-exit)
	$(JAVADOC) -d $(DOC_DIR) $(JAVADOC_FLAGS) $(PACKAGES)
else
doc:
	@echo "======================> [Warning] <==========================="
	@echo "You must put your source files in package(s) to run make doc"
	@echo "=============================================================="
endif




# Script target
ifneq ($(strip  $(SCRIPT_OBJS)),)
all::	 $(SCRIPT_OBJS)

ifneq ($(strip $(SCRIPT_DIR)),)
install:: $(SCRIPT_OBJS)
	@echo "===> [Installing shell-scripts in $(SCRIPT_DIR)] "
	$(INSTALL_DIR) $(SCRIPT_DIR) $(check-exit)
	$(foreach file, $(SCRIPT_OBJS), \
		$(INSTALL_PROG) $(file) $(SCRIPT_DIR) $(check-exit))
else
# Print a warning here if you like. (No script install dir defined)
endif

clean::
	rm -f $(SCRIPT_OBJS)
endif



# Various cleanup routines
clean::
	$(FIND) . \( -name '*~' -o -name '*.class' \) -print | $(XARGS) $(RM) 
	$(FIND) . -name '*.u' -print | $(XARGS) $(RM) 


ifneq ($(strip $(JAR_FILE)),)
clean::
	$(RM) $(JAR_FILE)
endif

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



# ----------------------------------------
# Include the dependency graph if it exist
# ----------------------------------------

MAKEFILE_DEPEND	= makefile.dep

DEPEND	= $(shell test -r $(MAKEFILE_DEPEND) && echo "true")
ifeq ($(DEPEND),true)
	include $(MAKEFILE_DEPEND)
endif

