######
# Documentation

DOCSDIR = $(SRCDIR)docs

D14 := $(shell which -a install/bin/d14 obj/gen/bin/dev/d14.sh | head -n 1)

DOCSOBJDIR = $(GENDIR)/docs
DOCSGUIDEDIR = $(DOCSOBJDIR)/guide
DOCS_GUIDE_SOURCES := $(shell find "$(DOCSDIR)" -name '*.rst')

DOCSREFDIR = $(DOCSOBJDIR)/ref
DOCS_REF_SOURCES = $(TOOL_SOURCES) $(LIBS_SOURCES) $(CONF_SOURCES)
DOCS_REF_OUTPUTS = $(addprefix $(DOCSREFDIR)/,$(DOCS_REF_SOURCES))
DOCS_REF_OUTPUTS_BASE = $(patsubst %.in,%,$(DOCS_REF_OUTPUTS))

DOCS_ARCHIVE = $(DISTDIR)/$(PKG)-docs-$(VERSION)
DOCS_ARCHIVES = $(addprefix $(DOCS_ARCHIVE),$(ARCHIVE_SUFFIXES))

######
# Extension Rules

docs: apps
	$(TIME) $(MAKE) docs-build

docs-build: docs-deps docs-rst docs-html

docs-clean: docs-deps-clean docs-rst-clean docs-html-clean
	$(RM_R) -v $(DOCSOBJDIR)

docs-check: $(UNIMPLEMENTED)

docs-install: $(UNIMPLEMENTED)
docs-uninstall: $(UNIMPLEMENTED)

docs-dist: $(DOCS_ARCHIVES)
docs-dist-clean: docs-clean
	$(MAKE) $@-unimplemented

######
# Documentation ChangeLogs

DOCS_CHANGELOGS = $(DOCSOBJDIR)/ChangeLog $(DOCSOBJDIR)/ChangeLog-$(VERSION)

docs-changelog: $(DOCS_CHANGELOGS)
docs-changelog-clean:
	rm -f $(DOCS_CHANGELOGS)

######
# Dependency Graphs

DOCS_DEP_SCRIPTS = $(patsubst %.in,%.deps.dot,$(TOOL_SOURCES) $(LIBS_SOURCES))
DOCS_DEP_SOURCES = $(addprefix $(DOCSREFDIR)/, $(DOCS_DEP_SCRIPTS))
DOCS_DEP_GRAPHS_DOT = $(DOCS_DEP_SOURCES) \
		  $(DOCSREFDIR)/libs/$(RUNTIME).deps.dot \
		  $(DOCSREFDIR)/runtime.deps.dot \
		  $(DOCSREFDIR)/libraries.deps.dot \
		  $(DOCSREFDIR)/tools.deps.dot \
		  $(DOCSREFDIR)/all.deps.dot \

DOCS_DEP_GRAPHS_SVG = $(patsubst %.dot,%.svg,$(DOCS_DEP_GRAPHS_DOT))
DOCS_DEP_GRAPHS_PDF = $(patsubst %.dot,%.pdf,$(DOCS_DEP_GRAPHS_DOT))
DOCS_DEP_GRAPHS_ALL = \
		$(DOCS_DEP_GRAPHS_DOT) \
		$(DOCS_DEP_GRAPHS_SVG) \
		$(DOCS_DEP_GRAPHS_PDF)

docs-deps: $(DOCS_DEP_GRAPHS_DOT)
docs-deps-clean:
	rm -f $(DOCS_DEP_GRAPHS_ALL)

$(DOCSREFDIR)/runtime.deps.dot: $(LIBS_SOURCES)
	$(D14) deps runtime

$(DOCSREFDIR)/libraries.deps.dot: $(LIBS_SOURCES)
	$(D14) deps libs

$(DOCSREFDIR)/tools.deps.dot: $(TOOL_SOURCES) $(LIBS_SOURCES)
	$(D14) deps tools

$(DOCSREFDIR)/all.deps.dot: $(TOOL_SOURCES) $(LIBS_SOURCES)
	$(D14) deps global

$(DOCSREFDIR)/src/%.deps.dot: src/%.in
	$(D14) deps files src/$*

$(DOCSREFDIR)/%.deps.dot: %.in
	$(D14) deps files $*

######
# reStructuredText Documentation

DOCS_GUIDE_RST := $(subst $(DOCSDIR),$(DOCSGUIDEDIR),$(DOCS_GUIDE_SOURCES))

DOCS_REF_RST := $(addsuffix .rst,$(DOCS_REF_OUTPUTS_BASE))

docs-rst: docs-rst-guide docs-rst-ref
docs-rst-clean: docs-rst-guide-clean docs-rst-ref-clean

