.PHONY: docs docsclean checkref checkref_en checkref_es checkref_fr checkref_nb checkref_vi checkref_zh_CN
.PHONY: pdfdocs htmldocs install-doc install-doc-pdf install-doc-html

# To make linuxcnc-checklink widely available
export PATH:=$(BASEPWD)/../scripts:$(PATH)

DOC_DIR=../docs
DOC_SRCDIR=../docs/src

LOC_HL_DIR=../docs/src/source-highlight
LOC_LANG_MAP=$(LOC_HL_DIR)/local/lang.map

GENERATED_MANPAGES += ../docs/man/man1/linuxcnc.1
GENERATED_MANPAGES += $(patsubst ../docs/src/man/%.adoc, ../docs/man/%, $(wildcard ../docs/src/man/*/*.adoc))

MAN_SRCS = $(sort \
	$(wildcard $(DOC_DIR)/man/man1/*.1) \
	$(wildcard $(DOC_DIR)/man/man3/*.3*) \
	$(wildcard $(DOC_DIR)/man/man9/*.9) \
	$(wildcard $(DOC_DIR)/man/es/man1/*.1) \
	$(wildcard $(DOC_DIR)/man/es/man3/*.3*) \
	$(wildcard $(DOC_DIR)/man/es/man9/*.9) \
	$(GENERATED_MANPAGES))

../docs/man/man1/linuxcnc.1: $(DOC_SRCDIR)/man/man1/linuxcnc.1.in config.status
	./config.status --file=$@:$<

ifneq ($(MANDB),)
docs: $(DOC_DIR)/man/index.db
$(DOC_DIR)/man/index.db: $(MAN_SRCS)
	@echo "Updating 'whatis' database"
	$(Q)$(MANDB) $(DOC_DIR)/man
endif

ifeq ($(BUILD_DOCS),yes)
DOC_SRCS_EN := \
	code/code-notes.adoc \
	code/style-guide.adoc \
	code/nml-messages.adoc \
	code/rs274.adoc \
	code/adding-configs.adoc \
	code/contributing-to-linuxcnc.adoc \
	code/building-linuxcnc.adoc \
	common/emc-history.adoc \
	common/glossary.adoc \
	common/gpld-copyright.adoc \
	common/overleaf.adoc \
	config/core-components.adoc \
	config/ini-config.adoc \
	config/ini-homing.adoc \
	config/iov2.adoc \
	config/integrator-concepts.adoc \
	config/lathe-config.adoc \
	config/moveoff.adoc \
	config/pncconf.adoc \
	config/python-interface.adoc \
	config/stepconf.adoc \
	config/stepper-diagnostics.adoc \
	config/stepper.adoc \
	config/stepper-quickstart.adoc \
	drivers/ax5214h.adoc \
	drivers/vfs11.adoc \
	drivers/mitsub-vfd.adoc \
	drivers/gm.adoc \
	drivers/gs2.adoc \
	drivers/hostmot2.adoc \
	drivers/mb2hal.adoc \
	drivers/motenc.adoc \
	drivers/opto22.adoc \
	drivers/pico-ppmc.adoc \
	drivers/pluto-p.adoc \
	drivers/pmx485.adoc \
	drivers/servo-to-go.adoc \
	drivers/shuttle.adoc \
	examples/gcode.adoc \
	examples/gs2-example.adoc \
	examples/mpg.adoc \
	examples/pci-parallel-port.adoc \
	examples/spindle.adoc \
	gcode/coordinates.adoc \
	gcode/g-code.adoc \
	gcode/machining-center.adoc \
	gcode/m-code.adoc \
	gcode/o-code.adoc \
	gcode/other-code.adoc \
	gcode/overview.adoc \
	gcode/rs274ngc.adoc \
	gcode/tool-compensation.adoc \
	getting-started/about-linuxcnc.adoc \
	getting-started/getting-linuxcnc.adoc \
	common/linux-faq.adoc \
	getting-started/running-linuxcnc.adoc \
	getting-started/system-requirements.adoc \
	getting-started/updating-linuxcnc.adoc \
	gui/axis.adoc \
	gui/filter-programs.adoc \
	gui/gladevcp.adoc \
	gui/gladevcp-libraries.adoc \
	gui/gmoccapy.adoc \
	gui/gscreen.adoc \
	gui/qtdragon.adoc \
	gui/qtvcp.adoc \
	gui/qtvcp-vcp-panels.adoc \
	gui/qtvcp-widgets.adoc \
	gui/qtvcp-libraries.adoc \
	gui/qtvcp-vismach.adoc \
	gui/qtvcp-custom-widgets.adoc \
	gui/qtvcp-code-snippets.adoc \
	gui/qtvcp-development.adoc \
	gui/panelui.adoc \
	gui/halui.adoc \
	gui/image-to-gcode.adoc \
	gui/mdro.adoc \
	gui/ngcgui.adoc \
	gui/tklinuxcnc.adoc \
	gui/tooledit.adoc \
	gui/touchy.adoc \
	gui/gstat.adoc \
	gui/vismach.adoc \
	hal/basic-hal.adoc \
	hal/canonical-devices.adoc \
	hal/comp.adoc \
	hal/components.adoc \
	hal/general-ref.adoc \
	hal/hal-examples.adoc \
	hal/halmodule.adoc \
	hal/halshow.adoc \
	hal/haltcl.adoc \
	hal/halui-examples.adoc \
	hal/intro.adoc \
	hal/parallel-port.adoc \
	gui/pyvcp.adoc \
	gui/pyvcp-examples.adoc \
	hal/rtcomps.adoc \
	hal/tools.adoc \
	hal/tutorial.adoc \
	hal/twopass.adoc \
	install/latency-test.adoc \
	integrator/steppers.adoc \
	integrator/stepper-timing.adoc \
	integrator/wiring.adoc \
	ladder/classic-ladder.adoc \
	ladder/ladder-examples.adoc \
	ladder/ladder-intro.adoc \
	lathe/lathe-user.adoc \
	motion/kinematics.adoc \
	motion/dh-parameters.adoc \
	motion/pid-theory.adoc \
	motion/tweaking-steppers.adoc \
	motion/5-axis-kinematics.adoc \
	motion/external-offsets.adoc \
	motion/switchkins.adoc \
	tooldatabase/tooldatabase.adoc \
	plasma/qtplasmac.adoc \
	plasma/plasma-cnc-primer.adoc \
	remap/remap.adoc \
	user/starting-linuxcnc.adoc \
	user/user-concepts.adoc \
	user/user-foreword.adoc \
	user/user-intro.adoc \
	Master_Getting_Started.adoc \
	Master_Documentation.adoc \
	Master_Integrator.adoc \
	Master_Developer.adoc

LANGUAGES := $(shell grep '\[po4a_langs\]' $(DOC_DIR)/po4a.cfg| cut -d" " -f2-)
LANGUAGES_MATCH := $(shell echo $(LANGUAGES) | tr " " "|")


GENERATED_TRANSLATED = $(foreach l, $(LANGUAGES), \
	$(DOC_SRCDIR)/$(l) \
	$(DOC_DIR)/help/$(l) \
	$(DOC_DIR)/html/$(l) \
	$(DOC_DIR)/man/$(l) \
)

# Time how long po4a takes to run if the system has the `time` command
# installed, but just skip timing and don't fail the build if it's
# not available.
TIME_CMD := $(shell which time || true)
ifneq (${TIME_CMD},)
    TIME_CMD := ${TIME_CMD} -v
endif

$(DOC_DIR)/po/documentation.pot: $(addprefix $(DOC_SRCDIR)/, $(DOC_SRCS_EN))
	cd $(DOC_DIR); ${TIME_CMD} po4a -v --msgmerge-opt='-v' --no-translations po4a.cfg
pofiles: $(DOC_DIR)/po/documentation.pot

ifeq ($(BUILD_DOCS_TRANSLATED),yes)
translateddocs: manpages $(DOC_DIR)/po/documentation.pot
	cd $(DOC_DIR); ${TIME_CMD} po4a -v --msgmerge-opt='-v' --no-update po4a.cfg
else
translateddocs:
endif
docs: translateddocs

postatus::
	@echo "info: Documentation POT and PO file status"
	@cd $(DOC_DIR)/..; for p in docs/po/*.pot docs/po/*.po; do \
	    echo -n "$$p ";  msgfmt --statistics -o /dev/null $$p; \
	done

DOC_SRCS_DE = $(subst $(DOC_SRCDIR)/,, \
	$(wildcard $(DOC_SRCDIR)/de/*.adoc) \
	$(wildcard $(DOC_SRCDIR)/de/*/*.adoc))

