#
# Targets:
#   all
#     Builds all the packages necessary for executing OpenXML. Uses a number
#     of variables to specify the packages and which compiler to use.
#     The 'javac' and 'jikes' targets use specific compilers.
#   jar
#     Builds the JAR distribution (binary only). Uses a number of variables
#     to specify packages and additional files for inclusion.
#   doc
#     Builds the API and example documentation sets into the doc/api and
#     doc/examples directories. These sets are intended for use by OpenXML
#     developers and are shipped as part of the distribution.
#   doc-full
#     Builds the full documentation set into the doc/full directory. It is
#     intended for use by OpenXML contributors.
#   clean
#     Deletes all the files generated by the 'release' target.
#   changelog
#     Processes the changelog XML into two HTML files. 'dist.changelog.html' is
#     intended for use in the Web site, while 'changelog.html' is intended for
#     inclusion in the source distribution.
#   release
#     Prepares a release distribution, cleaning all the files, compiling and
#     building the JAR distribution, and preparing the changelog. It then
#     updates a target 'release' file.
#


JAVAC = javac -O
JIKES = jikes -O
ERR_LOG = err.log


#
# CORE_DIRS is a list of the core OpenXML .java/.class directories that are
# necessary for building the JAR distribution. EXMP_DIRS includes only the
# example directories. ALL_DIRS and JAR_DIRS are based on them.
#
CORE_PKGS = org.w3c.dom org.w3c.dom.html org.w3c.dom.fi \
	org.xml.sax org.xml.sax.helpers \
	org.openxml org.openxml.beans org.openxml.dom org.openxml.dom.html \
	org.openxml.dom.ext org.openxml.dom.iterator org.openxml.io \
	org.openxml.parser org.openxml.source org.openxml.source.holders \
	org.openxml.util org.openxml.x3p org.openxml.x3p.publishers \
	org.openxml.x3p.processors org.openxml.x3p.helpers
EXMP_PKGS = examples examples.changelog

ALL_PKGS = ${CORE_PKGS} ${EXMP_PKGS}