docs-rst-guide: $(DOCS_GUIDE_RST)
docs-rst-guide-clean:
	$(RM) $(DOCS_GUIDE_RST)

docs-rst-ref: $(DOCS_REF_RST)
docs-rst-ref-clean:
	$(RM) $(DOCS_REF_RST)


######
# HTML Documentation

DOCS_CSS = $(DOCSOBJDIR)/docs.css

DOCS_GUIDE_HTML := $(patsubst %.rst,%.html,$(DOCS_GUIDE_RST))
DOCS_GUIDE_INDEX_HTML = $(DOCSGUIDEDIR)/contents.html

DOCS_REF_HTML := $(patsubst %.rst,%.html,$(DOCS_REF_RST))
DOCS_REF_INDEX_HTML = $(DOCSREFDIR)/index.html
DOCS_REF_SYMBOLS_HTML = $(DOCSREFDIR)/symbols.html

DOCS_RAW_SOURCES = $(DOCS_REF_SOURCES)
DOCS_RAW_HTML := $(addsuffix .raw.html,$(DOCS_REF_OUTPUTS_BASE))

DOCS_HTML_INDEXES = $(DOCS_GUIDE_INDEX_HTML) $(DOCS_REF_INDEX_HTML)

docs-html-view: docs-html
	$(WEBVIEWER) $(DOCS_HTML_INDEXES)

DOCS_HTML_TARGETS = $(addprefix docs-html-,indexes sources raw css)

docs-html: $(DOCS_HTML_TARGETS)
docs-html-clean: $(addsuffix -clean,$(DOCS_HTML_TARGETS))

docs-html-indexes: docs-html-guide-index docs-html-ref-index
docs-html-indexes-clean: docs-html-guide-index-clean docs-html-ref-index-clean

docs-html-sources: docs-html-guide docs-html-ref
docs-html-sources-clean: docs-html-guide-clean docs-html-ref-clean

docs-html-guide: $(DOCS_GUIDE_HTML)
docs-html-guide-clean:
	rm -f $(DOCS_GUIDE_HTML)

docs-html-guide-index: $(DOCS_GUIDE_INDEX_HTML)
docs-html-guide-index-clean:
	rm -f $(DOCS_GUIDE_INDEX_HTML)

docs-html-ref: $(DOCS_REF_HTML)
docs-html-ref-clean:
	rm -f $(DOCS_REF_HTML)

docs-html-ref-index: $(DOCS_REF_INDEX_HTML)
docs-html-ref-index-clean:
	rm -f $(DOCS_REF_INDEX_HTML)

docs-html-raw: $(DOCS_RAW_HTML)
docs-html-raw-clean:
	rm -f $(DOCS_RAW_HTML)

docs-html-css: $(DOCS_CSS)
docs-html-css-clean:
	rm -f $(DOCS_CSS)


######
# HTML CSS Files

$(DOCS_CSS):
	$(D14) html css


######
# User Guide

DOCS_GUIDE_TARGETS = docs-rst-guide docs-html-guide docs-html-guide-index

docs-guide: $(DOCS_GUIDE_TARGETS)
docs-guide-clean: $(addsuffix -clean,$(DOCS_GUIDE_TARGETS))

$(DOCSGUIDEDIR)/%.rst: $(DOCSDIR)/%.rst
	$(D14) rst guide $*

$(DOCSGUIDEDIR)/%.html: $(DOCSGUIDEDIR)/%.rst
	$(D14) html guide $*

$(DOCS_GUIDE_INDEX_HTML): $(DOCS_GUIDE_HTML) $(DOCS_CSS)
	$(D14) html index guide

######
# Reference Manual

DOCS_REF_TARGETS = docs-rst-ref docs-html-ref docs-html-ref-index docs-html-raw

docs-ref: $(DOCS_REF_TARGETS)
docs-ref-clean: $(addsuffix -clean,$(DOCS_REF_TARGETS))

$(DOCSREFDIR)/src/%.rst: src/%.in $(DOCSREFDIR)/src/%.deps.dot
	$(D14) rst ref src/$*

$(DOCSREFDIR)/libs/%.rst: libs/%.in $(DOCSREFDIR)/libs/%.deps.dot
	$(D14) rst ref libs/$*

$(DOCSREFDIR)/%.rst: %.in
	$(D14) rst ref $*

$(DOCSREFDIR)/%.html: $(DOCSREFDIR)/%.rst $(DOCSREFDIR)/%.raw.html
	$(D14) html ref $*

$(DOCS_REF_INDEX_HTML): $(DOCS_REF_HTML)
	$(D14) html index script

$(DOCS_REF_SYMBOLS_HTML): $(DOCS_REF_HTML)
	$(D14) html index symbol

######
# Raw Source Code

$(DOCSREFDIR)/%.raw.html: %.in
	$(D14) html raw $*
