# Xabsl2Makefile
#
# Generates the documentation, intermediate code and debugging symbols for all an Xabsl2 project.
#
# author: Martin Ltzsch
# 
# This Makefile should be includedd into a custom Makefile inside the directory, where the XML sources are.
# Inside this custom Makefile these Variables have to be set:
#
# XSLT:                 An XSLT processor that can process XInclude statements (with necessary parameters)
# DOT:                  Path of the dot tool 
# DOTML_DIR:            Directory that contains the DotML Schemas and XSLT stylesheets
# SCHEMA_VALIDATOR:     Validates an XML file against the schemas referenced in the source file taking the input from stdin
# XABSL_DIR:            Directory of the XABSL Schemas and XSLT Stylesheets relative to the source files and the custom Makefile
# XABSL_OUTPUT_DIR:     Directory where the intermediate code and the debug symbols shall be generated
# DOC_OUTPUT_DIR:       Directory for the documentation output
# INSTANCE_DIR:         Directory containing the sources relative to the Xabsl2/xabsl-2.2 directory
# DEBUG_SYMBOLS:        Path of the debug symbols to be generated
# INTERMEDIATE_CODE:    The path of the intermediate code to be generated
# SYMBOL_FILES:         All symbol files
# BASIC_BEHAVIOR_FILES: All basic behavior files
# OPTION_FILES:         All Option files. The have to be all in a directory "Options" inside the directory of agents.xml
#



# Schema files (needed for validation)
SCHEMA_FILES     = $(XABSL_DIR)/xabsl-2.2.agent-collection.xsd \
                   $(XABSL_DIR)/xabsl-2.2.basic-behaviors.xsd \
                   $(XABSL_DIR)/xabsl-2.2.symbols.xsd \
                   $(XABSL_DIR)/xabsl-2.2.option.xsd \
                   $(XABSL_DIR)/xabsl-2.2.option-definitions.xsd \
                   $(XABSL_DIR)/xabsl-2.2.expressions.xsd \
                   $(XABSL_DIR)/xabsl-2.2.parameter.xsd 
                    


# General documentation output files
DOC_INDEX_HTML   = $(DOC_OUTPUT_DIR)/index.html
DOC_OPTION_TREE_XML = $(DOC_OUTPUT_DIR)/option-tree.xml
DOC_AGENTS_HTML = $(DOC_OUTPUT_DIR)/agents.html
DOC_SYMBOLS_INDEX_HTML = $(DOC_OUTPUT_DIR)/symbols.html
DOC_OPTION_INDEX_HTML = $(DOC_OUTPUT_DIR)/options.html
DOC_BASIC_BEHAVIOR_INDEX_HTML = $(DOC_OUTPUT_DIR)/basic-behaviors.html
DOC_STYLES_CSS   = $(DOC_OUTPUT_DIR)/styles.css
VALIDATION       = $(DOC_OUTPUT_DIR)/valid #This is just a temporary file to prevent make from building the target VALIDATION always


# Documentation XSLT Stylesheets
DOC_INDEX_XSL    = $(XABSL_DIR)/generate-documentation.index.xsl
DOC_MENU_XSL     = $(XABSL_DIR)/generate-documentation.menu.xsl
DOC_SYMBOLS_XSL  = $(XABSL_DIR)/generate-documentation.symbols.xsl
DOC_SYMBOLS_INDEX_XSL = $(XABSL_DIR)/generate-documentation.symbols-index.xsl
DOC_AGENTS_XSL    = $(XABSL_DIR)/generate-documentation.agents.xsl
DOC_BASIC_BEHAVIORS_XSL = $(XABSL_DIR)/generate-documentation.basic-behaviors.xsl
DOC_BASIC_BEHAVIOR_INDEX_XSL = $(XABSL_DIR)/generate-documentation.basic-behavior-index.xsl
DOC_OPTION_XSL   = $(XABSL_DIR)/generate-documentation.option.xsl
DOC_OPTION_INDEX_XSL = $(XABSL_DIR)/generate-documentation.option-index.xsl
DOC_OPTION_TREE_XSL = $(XABSL_DIR)/generate-documentation.option-tree.xsl
DOC_PSEUDO_CODE_XSL = $(XABSL_DIR)/generate-documentation.pseudo-code.xsl
DOC_PARAMETERS_XSL = $(XABSL_DIR)/generate-documentation.parameters.xsl
REMOVE_COMMENTS_XSL = $(XABSL_DIR)/remove-comments.xsl