DOC_SRCS_ES = $(subst $(DOC_SRCDIR)/,, \
	$(wildcard $(DOC_SRCDIR)/es/*.adoc) \
	$(wildcard $(DOC_SRCDIR)/es/*/*.adoc))

DOC_SRCS_FR = $(subst $(DOC_SRCDIR)/,, \
	$(wildcard $(DOC_SRCDIR)/fr/*.adoc) \
	$(wildcard $(DOC_SRCDIR)/fr/*/*.adoc))

DOC_SRCS_HU = $(subst $(DOC_SRCDIR)/,, \
	$(wildcard $(DOC_SRCDIR)/hu/*.adoc) \
	$(wildcard $(DOC_SRCDIR)/hu/*/*.adoc))

DOC_SRCS_NB = $(subst $(DOC_SRCDIR)/,, \
	$(wildcard $(DOC_SRCDIR)/nb/*.adoc) \
	$(wildcard $(DOC_SRCDIR)/nb/*/*.adoc))

DOC_SRCS_ZH_CN = $(subst $(DOC_SRCDIR)/,, \
	$(wildcard $(DOC_SRCDIR)/zh_CN/*.adoc) \
	$(wildcard $(DOC_SRCDIR)/zh_CN/*/*.adoc))

DOC_SRCS = $(DOC_SRCS_EN) $(DOC_SRCS_DE) $(DOC_SRCS_ES) $(DOC_SRCS_FR) $(DOC_SRCS_HU) $(DOC_SRCS_NB) $(DOC_SRCS_ZH_CN)

DOC_SRCS_EN_SMALL = $(filter-out Master_%,$(DOC_SRCS_EN))
DOC_SRCS_DE_SMALL = $(filter-out Master_%,$(DOC_SRCS_DE))
DOC_SRCS_ES_SMALL = $(filter-out Master_%,$(DOC_SRCS_ES))
DOC_SRCS_FR_SMALL = $(filter-out Master_%,$(DOC_SRCS_FR))
DOC_SRCS_HU_SMALL = $(filter-out Master_%,$(DOC_SRCS_HU))
DOC_SRCS_NB_SMALL = $(filter-out Master_%,$(DOC_SRCS_NB))
DOC_SRCS_ZH_CN_SMALL = $(filter-out Master_%,$(DOC_SRCS_ZH_CN))

DOC_SRCS_HTML = $(patsubst %.adoc, %.html, $(foreach p, $(DOC_SRCS), $(if $(findstring Master_, $(p)),, $p)))
DOC_TARGETS_HTML = $(addprefix $(DOC_DIR)/html/,$(DOC_SRCS_HTML)) #$(subst /,_,$(DOC_SRCS_HTML)))
DOC_TARGETS_XML = $(patsubst $(DOC_DIR)/html/%.html, objects/%.xml, $(DOC_TARGETS_HTML))

DOC_TARGETS_XML_DE = $(foreach p, $(DOC_TARGETS_XML), $(if $(findstring /de/, $(p)), $p))
DOC_TARGETS_XML_ES = $(foreach p, $(DOC_TARGETS_XML), $(if $(findstring /es/, $(p)), $p))
DOC_TARGETS_XML_FR = $(foreach p, $(DOC_TARGETS_XML), $(if $(findstring /fr/, $(p)), $p))
DOC_TARGETS_XML_HU = $(foreach p, $(DOC_TARGETS_XML), $(if $(findstring /hu/, $(p)), $p))
DOC_TARGETS_XML_NB = $(foreach p, $(DOC_TARGETS_XML), $(if $(findstring /nb/, $(p)), $p))
DOC_TARGETS_XML_VI = $(foreach p, $(DOC_TARGETS_XML), $(if $(findstring /vi/, $(p)), $p))
DOC_TARGETS_XML_ZH_CN = $(foreach p, $(DOC_TARGETS_XML), $(if $(findstring /zh_CN/, $(p)), $p))
DOC_TARGETS_XML_EN = $(filter-out $(DOC_TARGETS_XML_DE), $(filter-out $(DOC_TARGETS_XML_ES), $(filter-out $(DOC_TARGETS_XML_FR), $(filter-out $(DOC_TARGETS_XML_HU), $(filter-out $(DOC_TARGETS_XML_NB), $(filter-out $(DOC_TARGETS_XML_VI), $(filter-out $(DOC_TARGETS_XML_ZH_CN), $(DOC_TARGETS_XML))))))))
DOC_TARGETS_HTML_DE = $(foreach p, $(DOC_TARGETS_HTML), $(if $(findstring /de/, $(p)), $p))
DOC_TARGETS_HTML_ES = $(foreach p, $(DOC_TARGETS_HTML), $(if $(findstring /es/, $(p)), $p))
DOC_TARGETS_HTML_FR = $(foreach p, $(DOC_TARGETS_HTML), $(if $(findstring /fr/, $(p)), $p))
DOC_TARGETS_HTML_HU = $(foreach p, $(DOC_TARGETS_HTML), $(if $(findstring /hu/, $(p)), $p))
DOC_TARGETS_HTML_NB = $(foreach p, $(DOC_TARGETS_HTML), $(if $(findstring /nb/, $(p)), $p))
DOC_TARGETS_HTML_VI = $(foreach p, $(DOC_TARGETS_HTML), $(if $(findstring /vi/, $(p)), $p))
DOC_TARGETS_HTML_ZH_CN = $(foreach p, $(DOC_TARGETS_HTML), $(if $(findstring /zh_CN/, $(p)), $p))
DOC_TARGETS_HTML_EN = $(filter-out $(DOC_TARGETS_HTML_DE), $(filter-out $(DOC_TARGETS_HTML_ES), $(filter-out $(DOC_TARGETS_HTML_FR), $(filter-out $(DOC_TARGETS_HTML_HU), $(filter-out $(DOC_TARGETS_HTML_NB), $(filter-out $(DOC_TARGETS_HTML_VI), $(filter-out $(DOC_TARGETS_HTML_ZH_CN), $(DOC_TARGETS_HTML))))))))

