# -*- mode: Makefile -*-
# XDOC Documentation System for ACL2
# Copyright (C) 2009 Centaur Technology
#
# Contact:
#   Centaur Technology Formal Verification Group
#   7600-C N. Capital of Texas Highway, Suite 300, Austin, TX 78731, USA.
#   http://www.centtech.com/
#
# This program is free software; you can redistribute it and/or modify it under
# the terms of the GNU General Public License as published by the Free Software
# Foundation; either version 2 of the License, or (at your option) any later
# version.  This program is distributed in the hope that it will be useful but
# WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
# FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
# more details.  You should have received a copy of the GNU General Public
# License along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
#
# Original author: Jared Davis <jared@centtech.com>


# XDOC CONVERSION MAKEFILE
#
# This Makefile can convert generate HTML and TEXT documentation from XDOC
# documentation in XML format.
#
# Steps to using it:
#
#   (1) Generate your xdoc documentation using xdoc::save.  This should result
#       in a directory, say "my-doc-dir", with a subdirectory named "xml" that
#       contains a lot of XML files, and a copy of this Makefile as its Makefile.
#
#   (2) In my-doc-dir, just run "make" to do the conversion.  By default all
#       supported formats will be generated.  If you only want a particular
#       format, you can do, e.g., "make html", or "make text", etc.  If you 
#       encounter an error, consider using GNU Make's "-k" flag.



# --- Sanity check that Xalan is installed -----------------------------------

XALAN ?= $(shell which Xalan 2>/dev/null || echo "")
XALAN_UBUNTU ?= $(shell which xalan 2>/dev/null || echo "")

ifeq "$(XALAN)" ""
ifeq "$(XALAN_UBUNTU)" ""

$(info )
$(info Error: Xalan-C++ not found.)
$(info )
$(info Xalan-C++ is available from http://xml.apache.org/xalan-c/.)
$(info )
$(info Depending on your operating system, it may be possible to easily install it)
$(info using tools like apt-get, port, or similar.)
$(info )
$(info Once you have Xalan installed, you should be able to invoke "Xalan" with no)
$(info arguments and see a message like:)
$(info )
$(info     Xalan version 1.10.0.)
$(info     Xerces version 2.7.0.)
$(info     Usage: Xalan [options] source stylesheet)
$(info     ...)
$(info )
$(info Note: if you just want to view the documentation and do not care about)
$(info turning into HTML, try opening preview.html in your web browser.)
$(info )
$(error Xalan-C++ not found)

endif
endif


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

# Define a function named xslt-translate that take three arguments:
# (1) the input xml file, (2) the xsl file that indicates how the xml
# should be rendered, and (3) the output file.
ifneq "$(XALAN)" ""
$(info Using $(XALAN) with -o [output] [input] [stylesheet])
xslt-translate = $(XALAN) -o $(3) $(1) $(2)
else
$(info Using $(XALAN_UBUNTU) with -in [input] -xsl [stylesheet] -out [output])
xslt-translate = $(XALAN_UBUNTU) -in $(1) -xsl $(2) -out $(3)
endif


XML_TOPICS := $(wildcard xml/*.xml)
XDOC_LINKS := $(wildcard xml/*.xdoc-link)

HTML_TOPICS := $(patsubst xml/%.xml, html/%.html, $(XML_TOPICS))
TEXT_TOPICS := $(patsubst xml/%.xml, text/%.text, $(XML_TOPICS))

HTML_XDOC_LINKS := $(patsubst xml/%.xdoc-link, html/%.xdoc-link, $(XDOC_LINKS))


FORMATS := text html

.PHONY: all
all: $(FORMATS)


.PHONY: text
text: $(TEXT_TOPICS)

.PHONY: text-dir
text-dir:
	@echo "Preparing text/ directory"
	@mkdir -p text

text/%.text: xml/%.xml xml/text-topic.xsl text-dir
	@echo $@
	@$(call xslt-translate,xml/$*.xml,xml/text-topic.xsl,text/$*.text)

.PHONY: html
html: $(HTML_TOPICS) \
      $(HTML_XDOC_LINKS) \
      html/full-index.html \
      html/brief-index.html \
      html/topic-index.html

.PHONY: html-dir
html-dir:
	@echo "Preparing html/ directory"
	@mkdir -p html
	@cp xml/xdoc.css xml/xdoc.js html
	@cp xml/*.png xml/*.gif html

# Goofy to copy them all one by one, but prevents "too many arguments"
# warnings during the copy command in some extreme cases
html/%.xdoc-link: xml/%.xdoc-link
	@echo $@
	@cp xml/$*.xdoc-link html/$*.xdoc-link

html/%.html: xml/%.xml xml/html-topic.xsl html-dir
	@echo $@
	@$(call xslt-translate,xml/$*.xml,xml/html-topic.xsl,html/$*.html)

html/full-index.html: xml/index.xml xml/html-full-index.xsl html-dir
	@echo $@
	@$(call xslt-translate,xml/index.xml,xml/html-full-index.xsl,$@)

html/brief-index.html: xml/index.xml xml/html-brief-index.xsl html-dir
	@echo $@
	@$(call xslt-translate,xml/index.xml,xml/html-brief-index.xsl,$@)

html/topic-index.html: xml/topics.xml xml/html-topic-index.xsl html-dir
	@echo $@
	@$(call xslt-translate,xml/topics.xml,xml/html-topic-index.xsl,$@)
