   #***************************************************************************
   #
   #  (C) 2000 Geotechnical Software Services - GeoSoft
   #
   #***************************************************************************


   #***************************************************************************
   #
   #  Section 1: Directories.
   #
   #***************************************************************************

   CLASS_DIR        = $(JAVA_DEV_ROOT)/classes
   JAVADOC_DIR      = $(JAVA_DEV_ROOT)/docs/javadoc
   MAKE_DIR         = $(JAVA_DEV_ROOT)/make
   JAR_DIR          = $(JAVA_DEV_ROOT)/jars

   ifdef JAVA_DEV_LOCAL
   SOURCE_DIR       = $(JAVA_DEV_LOCAL)/src
   LOCAL_CLASS_DIR  = $(JAVA_DEV_LOCAL)/classes
   DESTINATION      = $(JAVA_DEV_LOCAL)/classes
   else
   SOURCE_DIR       = $(JAVA_DEV_ROOT)/src
   LOCAL_CLASS_DIR  = .
   DESTINATION      = $(JAVA_DEV_ROOT)/classes
   endif

   ifndef JAVA5_HOME
   JAVA5_HOME       = $(JAVA_HOME)
   endif

   JAVA_BIN         = $(JAVA_HOME)/bin


   #***************************************************************************
   #
   #  Section 2. Tools and options.
   #
   #***************************************************************************

   JAVA     = java
   ifndef JAVAC
     JAVAC    = javac
   endif

   JAVAH    = javah
   RMIC     = rmic
   JAR      = jar
   DEBUG    = jdb
   DELETE   = rm -f
   COPY     = cp
   JINDENT  = Jindent
   PROFILER = -Xrunhprof
   MAKEDIR  = mkdir -p
   PRINT    = @echo
   JAVADOC  = javadoc
   CHMOD    = chmod.exe

   ifdef IS_UNIX
   SEP = :
   else
   SEP = ;
   endif

   EMPTY            =
   SPACE            = $(EMPTY) $(EMPTY)

   LOCAL_JARTMP     = $(patsubst %,$(JAR_DIR)/%,$(JARS))
   LOCAL_JARLIST    = $(subst $(SPACE),$(SEP),$(LOCAL_JARTMP))

   OTHER_JARTMP     = $(patsubst %,$(JAR_DIR)/%,$(JARS_3RDPARTY))
   OTHER_JARLIST    = $(subst $(SPACE),$(SEP),$(OTHER_JARTMP))

   JRE              = $(JAVA_HOME)/jre/lib/rt.jar

   SOURCEPATH       = $(SOURCE_DIR)

   #CLASSPATH        = $(JRE)$(SEP)$(LOCAL_CLASS_DIR)$(SEP)$(CLASS_DIR)$(SEP)$(LOCAL_JARLIST)$(OTHER_JARLIST)$(SEP)$(JAVA_DEV_ROOT)/classes
   CLASSPATH        = $(JRE)$(SEP)$(LOCAL_CLASS_DIR)$(SEP)$(CLASS_DIR)$(SEP)$(JAVA_DEV_ROOT)/classes

   JAVAC_OPTIONS    = -encoding ascii -g -d $(DESTINATION) -classpath $(CLASSPATH) -sourcepath $(SOURCEPATH) -deprecation 
   JAVAC5_OPTIONS    = -encoding ascii -g -d $(DESTINATION) -classpath $(CLASSPATH) -sourcepath $(SOURCEPATH) -deprecation -target 1.3 -source 1.5

   JAVA_OPTIONS     = -classpath $(CLASSPATH) 
   RMIC_OPTIONS     = -d $(CLASS_DIR) -classpath $(CLASSPATH)
   JAR_OPTIONS      = -cvmf
   JINDENT_OPTIONS  = -p $(MAKE_DIR)/style.jin
   PROFILER_OPTIONS = cpu=samples,depth=6

   JAVADOC_OPTIONS  = \
           -d $(JAVADOC_DIR) \
           -sourcepath $(SOURCE_DIR) \
           -classpath $(CLASSPATH) \
	   -overview $(JAVADOC_DIR)/overview.html \
           -author \
           -package \
           -use \
           -splitIndex \
           -version \
           -windowtitle $(WINDOWTITLE) \
           -doctitle $(DOCTITLE) \
           -header $(HEADER) \
           -bottom $(BOTTOM)

   # Flag whether to continue when make errors occur
   ### CONTINUE_FLAG = -k
   CONTINUE_FLAG = 


   #***************************************************************************
   #
   #  Section 3. Rules and dependencies.
   #
   #  This section defines the exact rules for creating a target file from
   #  a (set of) source file(s). The rules can be quite complex and the
   #  makefile syntax is not extreamly readable. A quick crash course:
   #
   #  target : depends
   #    rule
   #
   #  target  - the parameter given to make: What to build
   #  depends - file or other targets target depends on
   #  rule    - how to create target
   #  $(VAR)  - environment variable or variable defined above
   #  $@      - Current target
   #  $*      - Current target without extension
   #  $<      - Current dependency
   #
   #***************************************************************************


   PACKAGE_LOC     = $(subst .,/,$(PACKAGE))
   PACKAGE_DIR     = $(DESTINATION)/$(PACKAGE_LOC)
   JAVA_FILES      = $(filter  %.java,$(SOURCE))
   NONJAVA_FILES   = $(patsubst %.java,,$(SOURCE))
   CLASS_FILES     = $(JAVA_FILES:%.java=$(PACKAGE_DIR)/%.class)
   OTHER_FILES     = $(NONJAVA_FILES:%=$(PACKAGE_DIR)/%)
   JNI_CLASS_FILES = $(JNI_SOURCE:%.java=$(PACKAGE_DIR)/%.class)
   JNI_HEADERS     = $(JNI_SOURCE:%.java=%.h)
   RMI_CLASS_FILES = $(RMI_SOURCE:%.java=$(PACKAGE_DIR)/%.class)
   RMI_STUB_FILES  = $(RMI_SOURCE:%.java=$(PACKAGE_DIR)/%_Stub.class)
   JAVA5_FILES     = $(filter  %.java,$(JAVA5_SOURCE))
   JAVA5_CLASS_FILES = $(JAVA5_FILES:%.java=$(PACKAGE_DIR)/%.class)
   