MAN_HTML_TARGETS = $(patsubst $(DOC_DIR)/man/%, $(DOC_DIR)/html/man/%.html, $(MAN_SRCS))

PDF_TARGETS_EN := $(addprefix $(DOC_DIR)/, $(patsubst %.adoc,%.pdf, \
        $(subst Master_,LinuxCNC_, $(filter Master_%,$(DOC_SRCS_EN))))\
        LinuxCNC_Manual_Pages.pdf \
        ) \

PDF_TARGETS_DE = $(addprefix $(DOC_DIR)/, $(subst de/,, \
	$(patsubst %.adoc,%_de.pdf, \
	$(subst Master_,LinuxCNC_, $(filter de/Master_%,$(DOC_SRCS_DE))))))

PDF_TARGETS_ES = $(addprefix $(DOC_DIR)/, $(subst es/,, \
	$(patsubst %.adoc,%_es.pdf, \
	$(subst Master_,LinuxCNC_, $(filter es/Master_%,$(DOC_SRCS_ES))))))

PDF_TARGETS_FR = $(addprefix $(DOC_DIR)/, $(subst fr/,, \
	$(patsubst %.adoc,%_fr.pdf, \
	$(subst Master_,LinuxCNC_, $(filter fr/Master_%,$(DOC_SRCS_FR))))))

PDF_TARGETS_NB = $(addprefix $(DOC_DIR)/, $(subst nb/,, \
	$(patsubst %.adoc,%_nb.pdf, \
	$(subst Master_,LinuxCNC_, $(filter nb/Master_%,$(DOC_SRCS_NB))))))

PDF_TARGETS_ZH_CN = $(addprefix $(DOC_DIR)/, $(subst zh_CN/,, \
	$(patsubst %.adoc,%_zh_CN.pdf, \
	$(subst Master_,LinuxCNC_, $(filter zh_CN/Master_%,$(DOC_SRCS_ZH_CN))))))

PDF_TARGETS = $(PDF_TARGETS_EN) $(PDF_TARGETS_DE) $(PDF_TARGETS_ES) $(PDF_TARGETS_FR) $(PDF_TARGETS_NB)

# Chinese PDFs only build with xetex, which is missing in stretch
SHELL=/bin/bash
ifneq (, $(shell command -v xetex))
USING_XETEX = true
endif
ifdef USING_XETEX
PDF_TARGETS += $(PDF_TARGETS_ZH_CN)
DBLATEX_OPTS=--backend xetex
else
endif

# It's better to keep the above on separate lines for troubleshooting by swapping

HTML_TARGETS = \
	$(DOC_TARGETS_HTML) \
	$(MAN_HTML_TARGETS) \
	$(DOC_DIR)/html/index.html \
	$(sort $(patsubst $(DOC_SRCDIR)/index_%.tmpl, \
		$(DOC_DIR)/html/%/index.html, \
		$(wildcard $(DOC_DIR)/src/index_*.tmpl)))

A2X = a2x --xsltproc-opts "--nonet \
			   --stringparam toc.section.depth 3 \
			   --stringparam toc.max.depth 2 \
			   --stringparam generate.section.toc.level 2 \
			   --stringparam generate.toc 'book toc,title chapter toc'" \
	  -a "scriptdir=$(DOC_SRCDIR)/" \
	  --asciidoc-opts="-f $(DOC_SRCDIR)/docbook.conf" \
	  --asciidoc-opts="-f $(DOC_SRCDIR)/asciidoc-dont-replace-arrows.conf" \
	  --dblatex-opts "-P doc.publisher.show=0 -P latex.output.revhistory=0 -s $(DOC_SRCDIR)/emc2.sty \
			  -P latex.hyperparam=colorlinks,linkcolor=blue,filecolor=blue,urlcolor=blue,citecolor=blue \
                          $(A2X_LATEX_ENCODING) $(DBLATEX_OPTS)"

ifeq ($(TRIVIAL_BUILD),no)
-include $(patsubst %.adoc, depends/%.d, $(DOC_SRCS))
Makefile: $(patsubst %.adoc, depends/%.d, $(DOC_SRCS))
endif

docs: manpages

clean: clean-manpages clean-mandb clean-translated
clean-manpages:
	-rm -f $(GENERATED_MANPAGES)
clean-mandb:
	find $(DOC_SRCDIR) -name index*db -delete
clean-translated:
	-$(RM) -r $(GENERATED_TRANSLATED)

DOTFILES=$(shell find . -name "*.dot") $(shell find ../docs/src/ -name "*.dot")
.PHONY: svgs_made_from_dots
svgs_made_from_dots: $(DOTFILES:.dot=.svg)

ifeq ($(BUILD_DOCS_PDF),yes)
docs: pdfdocs
install-doc: install-doc-pdf
endif
ifeq ($(BUILD_DOCS_HTML),yes)
docs: htmldocs
install-doc: install-doc-html
endif

pdfdocs: svgs_made_from_dots $(PDF_TARGETS)
	../scripts/make-docs-pdf-index

htmldocs: svgs_made_from_dots .htmldoc-stamp checkref_en

.htmldoc-stamp: copy_asciidoc_files gen_complist $(HTML_TARGETS) .images-stamp .include-stamp
	touch $@

