# JAF Makefile
#
# Summary - 
#
# 'make' will compile Java source in the src directory
# 'make foo.cfg' will create the foo agent's gnt file
# 'make foo.gnt' will run the agent configured in foo.gnt
#
# Troubleshooting -
# 
# If the Makefile fails to be interpreted, make sure
# you are using GNU make.  The presence of various
# GNU utilities is also assumed (e.g. perl, grep, sed,
# find, uname, sort, uniq).  You may also need to
# to redefine the JAVART variable below.
#
# See the the Rules section of this file and the
# accompanying README file for more information.
# #######################################################

###
# Vars
###

# Locations and such
DIR	= .
LIBS	= $(DIR)/lib
CLASSD	= $(DIR)/classes
SRCD	= $(DIR)/src
DOCD	= $(DIR)/doc
SCRIPTS	= $(DIR)/bin
JAVART	= 

# Constants
SEP	= :
EMPTY	=
SPACE	= $(EMPTY) $(EMPTY)

# More locations - you may have to change these if
# your java executable is not in your path.
#
# JAVART should point to the directory containing
# your java executables (java, javac, jar).
ARCH	= $(shell uname -s)
ifeq "$(ARCH)" "Linux"
	#JAVART	= /usr/local/jdk2/bin/
	#JAVART	= /usr/maslocal/jdk2/bin/
endif
ifeq "$(ARCH)" "OSF1"
	#JAVART	= /usr/bin/
endif
ifeq "$(ARCH)" "SunOS"
	#JAVART	= /usr/bin/
endif
ifeq "$(ARCH)" "Darwin"
	#JAVART	= /usr/bin/
endif
ifeq "$(OSTYPE)" "cygwin"
        #JAVART = /cygdrive/c/jdk/bin/
        JAVART  = /cygdrive/c/jdk1.3.1_01/bin/
        SEP     = \;
endif

# Programs
JAVAC	= $(JAVART)javac
JAVA	= $(JAVART)java
JAR	= $(JAVART)jar
JAVADOC	= $(JAVART)javadoc
PERL	= perl
MMANIF	= $(SCRIPTS)/makemanifest
MCONFIG	= $(SCRIPTS)/makeconfig
MAGENV	= $(SCRIPTS)/makeagentenv

# Files
JARFILE	= $(DIR)/newagent.jar
JLIBS	= $(wildcard $(LIBS)/*[".jar"|".zip"])
CLASSPTH= $(CLASSPATH)$(SEP)$(CLASSD)$(SEP)$(subst $(SPACE),$(SEP),$(strip $(JLIBS)))

# Lists of things
JAVASRC = $(shell find $(SRCD) -name "*.java" -print)
JAVAPAK	= $(shell grep "^\\s*package.*;\\s*$$" $(JAVASRC) | sed -e "s/.*package *\(.*\);$$/\1/i" | sort | uniq)
JDEPS   = $(patsubst $(SRCD)/%.java, $(CLASSD)/%.class, $(JAVASRC))
CLASSES = cd $(CLASSD); find . -name "*.class" -print | sed -e's/\$$/\\$$/g'
AGENTENV= $(shell $(MAGENV))

###
# Rules
###

# By default, we compile
all: $(JDEPS)

# Compiles .java files
$(CLASSD)/%.class: $(SRCD)/%.java
	$(JAVAC) -g -d $(CLASSD) -deprecation -sourcepath $(SRCD) \
	  -classpath $(CLASSPTH) \
	  $<

# Runs the agent specified in the .gnt file
%.gnt: FORCE
	$(JAVA) -classpath $(CLASSPTH) $(AGENTENV) \
	  agent.mass.Control --config $@ 2>&1 | tee $*.log 

# Creates the .gnt file from the .cfg file
%.cfg: FORCE
	$(PERL) $(MCONFIG) $@

# Generates the javadoc documentation from the source files
doc: FORCE
	-@mkdir $(DOCD)
	$(JAVADOC) -public -d $(DOCD) -version -author \
	  -sourcepath $(SRCD) \
	  -classpath $(CLASSPTH) \
	  -link http://mas.cs.umass.edu/research/mass/api/agent/ \
	  -link http://mas.cs.umass.edu/research/mass/api/ihome/ \
	  -link http://mas.cs.umass.edu/research/mass/api/utilities/ \
	  -link http://mas.cs.umass.edu/research/mass/api/taems/ \
	  $(JAVAPAK)

# Generates a jar file containing the class files produced by
# the .java files in src.  Use this if you need to migrate your
# components to a different environment without moving the
# source code.
jar: $(JARFILE)

$(JARFILE): $(JDEPS)
	@/bin/rm -f manifest.tmp
	$(PERL) $(MMANIF) $(JAVASRC) >> manifest.tmp
	@mv -f manifest.tmp $(CLASSD)
	cd $(CLASSD); $(JAR) cfm tmp.jar manifest.tmp $(shell $(CLASSES)) $(DATA)
	@rm -f $(CLASSD)/manifest.tmp
	@mv $(CLASSD)/tmp.jar $(JARFILE)

# Views a .log file with the LogViewer component
%.log: FORCE
	$(JAVA) -classpath $(CLASSPTH) $(AGENTENV) agent.simplest.LogViewer $@

# Cleans up the JAF directory
clean: 
	cd $(CLASSD); rm -rf $(shell $(CLASSES))
	cd $(SCRIPTS); rm -f *.alternatives* *people *.ttaems* *schedule*
	rm -f $(JARFILE)

# Prints out some relevant variables
debug:
	@ echo "      ARCH = $(ARCH)"
	@ echo "    OSTYPE = $(OSTYPE)"
	@ echo "  HOSTTYPE = $(HOSTTYPE)"
	@ echo "       DIR = $(DIR)"
	@ echo "    JAVART = $(JAVART)"
	@ echo "     JAVAC = $(JAVAC)"
	@ echo "      JAVA = $(JAVA)"
	@ echo "      LIBS = $(LIBS)"
	@ echo "     JLIBS = $(JLIBS)"
	@ echo "  CLASSPTH = $(CLASSPTH)"
	@ echo "      Host = `uname -a`"
	@ echo ""
	@ echo "JAF Control component help:"
	@$(JAVA) -classpath $(CLASSPTH) agent.simplest.Control --help

FORCE: ;