# XSLT Stylesheet for debug symbols
DEBUG_SYMBOLS_XSL = $(XABSL_DIR)/generate-debug-symbols.xsl

# XSLT Stylesheet for intermediate code
INTERMEDIATE_CODE_XSL = $(XABSL_DIR)/generate-intermediate-code.xsl

# First validate the files, then generate debug symbols, intermediate code
all: $(DOC_OUTPUT_DIR) $(VALIDATION) $(DEBUG_SYMBOLS) $(INTERMEDIATE_CODE) #DOCUMENTATION
	@if test -f agents.xinclude-processed.xml; then echo removing `pwd`/agents.xinclude-processed.xml; rm agents.xinclude-processed.xml; fi 

# Some shortcuts for common targets:
IC:    $(INTERMEDIATE_CODE)
DS:    $(DEBUG_SYMBOLS)
IC_DS: $(DEBUG_SYMBOLS) $(INTERMEDIATE_CODE) $(VALIDATION) 
VALID: $(VALIDATION) 
DOC:   DOCUMENTATION	
AGENTS: $(DOC_AGENTS_HTML);

agents.xinclude-processed.xml: agents.xml options.xml $(SYMBOL_FILES) $(BASIC_BEHAVIOR_FILES) $(OPTION_FILES) $(REMOVE_COMMENTS_XSL)
	@echo generating `pwd`/agents.xinclude-processed.xml 
	@$(XSLT) -o agents.xinclude-processed.xml $(REMOVE_COMMENTS_XSL) agents.xml
	

# Documenation needs and output directory, the .css file, index pages and a page for 
# each symbols, basic-behaviors, and options file.
DOCUMENTATION: $(DOC_OUTPUT_DIR)/svg \
               $(DOC_OPTION_TREE_XML) \
               $(DOC_STYLES_CSS) \
               $(DOC_INDEX_HTML) \
               $(DOC_SYMBOLS_INDEX_HTML) \
               $(DOC_BASIC_BEHAVIOR_INDEX_HTML) \
               $(DOC_OPTION_INDEX_HTML) \
               $(shell echo $(SYMBOL_FILES)         |sed 's%[^ ]*/%%g' |sed "s%\([^ ]*\)\.xml%$(DOC_OUTPUT_DIR)/symbols.\1.html%g") \
               $(shell echo $(BASIC_BEHAVIOR_FILES) |sed 's%[^ ]*/%%g' |sed "s%\([^ ]*\)\.xml%$(DOC_OUTPUT_DIR)/basic-behaviors.\1.html%g") \
               $(shell echo $(OPTION_FILES)         |sed 's%[^ ]*/%%g' |sed "s%\([^ ]*\)\.xml%$(DOC_OUTPUT_DIR)/option.\1.html%g") \
               $(DOC_AGENTS_HTML) \

# Generic rule for option documentation               
$(DOC_OUTPUT_DIR)/option.%.html: Options/%.xml \
               $(DOC_OPTION_XSL) $(DOC_OPTION_TREE_XSL) $(DOC_PSEUDO_CODE_XSL) $(DOC_PARAMETERS_XSL) $(DOC_MENU_XSL)
	@echo $@ 
	@$(XSLT) --stringparam option-tree-xml $(INSTANCE_DIR)/$(DOC_OPTION_TREE_XML) -o $@.temp $(DOC_OPTION_XSL) $< 
	@$(XSLT) -o $@ $(DOTML_DIR)/embed-svg-graphics.xsl $@.temp
	@DOTML_DIR=$(DOTML_DIR) DOT=$(DOT) DOTML_XSLT="$(XSLT) (XSL) (INPUT)" $(DOTML_DIR)/generate-svg-graphics.bash $@.temp $(DOC_OUTPUT_DIR)
	@rm $@.temp 