#
# JAR_FILES lists additional files and directories that must be included in
# the JAR distribution, in addition to the packages listed in JAR_PKGS.
# JAR_NAME specifies the name of the JAR distribution file.
#
JAR_FILES = xsa.xml LICENSE.txt openxmlpl.txt \
	org/openxml/*.properties org/openxml/source/dtd-catalog/*.???* \
	org/openxml/util/resources/*.properties \
	org/openxml/x3p/publishers/*.res \
	org/w3c/w3c-copyright-software.html org/xml/sax/sax-copying.html

JAR_PKGS = ${CORE_PKGS}

JAR_NAME = openxml.jar


#
# DOC_API, DOC_FULL and DOC_EXMP list the packages to be used in the API,
# full set and examples documentation, respectively.
#
DOC_API = org.openxml org.openxml.beans org.openxml.parser \
	org.openxml.source org.openxml.x3p org.w3c.dom org.w3c.dom.html \
        org.w3c.dom.fi org.xml.sax org.xml.sax.helpers

DOC_FULL = org.openxml.dom org.openxml.dom.html org.openxml.dom.iterator \
	org.openxml.dom.ext org.openxml.io org.openxml.util \
	org.openxml.source.holders org.openxml.x3p.processors \
	org.openxml.x3p.publishers

DOC_EXMP = examples examples.changelog


#
# DOC_GROUP specifies grouping of documented packages. DOC_OPTIONS specifies
# other options for use with javadoc, and DOC_TARGET names the parent doc
# directory, underneath api, full and examples will be created.
#
DOC_GROUP = -group \"OpenXML\" \"org.openxml*\" -group \"OpenXML Examples\" \
	\"examples*\" -group \"W3C DOM API\" \"org.w3c.dom*\" \
	-group \"SAX API\" \"org.xml.sax*\"
DOC_OPTIONS = -noindex -notree -version -author -windowtitle OpenXML \
	 -doctitle OpenXML ${DOC_GROUP}
DOC_TARGET = doc


#
# The target 'all' uses ALL_PKGS to compose a list of all the packages that
# must be compiled. The package names are translated into directory paths
# ending with '*.java' and are quoted, so as not to expand to a long argument
# list that will crash the shell.
#
ifndef ${COMPILER}
COMPILER := ${JAVAC}
endif

.PHONY : all jikes javac
all :
	@ \
	CLASSES=`echo ${ALL_PKGS} | tr '.' '/' | tr ' ' '\012' | sed -e 's/^/"/' -e 's/$$/\/*.java"/' | tr '\012' ' '`; \
	echo "Compiling OpenXML using '${COMPILER}'"; \
	echo $$CLASSES | xargs ${COMPILER} | tee ${ERR_LOG};

jikes :
	make all "COMPILER=${JIKES}"

javac : 
	make all "COMPILER=${JAVAC}"


#
# The target 'jar' uses JAR_PKGS to compose a list of all the packages that
# must be packaged. The package names are translated into directory paths
# ending with '*.class' and are quoted, so as not to expand to a too long
# argument list that will crash the shell. Additional files specifies in
# JAR_FILES are also included in the JAR and MANIFEST.MF is used as the
# manifest file.
#
.PHONY : jar
jar : ${JAR_NAME}
${JAR_NAME} : all
	@ \
	CLASSES=`echo "${JAR_PKGS}" | tr '.' '/' | tr ' ' '\012' | sed -e 's/^/"/' -e 's/$$/\/*.class"/' | tr '\012' ' '`; \
	FILES=`echo "${JAR_FILES}" | tr ' ' '\012' | sed -e 's/^/"/' -e 's/$$/"/' | tr '\012' ' '`; \
	echo 'Building JAR distribution ${JAR_NAME}'; \
	echo $$CLASSES $$FILES | xargs jar cfm ${JAR_NAME} MANIFEST.MF;


#
# The 'doc' target builds the API and example documentation sets into
# the doc/api and doc/examples directories. These sets are intended for
# use by OpenXML developers and are shipped as part of the distribution.
#
.PHONY : doc
doc :
	@ \
	rm -rf ${DOC_TARGET}/api ${DOC_TARGET}/full ${DOC_TARGET}/examples; \
	mkdir ${DOC_TARGET}/api ${DOC_TARGET}/full ${DOC_TARGET}/examples; \
	echo "Building API documentation set"; \
	javadoc -d ${DOC_TARGET}/api -protected ${DOC_OPTIONS} ${DOC_API} 2> ${ERR_LOG}; \
	echo "Building examples documentation set"; \
	javadoc -d ${DOC_TARGET}/examples -protected ${DOC_OPTIONS} ${DOC_EXMP} 2>> ${ERR_LOG};


#
# The 'doc-full' target builds the full documentation set into the doc/full
# directory. The full documentation set includes all OpenXML packages,
# private classes and their private members. It is intended for use by
# OpenXML contributors.
#
.PHONY : doc-full
doc-full : doc
	@ \
	 echo 'Building full documentation set (all classes, private members)'; \
	javadoc -d ${DOC_TARGET}/full -private ${DOC_OPTIONS} ${DOC_API} ${DOC_FULL} 2>> ${ERR_LOG};


#
# The 'clean' target deletes all the files generated by the 'release' target.
#
.PHONY : clean
clean :
	@ \
	echo 'Removing .class files in all subdirectories...'; \
	clean() { \
		rm -f *.class *~; \
		for PACKAGE in *; do \
			if [ -d "$$PACKAGE" -a \! -h "$$PACKAGE" ]; then \
				cd $$PACKAGE; \
				clean $$PACKAGE; \
				cd ..; \
			fi; \
		done; \
	}; \
	clean org; clean examples; \
	rm -f ${ERR_LOG} ${JAR_NAME} release;


#
# The 'changelog' target processes the changelog XML into two HTML files.
# 'dist.changelog.html' is intended for use in the Web site, while
# 'changelog.html' is intended for inclusion in the source distribution.
#
.PHONY : changelog
changelog : changelog.html
changelog.html :
	@ \
	java examples.changelog.ChangeLogProcessor res:/changelog.xml res:/examples/changelog/template.html changelog.html; \
	mv changelog.html dist.changelog.html; \
	sed -e "s/\/images\//images\//" dist.changelog.html > changelog.html;


#
# The target 'release' prepares a release distribution, cleaning all the files,
# and compiling and building the JAR distribution. It then updates a
# target 'release' file.
#
release : jar
	@ \
	touch release;