#   Remove this comment and enable rule #6 if you want skeleton files
#   RMI_SKEL_FILES  = $(RMI_SOURCE:%.java=$(PACKAGE_DIR)/%_Skel.class)

   ALL_CLASS_FILES = $(CLASS_FILES) $(RMI_STUB_FILES) $(RMI_SKEL_FILES)
   JAR_CONTENT_CMD = $(patsubst %,-C $(CLASS_DIR) %,$(JAR_CONTENT))


   # Make a list of all packages involved
   ifdef PACKAGE
   PACKAGE_LIST    = $(subst .,/,$(PACKAGE))
   MAIN_CLASS      = $(MAIN)
   MAIN_PACKAGE    = $(PACKAGE)
   else
   PACKAGE_LIST    = $(subst .,/,$(PACKAGES)) $(subst .,/,$(NODOC_PACKAGES))
   endif

   PLIST_CLEAN     = $(patsubst %,$(SOURCE_DIR)/%/.clean,$(PACKAGE_LIST))
   PLIST_BUILD     = $(patsubst %,$(SOURCE_DIR)/%/.build,$(PACKAGE_LIST))
   JAVA5_PLIST     = $(patsubst %,$(SOURCE_DIR)/%/.build5,$(PACKAGE_LIST))


   # Rule 0. Applied when make is called without targets. Invokes rule 10.
   default : buildall


   # Rule 1. Building a .class file from a .java file
   $(PACKAGE_DIR)/%.class :: $(SOURCE_DIR)/$(PACKAGE_LOC)/%.java
	$(JAVA_BIN)/$(JAVAC) $(JAVAC_OPTIONS) $< 


   # Rule 2. Building a .class file from a .java file. Invokes rule 1.
   %.class : $(SOURCE_DIR)/$(PACKAGE_LOC)/%.java
	$(MAKE) $(CONTINUE_FLAG) -C $(dir $@) $(notdir $@)


   # Rule 3. Building a JNI .h stub file from a .class file
   $(SOURCE_DIR)/$(PACKAGE_LOC)/%.h : $(PACKAGE_DIR)/%.class
	$(JAVA_BIN)/$(JAVAH) $(JAVAH_OPTIONS) $(PACKAGE).$*


   # Rule 4. Building a JNI .h stub file from a class file. Invokes rule 3.
   %.h : %.class
	$(MAKE) $(CONTINUE_FLAG) -C $(dir $@) $(notdir $@)


   # Rule 5. Building an RMI _Stub.class file from a .class file 
   %_Stub.class :: %.class
	$(JAVA_BIN)/$(RMIC) $(RMIC_OPTIONS) $(PACKAGE).$(notdir $*)


   # Optional Rule 6. Building an RMI _Skel.class file from a .class file
   #remove the comment before && if you want to avoid remaking every time and
   # can tolorate empty .class files