# Generic rule for symbol documentation
$(DOC_OUTPUT_DIR)/symbols.%.html: $(DOC_MENU_XSL) $(SYMBOL_FILES) $(DOC_SYMBOLS_XSL) 
	@echo $@
	@$(XSLT) -o $@ $(DOC_SYMBOLS_XSL) $(shell echo " " $(SYMBOL_FILES) | sed "s=.* \([^ ]*$*.xml\).*=\1=;")
	
# Generic rule for basic behavior documentation
$(DOC_OUTPUT_DIR)/basic-behaviors.%.html: $(DOC_MENU_XSL) \
               $(BASIC_BEHAVIOR_FILES) $(DOC_BASIC_BEHAVIORS_XSL) $(DOC_PARAMETERS_XSL)
	@echo $@
	@$(XSLT) -o $@ $(DOC_BASIC_BEHAVIORS_XSL) $(shell echo " " $(BASIC_BEHAVIOR_FILES) | sed "s=.* \([^ ]*$*.xml\).*=\1=;")
	
# Generates the index page
$(DOC_INDEX_HTML): $(DOC_MENU_XSL) $(DOC_INDEX_XSL) agents.xml 
	@make -s agents.xinclude-processed.xml
	@echo $(DOC_INDEX_HTML) 
	@$(XSLT) -o $(DOC_INDEX_HTML) $(DOC_INDEX_XSL) agents.xinclude-processed.xml

# Generates the option index page
$(DOC_OPTION_INDEX_HTML): options.xml $(DOC_OPTION_INDEX_XSL) $(DOC_MENU_XSL) 
	@make -s agents.xinclude-processed.xml
	@echo $(DOC_OPTION_INDEX_HTML) 
	@$(XSLT) -o $(DOC_OPTION_INDEX_HTML) $(DOC_OPTION_INDEX_XSL) options.xml 

# Generates the agent index page
$(DOC_AGENTS_HTML): agents.xml $(DOC_AGENTS_XSL) $(DOC_MENU_XSL) $(DOC_OPTION_TREE_XSL)
	@make -s agents.xinclude-processed.xml
	@echo $(DOC_AGENTS_HTML) 
	@$(XSLT) --stringparam option-tree-xml $(INSTANCE_DIR)/$(DOC_OPTION_TREE_XML) -o $(DOC_OUTPUT_DIR)/agents.temp.xml $(DOC_AGENTS_XSL) agents.xinclude-processed.xml 
	@$(XSLT) -o $(DOC_OUTPUT_DIR)/agents.html $(DOTML_DIR)/embed-svg-graphics.xsl $(DOC_OUTPUT_DIR)/agents.temp.xml
	@DOTML_DIR=$(DOTML_DIR) DOT=$(DOT) DOTML_XSLT="$(XSLT) (XSL) (INPUT)" $(DOTML_DIR)/generate-svg-graphics.bash $(DOC_OUTPUT_DIR)/agents.temp.xml $(DOC_OUTPUT_DIR)
	@rm $(DOC_OUTPUT_DIR)/agents.temp.xml 

# Generates the basic behavior index page
$(DOC_BASIC_BEHAVIOR_INDEX_HTML): $(DOC_BASIC_BEHAVIOR_INDEX_XSL) $(DOC_MENU_XSL) $(BASIC_BEHAVIOR_FILES) 
	@make -s agents.xinclude-processed.xml
	@echo $(DOC_BASIC_BEHAVIOR_INDEX_HTML) 
	@$(XSLT) -o $(DOC_BASIC_BEHAVIOR_INDEX_HTML) $(DOC_BASIC_BEHAVIOR_INDEX_XSL) agents.xinclude-processed.xml