copy_asciidoc_files: $(wildcard /etc/asciidoc/stylesheets/*.css) $(wildcard /etc/asciidoc/javascripts/*.js)
	cp -f $^ $(DOC_DIR)/html

gen_complist: $(DOC_SRCDIR)/gen_complist.py $(DOC_SRCDIR)/hal/components.adoc $(MAN_HTML_TARGETS)
	mkdir -p $(DOC_DIR)/html/hal
	python3 $(DOC_SRCDIR)/gen_complist.py $(DOC_SRCDIR)/hal/components.adoc

checkref: checkref_en checkref_de checkref_es checkref_fr checkref_hu checkref_nb checkref_vi checkref_zh_CN

checkref_en: $(DOC_TARGETS_HTML_EN) $(DOC_DIR)/html/index.html $(DOC_DIR)/html/gcode.html .htmldoc-stamp
	@$(DOC_SRCDIR)/checkref English $^

checkref_de: $(DOC_TARGETS_HTML_DE) $(DOC_DIR)/html/de/gcode.html .htmldoc-stamp
	@$(DOC_SRCDIR)/checkref German $^

checkref_es: $(DOC_TARGETS_HTML_ES) $(DOC_DIR)/html/es/gcode.html .htmldoc-stamp
	@$(DOC_SRCDIR)/checkref Spanish $^

checkref_fr: $(DOC_TARGETS_HTML_FR) $(DOC_DIR)/html/fr/gcode.html .htmldoc-stamp
	@$(DOC_SRCDIR)/checkref French $^

checkref_hu: $(DOC_TARGETS_HTML_HU) .htmldoc-stamp
	@$(DOC_SRCDIR)/checkref Hungarian $^

checkref_nb: $(DOC_TARGETS_HTML_NB) .htmldoc-stamp
	@$(DOC_SRCDIR)/checkref Norwegian $^

checkref_zh_CN: $(DOC_TARGETS_HTML_ZH_CN) .htmldoc-stamp
	@$(DOC_SRCDIR)/checkref Chinese $^

checkref_vi: $(DOC_TARGETS_HTML_VI) $(DOC_DIR)/html/vi/gcode.html .htmldoc-stamp
	@$(DOC_SRCDIR)/checkref Vietnamese $^

MAN_SRCS_NOSO = $(patsubst $(DOC_DIR)/man/%,%, \
			$(shell grep -L '^\.so ' $(MAN_SRCS)))

PDF_MAN_ORDER := man1/linuxcnc.1 $(filter-out %/linuxcnc.1, $(filter man1/%, $(MAN_SRCS_NOSO))) \
	man3/intro.3hal $(filter-out %/undocumented.3hal %/intro.3hal, $(filter man3/%.3hal, $(MAN_SRCS_NOSO))) \
	man3/undocumented.3hal man3/intro.3rtapi $(filter-out %/undocumented.3rtapi %/intro.3rtapi, $(filter man3/%.3rtapi, $(MAN_SRCS_NOSO))) \
	man3/undocumented.3rtapi $(filter man9/%, $(MAN_SRCS_NOSO)) 

$(DOC_DIR)/LinuxCNC_Manual_Pages.pdf: $(MAN_SRCS) objects/var-PDF_MAN_ORDER
	@echo Formatting manual pages as PDF
	(cd $(DOC_DIR)/man; groff -t -rC1 -rD1 -Tps -man $(PDF_MAN_ORDER)) \
		| ps2pdf - $@ 

$(DOC_DIR)/LinuxCNC_Getting_Started.pdf: $(DOC_SRCDIR)/Master_Getting_Started.pdf
	@ln -f $< $@
$(DOC_DIR)/LinuxCNC_Getting_Started_de.pdf: $(DOC_SRCDIR)/de/Master_Getting_Started.pdf
	@ln -f $< $@
$(DOC_DIR)/LinuxCNC_Getting_Started_es.pdf: $(DOC_SRCDIR)/es/Master_Getting_Started.pdf
	@ln -f $< $@
$(DOC_DIR)/LinuxCNC_Getting_Started_fr.pdf: $(DOC_SRCDIR)/fr/Master_Getting_Started.pdf
	@ln -f $< $@
$(DOC_DIR)/LinuxCNC_Getting_Started_nb.pdf: $(DOC_SRCDIR)/nb/Master_Getting_Started.pdf
	@ln -f $< $@
$(DOC_DIR)/LinuxCNC_Getting_Started_zh_CN.pdf: $(DOC_SRCDIR)/zh_CN/Master_Getting_Started.pdf
	@ln -f $< $@

$(DOC_DIR)/LinuxCNC_Documentation.pdf: $(DOC_SRCDIR)/Master_Documentation.pdf
	@ln -f $< $@
$(DOC_DIR)/LinuxCNC_Documentation_de.pdf: $(DOC_SRCDIR)/de/Master_Documentation.pdf
	@ln -f $< $@
$(DOC_DIR)/LinuxCNC_Documentation_es.pdf: $(DOC_SRCDIR)/es/Master_Documentation.pdf
	@ln -f $< $@
$(DOC_DIR)/LinuxCNC_Documentation_fr.pdf: $(DOC_SRCDIR)/fr/Master_Documentation.pdf
	@ln -f $< $@
$(DOC_DIR)/LinuxCNC_Documentation_nb.pdf: $(DOC_SRCDIR)/nb/Master_Documentation.pdf
	@ln -f $< $@
$(DOC_DIR)/LinuxCNC_Documentation_zh_CN.pdf: $(DOC_SRCDIR)/zh_CN/Master_Documentation.pdf
	@ln -f $< $@

$(DOC_DIR)/LinuxCNC_Integrator.pdf: $(DOC_SRCDIR)/Master_Integrator.pdf
	@ln -f $< $@
$(DOC_DIR)/LinuxCNC_Integrator_de.pdf: $(DOC_SRCDIR)/de/Master_Integrator.pdf
	@ln -f $< $@
$(DOC_DIR)/LinuxCNC_Integrator_es.pdf: $(DOC_SRCDIR)/es/Master_Integrator.pdf
	@ln -f $< $@
$(DOC_DIR)/LinuxCNC_Integrator_fr.pdf: $(DOC_SRCDIR)/fr/Master_Integrator.pdf
	@ln -f $< $@
$(DOC_DIR)/LinuxCNC_Integrator_nb.pdf: $(DOC_SRCDIR)/nb/Master_Integrator.pdf
	@ln -f $< $@
$(DOC_DIR)/LinuxCNC_Integrator_zh_CN.pdf: $(DOC_SRCDIR)/zh_CN/Master_Integrator.pdf
	@ln -f $< $@

$(DOC_DIR)/LinuxCNC_Developer.pdf: $(DOC_SRCDIR)/Master_Developer.pdf
	@ln -f $< $@
$(DOC_DIR)/LinuxCNC_Developer_de.pdf: $(DOC_SRCDIR)/de/Master_Developer.pdf
	@ln -f $< $@
$(DOC_DIR)/LinuxCNC_Developer_es.pdf: $(DOC_SRCDIR)/es/Master_Developer.pdf
	@ln -f $< $@
$(DOC_DIR)/LinuxCNC_Developer_fr.pdf: $(DOC_SRCDIR)/fr/Master_Developer.pdf
	@ln -f $< $@
$(DOC_DIR)/LinuxCNC_Developer_nb.pdf: $(DOC_SRCDIR)/nb/Master_Developer.pdf
	@ln -f $< $@
$(DOC_DIR)/LinuxCNC_Developer_zh_CN.pdf: $(DOC_SRCDIR)/zh_CN/Master_Developer.pdf
	@ln -f $< $@

$(DOC_DIR)/html/man/%.html: $(DOC_DIR)/man/%
	@echo Formatting $(notdir $<) as HTML
	@mkdir -p $(dir $@)
#   Attention! This is a temporary fix until Groff 1.23 is released. Should be
#   set back to "-m man" when 1.23 is available.
#	This fixes the formatting of subsections and appearance in the toc.
#   It uses the local an-old.tmac instead of the one which is shipped with Groff.
	@(cd $(DOC_DIR)/man; groff -Thtml -m an-old-fixed -M . stylesheet.9 $(patsubst $(DOC_DIR)/man/%,%,$<)) > $@

SA := <p><a onclick=\"return toggle('man_
SB := ')\"><img id=\"man_
SC := _image\" src=\"plus.png\" alt=\"plus\" style=\"border:0;margin-right:5px;vertical-align:middle\"/>

SD := <div id=\"man_
SE := \" style=\"-moz-column-width: 25ex; \
	-moz-column-gap: 4ex; \
	-webkit-column-width: \
	25ex; -webkit-column-gap: 4ex;\">

#
# This function appends a section to the "manpages" HTML fragment called
# index.incl, listing all the specified manpages.  It takes 2 arguments:
#     * the part of the manpage space, for example "1" or "3rtapi"
#     * the manpage section title
#
ADD_HTML_MANPAGES = \
	@echo "Adding manpages: man$(strip $(1)): $(strip $(2))"; \
	echo "$(SA)$(strip $(1))$(SB)$(strip $(1))$(SC)$(strip $(2))</a></p>" >> objects/index.incl; \
	echo "$(SD)$(strip $(1))$(SE)" >> objects/index.incl; \
	echo "<ul>" >> objects/index.incl; \
	for HTML_FILE in $(filter $(DOC_DIR)/html/man/man$(strip $(1))/%.html, $(MAN_HTML_TARGETS)); do \
		BASENAME=$$(basename $$HTML_FILE .html); \
		echo "<li><a href=\"$${HTML_FILE\#$(DOC_DIR)/html/}\">$${BASENAME%.*}</a></li>"; \
	done >> objects/index.incl; \
	echo "</ul></div>" >> objects/index.incl


objects/index.incl: $(GENERATED_MANPAGES) objects/var-MAN_HTML_TARGETS $(DOC_SRCDIR)/Submakefile
	rm -f $@
	$(call ADD_HTML_MANPAGES, 1, Commands and userspace components)
	$(call ADD_HTML_MANPAGES, 9, Realtime components and kernel modules)
	$(call ADD_HTML_MANPAGES, 3hal, API calls: HAL)
	$(call ADD_HTML_MANPAGES, 3rtapi, API calls: RTAPI)
	$(call ADD_HTML_MANPAGES, 3hm2,  API calls: Hostmot2)
	$(call ADD_HTML_MANPAGES, 3, API calls: General)

	# now make sure all manpages made it into the html index
	FAIL=0; \
	for F in $$(find $(DOC_DIR)/man/? -type f); do \
		B=$$(basename $$F); \
		if ! grep -q $$B $@; then \
			echo stray manpage not added to index: $$B; \
			FAIL=1; \
		fi; \
	done; \
	if [ $$FAIL -ne 0 ]; then false; fi
	mkdir -p $(DOC_DIR)/html/man/man/images/
	find $(DOC_DIR)/man -name "*.png" ! -name "grohtml*" -exec mv {} "$(DOC_DIR)/html/man/man/images/" \;

$(DOC_DIR)/html/%/index.html: $(DOC_SRCDIR)/index_%.tmpl ../VERSION $(DOC_SRCDIR)/index.foot
	cat $(filter-out ../VERSION, $^) | \
		sed "s/@VERSION@/`cat ../VERSION`/" | \
		if [ "yes" != "$(BUILD_DOCS_TRANSLATED)" ]; then sed '/@TRANSLATIONS@/,/@ENDTRANSLATIONS@/d' ; else grep -Ev '@(END)?TRANSLATIONS@'; fi > $@

$(DOC_DIR)/html/index.html: $(DOC_SRCDIR)/index.tmpl objects/index.incl $(DOC_SRCDIR)/index.foot ../VERSION $(DOC_SRCDIR)/Submakefile
	(cat $(DOC_SRCDIR)/index.tmpl objects/index.incl $(DOC_SRCDIR)/index.foot) | sed "s/@VERSION@/`cat ../VERSION`/"  | \
		if [ "yes" != "$(BUILD_DOCS_TRANSLATED)" ]; then sed '/@TRANSLATIONS@/,/@ENDTRANSLATIONS@/d' ; else grep -Ev '@(END)?TRANSLATIONS@'; fi > $@

$(DOC_SRCDIR)/%.pdf: $(DOC_SRCDIR)/%.adoc svgs_made_from_dots .adoc-images-stamp
	$(ECHO) Building $@
	@rm -f $@
	$(A2X) -L -d book -vf pdf $< || (X=$$?; rm -f $@; exit $$X)
	@test -f $@

depends/%.d: $(DOC_SRCDIR)/%.adoc $(DOC_SRCDIR)/asciideps .include-stamp
	$(ECHO) Depending $<
	@mkdir -p $(dir $@)
	$(Q)$(DOC_SRCDIR)/asciideps $< > $@.tmp
	@mv $@.tmp $@

objects/%.links-stamp: $(DOC_SRCDIR)/%.adoc $(DOC_SRCDIR)/links.xslt
	@mkdir -p `dirname $@`
	asciidoc -f $(DOC_SRCDIR)/attribute-colon.conf -a "scriptdir=$(DOC_SRCDIR)/" -d book -o- -b docbook $< | xsltproc $(DOC_SRCDIR)/links.xslt - > $@.tmp || (X=$$?; rm -f $@; exit $$X)
	sh move-if-change $@.tmp $(patsubst %-stamp,%,$@)
	touch $@

objects/%.links: objects/%.links-stamp
	@:

# Secondary is not working here.
# See http://www.gnu.org/software/make/manual/make.html#Chained-Rules
.PRECIOUS: objects/%.links-stamp

objects/xref_en.links: $(patsubst %.adoc,objects/%.links,$(DOC_SRCS_EN_SMALL))
	$(PYTHON) $(DOC_SRCDIR)/links_db_gen.py objects/ $^ > $@

objects/xref_de.links: $(patsubst %.adoc,objects/%.links,$(DOC_SRCS_DE_SMALL))
	$(PYTHON) $(DOC_SRCDIR)/links_db_gen.py objects/ $^ > $@

objects/xref_es.links: $(patsubst %.adoc,objects/%.links,$(DOC_SRCS_ES_SMALL))
	$(PYTHON) $(DOC_SRCDIR)/links_db_gen.py objects/ $^ > $@

objects/xref_fr.links: $(patsubst %.adoc,objects/%.links,$(DOC_SRCS_FR_SMALL))
	$(PYTHON) $(DOC_SRCDIR)/links_db_gen.py objects/ $^ > $@

objects/xref_hu.links: $(patsubst %.adoc,objects/%.links,$(DOC_SRCS_HU_SMALL))
	$(PYTHON) $(DOC_SRCDIR)/links_db_gen.py objects/ $^ > $@

objects/xref_nb.links: $(patsubst %.adoc,objects/%.links,$(DOC_SRCS_NB_SMALL))
	$(PYTHON) $(DOC_SRCDIR)/links_db_gen.py objects/ $^ > $@

objects/xref_zh_CN.links: $(patsubst %.adoc,objects/%.links,$(DOC_SRCS_ZH_CN_SMALL))
	$(PYTHON) $(DOC_SRCDIR)/links_db_gen.py objects/ $^ > $@

$(DOC_TARGETS_HTML): $(DOC_DIR)/html/%.html: $(DOC_SRCDIR)/%.html
	@d=`dirname $*`; \
	mkdir -p $(shell dirname $@)
	@cp $< $@

.images-stamp: .adoc-images-stamp .html-images-stamp
	touch $@

.html-images-stamp: $(DOC_TARGETS_HTML)
	set -e; for HTML_FILE in $^; do \
		HTML_DIR=$$(basename $$(dirname $$HTML_FILE)); \
		for IMAGE_FILE in $$(xsltproc --novalid --nonet $(DOC_SRCDIR)/html-images.xslt $$HTML_FILE); do \
			IMAGE_DIR=$$(dirname $$IMAGE_FILE); \
			IMAGE_PATH=$$(echo $(DOC_SRCDIR)/$$HTML_DIR/$$IMAGE_FILE | sed -E 's%/src/($(LANGUAGES_MATCH))/%/src/%'); \
			mkdir -p $(DOC_DIR)/html/$$HTML_DIR/$$IMAGE_DIR; \
			cp -f $$IMAGE_PATH $(DOC_DIR)/html/$$HTML_DIR/$$IMAGE_FILE; \
		done; \
		mkdir -p objects/image-cache; \
		HTML_LATEX_CACHE=objects/image-cache $(DOC_SRCDIR)/html-latex-images $$HTML_FILE || (X=$$?; rm $$HTML_FILE; exit $$X); \
	done > $@.new && mv $@.new $@

# Hack to avoid ../../../src/ style include which do not work with
# translated documents.  Copy the ini file where the documentation
# build can find it without ../.. paths.
.include-stamp: hal/user_comps/mb2hal/mb2hal_HOWTO.ini emc/usr_intf/gmoccapy/release_notes.txt
	(set -x; set -e; \
	cp -f hal/user_comps/mb2hal/mb2hal_HOWTO.ini $(DOC_DIR)/src/drivers/;\
	mkdir -p $(DOC_DIR)/html/drivers/; \
	cp -f hal/user_comps/mb2hal/mb2hal_HOWTO.ini $(DOC_DIR)/html/drivers/;\
	cp -f emc/usr_intf/gmoccapy/release_notes.txt $(DOC_DIR)/src/gui/gmoccapy_release_notes.txt;\
	mkdir -p $(DOC_DIR)/html/gui/; \
	cp -f emc/usr_intf/gmoccapy/release_notes.txt $(DOC_DIR)/html/gui/gmoccapy_release_notes.txt;\
	for lang in $(LANGUAGES); do \
		mkdir -p $(DOC_DIR)/src/$$lang/drivers/; \
		mkdir -p $(DOC_DIR)/html/$$lang/drivers/; \
		cp -f hal/user_comps/mb2hal/mb2hal_HOWTO.ini $(DOC_DIR)/src/$$lang/drivers/ ; \
		cp -f hal/user_comps/mb2hal/mb2hal_HOWTO.ini $(DOC_DIR)/html/$$lang/drivers/ ; \
		mkdir -p $(DOC_DIR)/src/$$lang/gui/; \
		mkdir -p $(DOC_DIR)/html/$$lang/gui/; \
		cp -f emc/usr_intf/gmoccapy/release_notes.txt $(DOC_DIR)/src/$$lang/gui/gmoccapy_release_notes.txt ; \
		cp -f emc/usr_intf/gmoccapy/release_notes.txt $(DOC_DIR)/html/$$lang/gui/gmoccapy_release_notes.txt ; \
	done) > $@.new && mv $@.new $@

# Copy all images used by translated adoc files into the directories
# with translated adoc files.
.adoc-images-stamp: translateddocs $(addprefix $(DOC_SRCDIR)/, $(filter-out $(DOC_SRCS_EN), $(DOC_SRCS)))
	set -e; for ADOC_FILE in $(addprefix $(DOC_SRCDIR)/, $(filter-out $(DOC_SRCS_EN), $(DOC_SRCS))); do \
		ADOC_DIR=$$(echo $$(dirname $$ADOC_FILE) | sed s%$(DOC_SRCDIR)/%% ); \
		echo Processing $$ADOC_FILE, dir $$ADOC_DIR; \
		for IMAGE_FILE in $$(grep -E ^image:[^[:space:]] $$ADOC_FILE | sed -E "s/image:+([^[]+)\[/\nimage:\1\n/g" | grep image: | cut -d: -f2-); do \
			IMAGE_DIR=$$(dirname $$IMAGE_FILE); \
			IMAGE_PATH=$$(echo $(DOC_SRCDIR)/$$ADOC_DIR/$$IMAGE_FILE | sed -E 's%/src/($(LANGUAGES_MATCH))/%/src/%'); \
			TIMAGE_PATH=$(DOC_DIR)/src/$$ADOC_DIR/$$IMAGE_FILE; \
			HIMAGE_PATH=$(DOC_DIR)/html/$$ADOC_DIR/$$IMAGE_FILE; \
			mkdir -p $(DOC_DIR)/src/$$ADOC_DIR/$$IMAGE_DIR; \
			mkdir -p $(DOC_DIR)/html/$$ADOC_DIR/$$IMAGE_DIR; \
			if [ ! -e $$TIMAGE_PATH ] ; then \
				echo "Generating $$TIMAGE_PATH for $$ADOC_FILE"; \
				cp -f $$IMAGE_PATH $$TIMAGE_PATH; \
				cp -f $$IMAGE_PATH $$HIMAGE_PATH; \
			fi ; \
		done; \
	done > $@.new && mv $@.new $@

# Define a target-specific variable called STYLES_SCRIPTS_DIR, which has
# the relative path from this html target to the root of the generated
# document tree (docs/html in the git repo).  This is where the CSS files
# and javascript files will be installed.
$(DOC_SRCDIR)/%.html: STYLES_SCRIPTS=$(shell \
    D=$$(python3 -c "print('../' * '$*'.count('/'))"); \
    if [ ! -z $$D ]; then \
        echo "-a 'scriptsdir=$$D' -a 'stylesdir=$$D'"; \
    fi \
)

$(patsubst %.adoc,$(DOC_SRCDIR)/%.html,$(DOC_SRCS_EN_SMALL)): $(DOC_SRCDIR)/%.html: $(DOC_SRCDIR)/%.adoc objects/xref_en.links $(LOC_LANG_MAP)
	asciidoc -f $(DOC_SRCDIR)/xhtml11.conf \
		 -f $(DOC_SRCDIR)/asciidoc-dont-replace-arrows.conf \
		 -f $(LOC_HL_DIR)/emc-langs-source-highlight.conf \
		 -a "source_highlight_dir=$(LOC_HL_DIR)/local" \
		 -a linkcss \
		 $(STYLES_SCRIPTS) \
	         -a "scriptdir=$(DOC_SRCDIR)/" \
	         -a "relindir=$(shell dirname $*)" \
	         -a "linksfile=objects/xref_en.links" \
		 -a stylesheet=linuxcnc.css \
		 -d book -a toc -a numbered -b xhtml11 $< || (X=$$?; rm -f $@; exit $$X)

$(patsubst %.adoc,$(DOC_SRCDIR)/%.html,$(DOC_SRCS_DE_SMALL)): $(DOC_SRCDIR)/%.html: $(DOC_SRCDIR)/%.adoc objects/xref_de.links $(LOC_LANG_MAP)
	asciidoc -f $(DOC_SRCDIR)/xhtml11.conf \
		 -f $(DOC_SRCDIR)/asciidoc-dont-replace-arrows.conf \
		 -f $(LOC_HL_DIR)/emc-langs-source-highlight.conf \
		 -a "source_highlight_dir=$(LOC_HL_DIR)/local" \
		 -a linkcss \
		 $(STYLES_SCRIPTS) \
	         -a "scriptdir=$(DOC_SRCDIR)/" \
	         -a "relindir=$(shell dirname $*)" \
	         -a "linksfile=objects/xref_de.links" \
		 -a stylesheet=linuxcnc.css \
		 -d book -a toc -a numbered -b xhtml11 $< || (X=$$?; rm -f $@; exit $$X)

$(patsubst %.adoc,$(DOC_SRCDIR)/%.html,$(DOC_SRCS_ES_SMALL)): $(DOC_SRCDIR)/%.html: $(DOC_SRCDIR)/%.adoc objects/xref_es.links $(LOC_LANG_MAP)
	asciidoc -f $(DOC_SRCDIR)/xhtml11.conf \
		 -f $(DOC_SRCDIR)/asciidoc-dont-replace-arrows.conf \
		 -f $(LOC_HL_DIR)/emc-langs-source-highlight.conf \
		 -a "source_highlight_dir=$(LOC_HL_DIR)/local" \
		 -a linkcss \
		 $(STYLES_SCRIPTS) \
	         -a "scriptdir=$(DOC_SRCDIR)/" \
	         -a "relindir=$(shell dirname $*)" \
	         -a "linksfile=objects/xref_es.links" \
		 -a stylesheet=linuxcnc.css \
		 -d book -a toc -a numbered -b xhtml11 $< || (X=$$?; rm -f $@; exit $$X)

$(patsubst %.adoc,$(DOC_SRCDIR)/%.html,$(DOC_SRCS_FR_SMALL)): $(DOC_SRCDIR)/%.html: $(DOC_SRCDIR)/%.adoc objects/xref_fr.links $(LOC_LANG_MAP)
	asciidoc -f $(DOC_SRCDIR)/xhtml11.conf \
		 -f $(DOC_SRCDIR)/asciidoc-dont-replace-arrows.conf \
		 -f $(LOC_HL_DIR)/emc-langs-source-highlight.conf \
		 -a "source_highlight_dir=$(LOC_HL_DIR)/local" \
		 -a linkcss \
		 $(STYLES_SCRIPTS) \
	         -a "scriptdir=$(DOC_SRCDIR)/" \
	         -a "relindir=$(shell dirname $*)" \
	         -a "linksfile=objects/xref_fr.links" \
		 -a stylesheet=linuxcnc.css \
		 -d book -a toc -a numbered -b xhtml11 $< || (X=$$?; rm -f $@; exit $$X)

$(patsubst %.adoc,$(DOC_SRCDIR)/%.html,$(DOC_SRCS_HU_SMALL)): $(DOC_SRCDIR)/%.html: $(DOC_SRCDIR)/%.adoc objects/xref_hu.links $(LOC_LANG_MAP)
	asciidoc -f $(DOC_SRCDIR)/xhtml11.conf \
		 -f $(DOC_SRCDIR)/asciidoc-dont-replace-arrows.conf \
		 -f $(LOC_HL_DIR)/emc-langs-source-highlight.conf \
		 -a "source_highlight_dir=$(LOC_HL_DIR)/local" \
		 -a linkcss \
		 $(STYLES_SCRIPTS) \
	         -a "scriptdir=$(DOC_SRCDIR)/" \
	         -a "relindir=$(shell dirname $*)" \
	         -a "linksfile=objects/xref_hu.links" \
		 -a stylesheet=linuxcnc.css \
		 -d book -a toc -a numbered -b xhtml11 $< || (X=$$?; rm -f $@; exit $$X)

$(patsubst %.adoc,$(DOC_SRCDIR)/%.html,$(DOC_SRCS_NB_SMALL)): $(DOC_SRCDIR)/%.html: $(DOC_SRCDIR)/%.adoc objects/xref_nb.links $(LOC_LANG_MAP)
	asciidoc -f $(DOC_SRCDIR)/xhtml11.conf \
		 -f $(DOC_SRCDIR)/asciidoc-dont-replace-arrows.conf \
		 -f $(LOC_HL_DIR)/emc-langs-source-highlight.conf \
		 -a "source_highlight_dir=$(LOC_HL_DIR)/local" \
		 -a linkcss \
		 $(STYLES_SCRIPTS) \
	         -a "scriptdir=$(DOC_SRCDIR)/" \
	         -a "relindir=$(shell dirname $*)" \
	         -a "linksfile=objects/xref_nb.links" \
		 -a stylesheet=linuxcnc.css \
		 -d book -a toc -a numbered -b xhtml11 $< || (X=$$?; rm -f $@; exit $$X)

$(patsubst %.adoc,$(DOC_SRCDIR)/%.html,$(DOC_SRCS_ZH_CN_SMALL)): $(DOC_SRCDIR)/%.html: $(DOC_SRCDIR)/%.adoc objects/xref_zh_CN.links $(LOC_LANG_MAP)
	asciidoc -f $(DOC_SRCDIR)/xhtml11.conf \
		 -f $(DOC_SRCDIR)/asciidoc-dont-replace-arrows.conf \
		 -a linkcss \
		 $(STYLES_SCRIPTS) \
	         -a "scriptdir=$(DOC_SRCDIR)/" \
	         -a "relindir=$(shell dirname $*)" \
	         -a "linksfile=objects/xref_zh_CN.links" \
		 -a stylesheet=linuxcnc.css \
		 -d book -a toc -a numbered -b xhtml11 $< || (X=$$?; rm -f $@; exit $$X)

$(DOC_DIR)/html/de/xref.html: objects/xref_de.xml $(DOC_SRCDIR)/xref.xsl $(DOC_SRCDIR)/docs.xml $(DOC_SRCDIR)/terms.xml
	$(ECHO) Converting $< to HTML
	@xsltproc --stringparam docname "xref_de" --stringparam language spanish --path objects -o $@ $(DOC_SRCDIR)/xref.xsl $<

$(DOC_DIR)/html/es/xref.html: objects/xref_es.xml $(DOC_SRCDIR)/xref.xsl $(DOC_SRCDIR)/docs.xml $(DOC_SRCDIR)/terms.xml
	$(ECHO) Converting $< to HTML
	@xsltproc --stringparam docname "xref_es" --stringparam language spanish --path objects -o $@ $(DOC_SRCDIR)/xref.xsl $<

$(DOC_DIR)/html/fr/xref.html: objects/xref_fr.xml $(DOC_SRCDIR)/xref.xsl $(DOC_SRCDIR)/fr/docs.xml $(DOC_SRCDIR)/terms.xml
	$(ECHO) Converting $< to HTML
	@xsltproc --stringparam docname "xref_fr" --stringparam language french --path objects -o $@ $(DOC_SRCDIR)/xref.xsl $<

$(DOC_DIR)/html/hu/xref.html: objects/xref_hu.xml $(DOC_SRCDIR)/xref.xsl $(DOC_SRCDIR)/docs.xml $(DOC_SRCDIR)/terms.xml
	$(ECHO) Converting $< to HTML
	@xsltproc --stringparam docname "xref_hu" --stringparam language hungarian --path objects -o $@ $(DOC_SRCDIR)/xref.xsl $<

$(DOC_DIR)/html/nb/xref.html: objects/xref_nb.xml $(DOC_SRCDIR)/xref.xsl $(DOC_SRCDIR)/docs.xml $(DOC_SRCDIR)/terms.xml
	$(ECHO) Converting $< to HTML
	@xsltproc --stringparam docname "xref_nb" --stringparam language spanish --path objects -o $@ $(DOC_SRCDIR)/xref.xsl $<

$(DOC_DIR)/html/zh_CN/xref.html: objects/xref_zh_CN.xml $(DOC_SRCDIR)/xref.xsl $(DOC_SRCDIR)/docs.xml $(DOC_SRCDIR)/terms.xml
	$(ECHO) Converting $< to HTML
	@xsltproc --stringparam docname "xref_zh_CN" --stringparam language chinese --path objects -o $@ $(DOC_SRCDIR)/xref.xsl $<

default: docs

install-doc-pdf:
	$(DIR) $(DESTDIR)$(docsdir)
	$(FILE) $(PDF_TARGETS) $(DESTDIR)$(docsdir)

install-doc-html:
	$(DIR) $(DESTDIR)$(docsdir)
	cp -a $(DOC_DIR)/html $(DESTDIR)$(docsdir)

docclean:
	-rm -f $(DOC_DIR)/LinuxCNC_Getting_Started.pdf
	-rm -f $(DOC_DIR)/LinuxCNC_Getting_Started_*.pdf
	-rm -f $(DOC_DIR)/LinuxCNC_Documentation.pdf
	-rm -f $(DOC_DIR)/LinuxCNC_Documentation_*.pdf
	-rm -f $(DOC_DIR)/LinuxCNC_Integrator.pdf
	-rm -f $(DOC_DIR)/LinuxCNC_Integrator_*.pdf
	-rm -f $(DOC_DIR)/LinuxCNC_Developer.pdf
	-rm -f $(DOC_DIR)/LinuxCNC_Developer_*.pdf
	-rm -f $(DOC_DIR)/LinuxCNC_Manual_Pages.pdf
	-rm -f $(DOC_SRCDIR)/*.d
	-rm -f $(DOC_SRCDIR)/*.pdf
	-rm -f $(DOC_SRCDIR)/*/*.html
	-rm -f $(DOC_SRCDIR)/index_*.tmpl
	-rm -f $(DOC_SRCDIR)/drivers/mb2hal_HOWTO.ini
	-rm -f $(DOC_SRCDIR)/*/drivers/mb2hal_HOWTO.ini
	-rm -f $(DOC_SRCDIR)/gui/gmoccapy_release_notes.txt
	-rm -f $(DOC_SRCDIR)/*/gui/gmoccapy_release_notes.txt
	-rm -f $(DOC_DIR)/html/*/drivers/mb2hal_HOWTO.ini
	-rm -f $(DOC_DIR)/html/drivers/mb2hal_HOWTO.ini
	-rm -f $(DOC_DIR)/html/gui/gmoccapy_release_notes.txt
	-rm -f $(DOC_DIR)/html/*/gui/gmoccapy_release_notes.txt
	-rm -f $(DOC_DIR)/html/code/*.*
	-rm -f $(DOC_DIR)/html/common/images/*.*
	-rm -f $(DOC_DIR)/html/config/images/*.*
	-rm -f $(DOC_DIR)/html/drivers/images/*.*
	-rm -f $(DOC_DIR)/html/examples/images/*.*
	-rm -f $(DOC_DIR)/html/gcode/images/*.*
	-rm -f $(DOC_DIR)/html/getting-started/images/*.*
	-rm -f $(DOC_DIR)/html/gui/images/*.*
	-rm -f $(DOC_DIR)/html/hal/images/*.*
	-rm -f $(DOC_DIR)/html/install/images/*.*
	-rm -f $(DOC_DIR)/html/ladder/images/*.*
	-rm -f $(DOC_DIR)/html/lathe/images/*.*
	-rm -f $(DOC_DIR)/html/motion/images/*.*
	-rm -f $(DOC_DIR)/html/plasma/images/*.*
	-rm -f $(DOC_DIR)/html/remap/images/*.*
	-rm -f $(DOC_DIR)/html/man/man1/*.*
	-rm -f $(DOC_DIR)/html/man/man3/*.*
	-rm -f $(DOC_DIR)/html/man/man9/*.*
	-rm -f $(DOC_TARGETS_HTML) $(DOC_DIR)/html/xref*.html $(DOC_DIR)/html/index*.html $(DOC_DIR)/*.png $(DOC_DIR)/man/*.png
	-rm -f .htmldoc-stamp
	-rm -f .adoc-images-stamp
	-rm -f .html-images-stamp
	-rm -f .images-stamp
	-rm -f .include-stamp
	-rm -f $(DOTFILES:.dot=.svg)


MAN_DEPS := $(patsubst $(DOC_DIR)/man/%, depends/%.d, $(MAN_SRCS))
$(MAN_DEPS): depends/%.d: $(DOC_DIR)/man/%
	@echo Depending $(notdir $<)
	@mkdir -p $(dir $@)
	$(Q)echo -n "$(DOC_DIR)/html/man/$*.html: $<" > $@.tmp
	$(Q)grep '^\.so ' $< | awk '{printf " \\\n\t$(DOC_DIR)/man/%s", $$2}' >> $@.tmp
	$(Q)echo >> $@.tmp
	$(Q)mv -f $@.tmp $@

ifeq ($(TRIVIAL_BUILD),no)
-include $(MAN_DEPS)
Makefile: $(MAN_DEPS)
endif

%.png: %.dot
	dot -Tpng -o$@ $<

%.svg: %.dot
	dot -Tsvg -o$@ $<

%.png:; $(error Required image file $@ does not exist)
%.jpg:; $(error Required image file $@ does not exist)
%.svg:; $(error Required image file $@ does not exist)
%.dxf:; $(error Required image file $@ does not exist)
%.ps:; $(error Required image file $@ does not exist)
%.eps:; $(error Required image file $@ does not exist)

else
:
	@:

docs:
	$(error Cannot build documents, missing AsciiDoc or some other required program, or explicitly disabled in configure)

endif

manpages: $(GENERATED_MANPAGES)
TARGETS += manpages

# make manpages from all the asciidoc manpage-sources
# Note that in some versions of asciidoc, including the one in Debian Jessie,
# a2x spuriously warns that this --destination-dir is ignored.  It is *NOT*
# ignored, don't remove it trying to fix the diagnostic.
# For more information, see https://github.com/asciidoc/asciidoc/issues/44
GENERATED_MANPAGES += $(patsubst $(DOC_DIR)/src/man/%.adoc, $(DOC_DIR)/man/%, $(wildcard $(DOC_DIR)/src/man/man?/*.adoc))
$(DOC_DIR)/man/%: $(DOC_DIR)/src/man/%.adoc
	@mkdir -p `dirname $@`
	a2x -v --doctype manpage \
		--format manpage \
		--destination-dir `dirname $@` \
		--xsltproc-opts="--nonet" \
		-a mansource=LinuxCNC \
		-a manmanual='LinuxCNC Documentation' \
		$<