#   %_Skel.class :: %.class
#	$(JAVA_BIN)/$(RMIC) $(RMIC_OPTIONS) $(PACKAGE).$(notdir $*) # && touch $@ $*_Stub.class

   # rules 7 & 8 were incorporated into 5 & 6 -- 5/25/02 brent@mbari.org
   
   # Rule 9. Default behaviour within a package: Simply copy the object from src
   # to classes. Note that the location of this rule is important. It must be after
   # the package specifics.
   $(PACKAGE_DIR)/% :: $(SOURCE_DIR)/$(PACKAGE_LOC)/%
	$(MAKEDIR) $(PACKAGE_DIR)  #make package directories if they don't exist
	$(COPY) $< $@
	$(CHMOD) u+rw $@


   # Rule 10. Build class files rmi stub and skeletons and process all other source
   all : $(CLASS_FILES) $(RMI_STUB_FILES) $(RMI_SKEL_FILES) $(OTHER_FILES)


   # Rule 11. Build JNI .h files. Invokes rule 4.
   jni       : $(JNI_CLASS_FILES) $(JNI_HEADERS)


   # Rule 12. Build RMI stubs and skeleton files. Invokes rule 7. and rule 8.
   rmi       : $(RMI_CLASS_FILES) $(RMI_STUB_FILES) $(RMI_SKEL_FILES)


   # Rule 13. Remove all produced files (except javadoc)
   cleanall :
	$(DELETE) $(PACKAGE_DIR)/*.class $(OTHER_FILES) $(JNI_HEADERS)


   # Rule 14. Change ".clean" tag to "Makefile", thus call the package makefile which
   # in turn recalls this makefile with target cleanall (rule 13).
   %.clean :
	$(MAKE) $(CONTINUE_FLAG) -C $(dir $@) cleanall


   # Rule 15: Call rule 14 for every package directory
   clean : $(PLIST_CLEAN)
	$(PRINT) Done clean.


   # Rule 16. Change ".build" tag to "Makefile", thus call the package makefile which
   # in turn recalls this makefile with target all (rule 10).
   %.build :
	$(MAKE) $(CONTINUE_FLAG) -C $(dir $@) all


   # Rule 17. Call rule 16 for every package
   buildall : $(PLIST_BUILD)               
	$(PRINT) Done build.


   # Rule 18. Build a jar file. $* strips the last phony .JAR extension.
   %.JAR :
	$(DELETE) $(JAR_DIR)/$*
	$(JAVA_BIN)/$(JAR) $(JAR_OPTIONS) $(JAR_DIR)/$(MANIFEST) \
	$(JAR_DIR)/$* $(JAR_CONTENT_CMD)
   #       $(JAVA_BIN)/$(JAR) -i $(JAR_DIR)/$@


   # Rule 19. Create given jar file by invoking its Makefile which triggers rule 18
   %.jar :
	$(MAKE) $(CONTINUE_FLAG) -f $(patsubst %,$(JAR_DIR)/Makefile.%,$@) $@.JAR


   # Rule 20. Create all jar files by invoking rule 19
   jar : $(JARS)
	$(PRINT) Done jars.


   # Rule 21. Build javadoc for all listed packages
   javadoc :
	$(PRINT) $(PACKAGES) > $(JAVA_DEV_ROOT)/packages.tmp
	$(JAVA_BIN)/$(JAVADOC) $(JAVADOC_OPTIONS) @$(JAVA_DEV_ROOT)/packages.tmp
	$(DELETE) $(JAVA_DEV_ROOT)/packages.tmp
	$(PRINT) Done JavaDoc.


   # Rule 22. Run application using classes tree
   run :
	$(JAVA_BIN)/$(JAVA) $(JAVA_OPTIONS) $(MAIN_PACKAGE).$(MAIN_CLASS) \
                   $(RUN_PARAMETERS)


   # Rule 23. Run application using jar archive
   runjar :
	$(JAVA_BIN)/$(JAVA) -jar $(JAR_DIR)/$(MAIN_JAR) $(RUN_PARAMETERS)


   # Rule 24. Run debugger
   debug :
	$(JAVA_BIN)/$(DEBUG) $(JAVA_OPTIONS) $(PACKAGE).$(MAIN)


   # Rule 25. Run the auto indentation tool. Experimental setup. 
   indent :
	$(JAVA_BIN)/$(JAVA) -classpath $(JAR_3RDPARY_DIR)/Jindent.jar \
                   $(JINDENT) $(JINDENT_OPTIONS) -f "*.java"


   # Rule 26. Run profiler.
   profile :
	$(JAVA_BIN)/$(JAVA) $(PROFILER):$(PROFILER_OPTIONS) $(JAVA_OPTIONS) \
                   $(PACKAGE).$(MAIN)


   # Rule 27. A combination of steps used for automatic building
   complete : clean buildall jar javadoc

  java5_files: $(JAVA5_CLASS_FILES)

  %.build5 :
	$(MAKE) $(CONTINUE_FLAG) -C $(dir $@) JAVA_HOME=$(JAVA5_HOME) JAVAC_OPTIONS='$(JAVAC5_OPTIONS)' java5_files

  java5 : $(JAVA5_PLIST)               

#
# Use env.sh to define the following 
# environment variables
# JAVA_DEV_ROOT (Path to top build directory)
# XPORT_HOST (IP address/hostname of XPort)

# Environment variable defaults
# Use env.sh to override
#
COB_FILE ?= xport.cob
XPORT_HTML ?= xport.html
XPORT_JAR ?= xport.jar
LED_GIF ?= CP_CTL_rbs.gif
HTML_TEMPLATE ?= xport.tpl
TEST_URL ?= test-url

#
# Directory structure and paths
#
BIN_DIR=$(JAVA_DEV_ROOT)/bin
BUILD_DIR=$(JAVA_DEV_ROOT)/build
BUILD_SRC=$(BUILD_DIR)/src
BUILD_BIN=$(BUILD_DIR)/bin
IMG_DIR=$(SOURCE_DIR)/img
TEMPLATE_DIR=$(SOURCE_DIR)/template

#
# Lantronix COB composition tool
#
WEB2COB=$(BIN_DIR)/web2cob

#
# Java Applet Viewer utility
#
APPLETVIEWER=appletviewer

.PHONY: cob view help

help:
	@echo " "
	@echo " #########################"
	@echo " # XPort Applet Targets"
	@echo " #########################"
	@echo " "
	@echo " The following primary make targets are available"
	@echo " "
	@echo "  - release       : build Java classes and XPort applet COB file"
	@echo "  - test          : test the applet in appletviewer"
	@echo "  - xp_clean      : remove java classes and release files"
	@echo "  - clean         : remove java classes only"
	@echo "  - release_clean : remove release files created in build directory only"
	@echo " "
	@echo " The following intermediate make targets are available"
	@echo "  - xport_jar     : build applet resources JAR file"
	@echo "  - xport_html    : build applet HTML"
	@echo "  - build_dirs    : create build directory structure"
	@echo "  - cob           : build COB from xport resources (xport_jar and xport_html)"
	@echo " "
	@echo " Edit and source env.sh before building the XPort applet "
	@echo " "

xp_clean: clean release_clean

release_clean:
	-rm -f $(BUILD_BIN)/$(COB_FILE)
	-rm -f $(BUILD_SRC)/$(XPORT_JAR) $(BUILD_SRC)/$(XPORT_HTML) $(BUILD_SRC)/$(TEST_URL)

build_dirs:
	mkdir -p $(CLASS_DIR)
	mkdir -p $(BUILD_SRC)
	mkdir -p $(BUILD_BIN)

xport_jar:
	jar cvf $(BUILD_SRC)/$(XPORT_JAR) -C $(CLASS_DIR) org/ 
	jar uvf $(BUILD_SRC)/$(XPORT_JAR) -C $(IMG_DIR) $(LED_GIF)

xport_html:
	echo "setting XPORT_HOST=$(XPORT_HOST)"
	sed -e "s/<XPORT_HOST>/$(XPORT_HOST)/g" $(TEMPLATE_DIR)/$(HTML_TEMPLATE) > $(BUILD_SRC)/$(XPORT_HTML)

cob:
	-cd $(BIN_DIR); \
	$(WEB2COB) /d $(BUILD_SRC) /o $(BUILD_BIN)/$(COB_FILE)

release: buildall build_dirs release_clean xport_jar xport_html cob

test:
	cd $(BUILD_SRC); \
	$(APPLETVIEWER) $(XPORT_HTML)