# Generates the symbols index page
$(DOC_SYMBOLS_INDEX_HTML): $(DOC_SYMBOLS_INDEX_XSL) $(DOC_MENU_XSL) $(SYMBOL_FILES)  
	@make -s agents.xinclude-processed.xml
	@echo $(DOC_SYMBOLS_INDEX_HTML) 
	@$(XSLT) -o $(DOC_SYMBOLS_INDEX_HTML) $(DOC_SYMBOLS_INDEX_XSL) agents.xinclude-processed.xml

# Generates a intermediate xml file for the option trees of all options
$(DOC_OPTION_TREE_XML): $(OPTION_FILES) $(DOC_OPTION_TREE_XSL)
	@make -s agents.xinclude-processed.xml
	@echo $(DOC_OPTION_TREE_XML)
	@$(XSLT) -o $(DOC_OPTION_TREE_XML) $(DOC_OPTION_TREE_XSL) agents.xinclude-processed.xml

# Validatates agents.xml and included files
$(VALIDATION): $(SCHEMA_FILES) \
               agents.xml options.xml $(SYMBOL_FILES) $(BASIC_BEHAVIOR_FILES) $(OPTION_FILES) 
	@make -s agents.xinclude-processed.xml
	@if test -d $(VALIDATION); then rm $(VALIDATION); fi
	@echo validating `pwd`/agents.xinclude-processed.xml 
	@cat agents.xinclude-processed.xml | $(SCHEMA_VALIDATOR)
	@echo "This file indicates that agents.xml was valid at last call of make. You can remove it if you want." > $(VALIDATION) 

# Generates the debug symbols
$(DEBUG_SYMBOLS): $(DEBUG_SYMBOLS_XSL) agents.xml options.xml $(SYMBOL_FILES) $(BASIC_BEHAVIOR_FILES) $(OPTION_FILES)  
	@make -s $(XABSL_OUTPUT_DIR) 
	@make -s agents.xinclude-processed.xml
	@echo $(DEBUG_SYMBOLS) 
	@$(XSLT) -o $(DEBUG_SYMBOLS) $(DEBUG_SYMBOLS_XSL) agents.xinclude-processed.xml
		
# Generates the intermediate code
$(INTERMEDIATE_CODE): $(INTERMEDIATE_CODE_XSL) agents.xml options.xml $(SYMBOL_FILES) $(BASIC_BEHAVIOR_FILES) $(OPTION_FILES)  
	@make -s $(XABSL_OUTPUT_DIR) 
	@make -s agents.xinclude-processed.xml
	@echo $(INTERMEDIATE_CODE) 
	@$(XSLT) -o $(INTERMEDIATE_CODE) $(INTERMEDIATE_CODE_XSL) agents.xinclude-processed.xml
		
# Generates the output directory for intermediate code and debug symbols
$(XABSL_OUTPUT_DIR):
	@if !(test -d $(XABSL_OUTPUT_DIR)); then echo $(XABSL_OUTPUT_DIR); mkdir $(XABSL_OUTPUT_DIR); fi

# Generates the output directory for documentation
$(DOC_OUTPUT_DIR): 
	@echo $(DOC_OUTPUT_DIR)
	@mkdir $(DOC_OUTPUT_DIR)
	
# Copies documentation.css to the documentation output directory
$(DOC_STYLES_CSS): $(XABSL_DIR)/documentation.css 
	@echo $(DOC_STYLES_CSS) 
	@cp $(XABSL_DIR)/documentation.css $(DOC_OUTPUT_DIR)/styles.css 

# The path where the SVG graphics generated for the documentation of options and agents are stored.
$(DOC_OUTPUT_DIR)/svg: 
	@echo $(DOC_OUTPUT_DIR)/svg
	@mkdir -p $(DOC_OUTPUT_DIR)/svg

rebuild: clean
	@make all

# Removes the documentation output directory, the debug symbols and the intermediate code.	
clean:
	@rm -rf $(DOC_OUTPUT_DIR)
	@rm -f $(DEBUG_SYMBOLS)
	@rm -f $(INTERMEDIATE_CODE)
	@rm -f valid
	
