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

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

MAN_SRCS := $(sort \
	$(patsubst %.in,%,$(wildcard $(DOC_DIR)/man/man1/*.1  $(DOC_DIR)/man/man1/*.1.in)) \
	$(filter-out $(DOC_DIR)/man/man3/skeleton%, $(wildcard $(DOC_DIR)/man/man3/*.3rtapi $(DOC_DIR)/man/man3/*.3hal)) \
	$(filter-out $(DOC_DIR)/man/man9/skeleton%, $(wildcard $(DOC_DIR)/man/man9/*.9)) \
	$(COMP_MANPAGES) $(COMP_DRIVER_MANPAGES))

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

ifeq ($(BUILD_DOCS),yes)
DOC_SRCS := \
	code/Code_Notes.txt \
	code/Style_Guide.txt \
	code/NML_Messages.txt \
	common/Document_Header.txt \
	common/Document_Header_de.txt \
	common/Document_Header_es.txt \
	common/Document_Header_fr.txt \
	common/Document_Header_pl.txt \
	common/emc-history.txt \
	common/Getting_EMC.txt \
	common/Getting_EMC_de.txt \
	common/Getting_EMC_es.txt \
	common/Getting_linuxcnc_fr.txt \
	common/Getting_EMC_pl.txt \
	common/Glossary.txt \
	common/Glossary_de.txt \
	common/Glossary_es.txt \
	common/Glossary_fr.txt \
	common/Glossary_pl.txt \
	common/GPLD_Copyright.txt \
	common/GPLD_Copyright_fr.txt \
	common/GPLD_Copyright_pl.txt \
	common/Integrator_Concepts.txt \
	common/Integrator_Concepts_de.txt \
	common/Integrator_Concepts_es.txt \
	common/Integrator_Concepts_fr.txt \
	common/Integrator_Concepts_pl.txt \
	common/Linux_FAQ.txt \
	common/Linux_FAQ_de.txt \
	common/Linux_FAQ_es.txt \
	common/Linux_FAQ_fr.txt \
	common/Linux_FAQ_pl.txt \
	common/outdated-notice_fr.txt \
	common/overleaf.txt \
	common/overleaf_de.txt \
	common/overleaf_fr.txt \
	common/overleaf_pl.txt \
	common/starting-emc.txt \
	common/starting-emc_de.txt \
	common/starting-emc_es.txt \
	common/starting-emc_pl.txt \
	common/Stepper_Diagnostics.txt \
	common/Stepper_Diagnostics_de.txt \
	common/Stepper_Diagnostics_es.txt \
	common/Stepper_Diagnostics_fr.txt \
	common/Stepper_Diagnostics_pl.txt \
	common/System_Requirements.txt \
	common/System_Requirements_de.txt \
	common/System_Requirements_es.txt \
	common/System_Requirements_fr.txt \
	common/System_Requirements_pl.txt \
	common/translator-needed_de.txt \
	common/translator-needed_es.txt \
	common/Updating_EMC.txt \
	common/Updating_EMC_de.txt \
	common/Updating_EMC_es.txt \
	common/Updating_linuxcnc_fr.txt \
	common/Updating_EMC_pl.txt \
	common/User_Concepts.txt \
	common/User_Concepts_de.txt \
	common/User_Concepts_es.txt \
	common/User_Concepts_fr.txt \
	common/User_Concepts_pl.txt \
	common/userforeword.txt \
	common/userforeword_de.txt \
	common/userforeword_es.txt \
	common/userforeword_fr.txt \
	common/userforeword_pl.txt \
	common/user_intro.txt \
	common/user_intro_de.txt \
	common/user_intro_es.txt \
	common/user_intro_fr.txt \
	common/user_intro_pl.txt \
	config/copy_and_run.txt \
	config/copy_and_run_de.txt \
	config/copy_and_run_es.txt \
	config/copy_and_run_pl.txt \
	config/emc2hal.txt \
	config/emc2hal_de.txt \
	config/emc2hal_es.txt \
	config/linuxcnc2hal_fr.txt \
	config/emc2hal_pl.txt \
	config/ini_config.txt \
	config/ini_config_de.txt \
	config/ini_config_es.txt \
	config/ini_config_fr.txt \
	config/ini_config_pl.txt \
	config/ini_homing.txt \
	config/ini_homing_de.txt \
	config/ini_homing_es.txt \
	config/ini_homing_fr.txt \
	config/ini_homing_pl.txt \
	config/lathe_config.txt \
	config/lathe_config_de.txt \
	config/lathe_config_es.txt \
	config/lathe_config_fr.txt \
	config/lathe_config_pl.txt \
	config/pncconf.txt \
	config/pncconf_de.txt \
	config/pncconf_es.txt \
	config/pncconf_fr.txt \
	config/pncconf_pl.txt \
	config/stepconf.txt \
	config/stepconf_de.txt \
	config/stepconf_es.txt \
	config/stepconf_fr.txt \
	config/stepconf_pl.txt \
	config/stepper.txt \
	config/stepper_de.txt \
	config/stepper_es.txt \
	config/stepper_fr.txt \
	config/stepper_pl.txt \
	drivers/AX5214H.txt \
	drivers/AX5214H_de.txt \
	drivers/AX5214H_es.txt \
	drivers/AX5214H_fr.txt \
	drivers/AX5214H_pl.txt \
	drivers/GS2.txt \
	drivers/GS2_de.txt \
	drivers/GS2_es.txt \
	drivers/GS2_fr.txt \
	drivers/GS2_pl.txt \
	drivers/hostmot2.txt \
	drivers/hostmot2_de.txt \
	drivers/hostmot2_es.txt \
	drivers/hostmot2_fr.txt \
	drivers/hostmot2_pl.txt \
	drivers/motenc.txt \
	drivers/motenc_de.txt \
	drivers/motenc_es.txt \
	drivers/motenc_fr.txt \
	drivers/motenc_pl.txt \
	drivers/opto22.txt \
	drivers/opto22_de.txt \
	drivers/opto22_es.txt \
	drivers/opto22_fr.txt \
	drivers/opto22_pl.txt \
	drivers/pico_ppmc.txt \
	drivers/pico_ppmc_de.txt \
	drivers/pico_ppmc_es.txt \
	drivers/pico_ppmc_fr.txt \
	drivers/pico_ppmc_pl.txt \
	drivers/pluto_p.txt \
	drivers/pluto_p_de.txt \
	drivers/pluto_p_es.txt \
	drivers/pluto_p_fr.txt \
	drivers/pluto_p_pl.txt \
	drivers/servo_to_go.txt \
	drivers/servo_to_go_de.txt \
	drivers/servo_to_go_es.txt \
	drivers/servo_to_go_fr.txt \
	drivers/servo_to_go_pl.txt \
	drivers/shuttlexpress.txt \
	drivers/shuttlexpress_es.txt \
	drivers/shuttlexpress_de.txt \
	drivers/shuttlexpress_pl.txt \
	examples/gcode.txt \
	examples/gcode_de.txt \
	examples/gcode_es.txt \
	examples/gcode_fr.txt \
	examples/gcode_pl.txt \
	examples/gs2_example.txt \
	examples/gs2_example_de.txt \
	examples/gs2_example_es.txt \
	examples/gs2_example_fr.txt \
	examples/gs2_example_pl.txt \
	examples/misc_fr.txt \
	examples/mpg.txt \
	examples/mpg_de.txt \
	examples/mpg_es.txt \
	examples/mpg_fr.txt \
	examples/mpg_pl.txt \
	examples/pci_parallel_port.txt \
	examples/pci_parallel_port_de.txt \
	examples/pci_parallel_port_es.txt \
	examples/pci_parallel_port_fr.txt \
	examples/pci_parallel_port_pl.txt \
	examples/spindle.txt \
	examples/spindle_de.txt \
	examples/spindle_es.txt \
	examples/spindle_fr.txt \
	examples/spindle_pl.txt \
	gcode/coordinates.txt \
	gcode/coordinates_de.txt \
	gcode/coordinates_es.txt \
	gcode/coordinates_fr.txt \
	gcode/coordinates_pl.txt \
	gcode/gcode.txt \
	gcode/gcode_de.txt \
	gcode/gcode_es.txt \
	gcode/gcode_fr.txt \
	gcode/gcode_pl.txt \
	gcode/machining_center.txt \
	gcode/machining_center_de.txt \
	gcode/machining_center_es.txt \
	gcode/machining_center_fr.txt \
	gcode/machining_center_pl.txt \
	gcode/m-code.txt \
	gcode/m-code_de.txt \
	gcode/m-code_es.txt \
	gcode/m-code_fr.txt \
	gcode/m-code_pl.txt \
	gcode/o-code.txt \
	gcode/o-code_de.txt \
	gcode/o-code_es.txt \
	gcode/o-code_fr.txt \
	gcode/o-code_pl.txt \
	gcode/other-code.txt \
	gcode/other-code_de.txt \
	gcode/other-code_es.txt \
	gcode/other-code_fr.txt \
	gcode/other-code_pl.txt \
	gcode/overview.txt \
	gcode/overview_de.txt \
	gcode/overview_es.txt \
	gcode/overview_fr.txt \
	gcode/overview_pl.txt \
	gcode/rs274ngc.txt \
	gcode/rs274ngc_de.txt \
	gcode/rs274ngc_es.txt \
	gcode/rs274ngc_fr.txt \
	gcode/rs274ngc_pl.txt \
	gcode/tool_compensation.txt \
	gcode/tool_compensation_de.txt \
	gcode/tool_compensation_es.txt \
	gcode/tool_compensation_fr.txt \
	gcode/tool_compensation_pl.txt \
	gui/axis.txt \
	gui/axis_de.txt \
	gui/axis_es.txt \
	gui/axis_fr.txt \
	gui/axis_pl.txt \
	gui/gladevcp.txt \
	gui/gladevcp_de.txt \
	gui/gladevcp_es.txt \
	gui/gladevcp_fr.txt \
	gui/gladevcp_pl.txt \
	gui/halui.txt \
	gui/halui_de.txt \
	gui/halui_es.txt \
	gui/halui_pl.txt \
	gui/image-to-gcode.txt \
	gui/image-to-gcode_de.txt \
	gui/image-to-gcode_es.txt \
	gui/image-to-gcode_fr.txt \
	gui/image-to-gcode_pl.txt \
	gui/keystick.txt \
	gui/keystick_de.txt \
	gui/keystick_es.txt \
	gui/keystick_fr.txt \
	gui/keystick_pl.txt \
	gui/mini.txt \
	gui/mini_de.txt \
	gui/mini_es.txt \
	gui/mini_fr.txt \
	gui/mini_pl.txt \
	gui/ngcgui.txt \
	gui/ngcgui_de.txt \
	gui/ngcgui_es.txt \
	gui/ngcgui_fr.txt \
	gui/ngcgui_pl.txt \
	gui/tklinuxcnc.txt \
	gui/tklinuxcnc_de.txt \
	gui/tklinuxcnc_es.txt \
	gui/tklinuxcnc_fr.txt \
	gui/tklinuxcnc_pl.txt \
	gui/touchy.txt \
	gui/touchy_de.txt \
	gui/touchy_es.txt \
	gui/touchy_fr.txt \
	gui/touchy_pl.txt \
	hal/basic_hal.txt \
	hal/basic_hal_de.txt \
	hal/basic_hal_es.txt \
	hal/basic_hal_fr.txt \
	hal/basic_hal_pl.txt \
	hal/canonical-devices.txt \
	hal/canonical-devices_de.txt \
	hal/canonical-devices_es.txt \
	hal/canonical-devices_pl.txt \
	hal/comp.txt \
	hal/comp_de.txt \
	hal/comp_es.txt \
	hal/comp_fr.txt \
	hal/comp_pl.txt \
	hal/components.txt \
	hal/components_de.txt \
	hal/components_es.txt \
	hal/components_fr.txt \
	hal/components_pl.txt \
	hal/general_ref.txt \
	hal/general_ref_de.txt \
	hal/general_ref_es.txt \
	hal/general_ref_fr.txt \
	hal/general_ref_pl.txt \
	hal/hal-examples.txt \
	hal/hal-examples_de.txt \
	hal/hal-examples_es.txt \
	hal/hal-examples_fr.txt \
	hal/hal-examples_pl.txt \
	hal/halmodule.txt \
	hal/halmodule_de.txt \
	hal/halmodule_es.txt \
	hal/halmodule_fr.txt \
	hal/halmodule_pl.txt \
	hal/halshow.txt \
	hal/halshow_de.txt \
	hal/halshow_es.txt \
	hal/halshow_fr.txt \
	hal/halshow_pl.txt \
	hal/haltcl.txt \
	hal/haltcl_de.txt \
	hal/haltcl_es.txt \
	hal/haltcl_fr.txt \
	hal/haltcl_pl.txt \
	hal/halui_fr.txt \
	hal/halui_examples.txt \
	hal/halui_examples_de.txt \
	hal/halui_examples_es.txt \
	hal/halui_examples_pl.txt \
	hal/intro.txt \
	hal/intro_de.txt \
	hal/intro_es.txt \
	hal/intro_fr.txt \
	hal/intro_pl.txt \
	hal/parallel_port.txt \
	hal/parallel_port_de.txt \
	hal/parallel_port_es.txt \
	hal/parallel_port_fr.txt \
	hal/parallel_port_pl.txt \
	hal/pyvcp.txt \
	hal/pyvcp_de.txt \
	hal/pyvcp_es.txt \
	hal/pyvcp_fr.txt \
	hal/pyvcp_pl.txt \
	hal/pyvcp_examples.txt \
	hal/pyvcp_examples_de.txt \
	hal/pyvcp_examples_es.txt \
	hal/pyvcp_examples_fr.txt \
	hal/pyvcp_examples_pl.txt \
	hal/rtcomps.txt \
	hal/rtcomps_de.txt \
	hal/rtcomps_es.txt \
	hal/rtcomps_fr.txt \
	hal/rtcomps_pl.txt \
	hal/tools.txt \
	hal/tools_de.txt \
	hal/tools_es.txt \
	hal/tools_pl.txt \
	hal/tutorial.txt \
	hal/tutorial_de.txt \
	hal/tutorial_es.txt \
	hal/tutorial_fr.txt \
	hal/tutorial_pl.txt \
	install/Latency_Test.txt \
	install/Latency_Test_de.txt \
	install/Latency_Test_es.txt \
	install/Latency_Test_fr.txt \
	install/Latency_Test_pl.txt \
	ladder/classic_ladder.txt \
	ladder/classic_ladder_de.txt \
	ladder/classic_ladder_es.txt \
	ladder/classic_ladder_fr.txt \
	ladder/classic_ladder_pl.txt \
	ladder/ladder_examples.txt \
	ladder/ladder_examples_de.txt \
	ladder/ladder_examples_es.txt \
	ladder/ladder_examples_fr.txt \
	ladder/ladder_examples_pl.txt \
	ladder/ladder_intro.txt \
	ladder/ladder_intro_de.txt \
	ladder/ladder_intro_es.txt \
	ladder/ladder_intro_fr.txt \
	ladder/ladder_intro_pl.txt \
	lathe/lathe-user.txt \
	lathe/lathe-user_de.txt \
	lathe/lathe-user_es.txt \
	lathe/lathe-user_fr.txt \
	lathe/lathe-user_pl.txt \
	motion/kinematics.txt \
	motion/kinematics_de.txt \
	motion/kinematics_es.txt \
	motion/kinematics_fr.txt \
	motion/kinematics_pl.txt \
	motion/pid_theory.txt \
	motion/pid_theory_de.txt \
	motion/pid_theory_es.txt \
	motion/pid_theory_fr.txt \
	motion/pid_theory_pl.txt \
	motion/tweaking_steppers.txt \
	motion/tweaking_steppers_de.txt \
	motion/tweaking_steppers_es.txt \
	motion/tweaking_steppers_fr.txt \
	motion/tweaking_steppers_pl.txt \
	quickstart/stepper_quickstart.txt \
	quickstart/stepper_quickstart_de.txt \
	quickstart/stepper_quickstart_es.txt \
	quickstart/stepper_quickstart_fr.txt \
	quickstart/stepper_quickstart_pl.txt \
	Master_Getting_Started.txt \
	Master_Getting_Started_de.txt \
	Master_Getting_Started_es.txt \
	Master_Getting_Started_fr.txt \
	Master_Getting_Started_pl.txt \
	Master_User.txt \
	Master_User_de.txt \
	Master_User_es.txt \
	Master_User_fr.txt \
	Master_User_pl.txt \
	Master_HAL.txt \
	Master_HAL_de.txt \
	Master_HAL_es.txt \
	Master_HAL_fr.txt \
	Master_HAL_pl.txt \
	Master_Integrator.txt \
	Master_Integrator_de.txt \
	Master_Integrator_es.txt \
	Master_Integrator_fr.txt \
	Master_Integrator_pl.txt \
	Master_Developer.txt \

DOC_SRCS_TXT := $(patsubst %.txt, %.txt, $(DOC_SRCS))

DOC_SRCS_DE := $(filter %_de.txt, $(DOC_SRCS_TXT))
DOC_SRCS_ES := $(filter %_es.txt, $(DOC_SRCS_TXT))
DOC_SRCS_FR := $(filter %_fr.txt, $(DOC_SRCS_TXT))
DOC_SRCS_PL := $(filter %_pl.txt, $(DOC_SRCS_TXT))
DOC_SRCS_EN := $(filter-out $(DOC_SRCS_DE), $(filter-out $(DOC_SRCS_ES), $(filter-out $(DOC_SRCS_FR), $(filter-out $(DOC_SRCS_PL), $(DOC_SRCS_TXT)) ) ) )

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_PL_SMALL := $(filter-out Master_%,$(DOC_SRCS_PL))
DOC_SRCS_EN_SMALL := $(filter-out Master_%,$(DOC_SRCS_EN))

DOC_SRCS_HTML := $(patsubst %.txt, %.html, $(filter-out Master_%, $(DOC_SRCS)))
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))

MAN_HTML_TARGETS := $(patsubst $(DOC_DIR)/man/%, $(DOC_DIR)/html/man/%.html, $(MAN_SRCS))
PDF_TARGETS := $(addprefix $(DOC_DIR)/, \
	LinuxCNC_Getting_Started.pdf \
	LinuxCNC_Getting_Started_de.pdf \
	LinuxCNC_Getting_Started_es.pdf \
	LinuxCNC_Getting_Started_fr.pdf \
	LinuxCNC_Getting_Started_pl.pdf \
	LinuxCNC_User_Manual.pdf \
	LinuxCNC_User_Manual_de.pdf \
	LinuxCNC_User_Manual_es.pdf \
	LinuxCNC_User_Manual_fr.pdf \
	LinuxCNC_User_Manual_pl.pdf \
	LinuxCNC_HAL_Manual.pdf \
	LinuxCNC_HAL_Manual_de.pdf \
	LinuxCNC_HAL_Manual_es.pdf \
	LinuxCNC_HAL_Manual_fr.pdf \
	LinuxCNC_HAL_Manual_pl.pdf \
	LinuxCNC_Integrator_Manual.pdf \
	LinuxCNC_Integrator_Manual_de.pdf \
	LinuxCNC_Integrator_Manual_es.pdf \
	LinuxCNC_Integrator_Manual_fr.pdf \
	LinuxCNC_Integrator_Manual_pl.pdf \
	LinuxCNC_Developer_Manual.pdf \
	LinuxCNC_Manual_Pages.pdf \
	)

# 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 \
	$(DOC_DIR)/html/index_de.html \
	$(DOC_DIR)/html/index_es.html \
	$(DOC_DIR)/html/index_fr.html \
	$(DOC_DIR)/html/index_pl.html \

A2X = a2x --xsltproc-opts "--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 -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 \
                          -P latex.encoding=utf8"

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

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: $(PDF_TARGETS)
htmldocs: $(HTML_TARGETS) $(MAN_HTML_TARGETS) checkref

checkref: checkref_en checkref_fr

checkref_en: $(DOC_DIR)/html/gcode.html $(DOC_DIR)/html/gcode/gcode.html $(DOC_DIR)/html/gcode/m-code.html $(DOC_DIR)/html/gcode/o-code.html $(DOC_DIR)/html/gcode/other-code.html $(DOC_DIR)/html/gcode/overview.html
	python $(DOC_SRCDIR)/checklinks.py $^

checkref_fr: $(DOC_DIR)/html/gcode_fr.html $(DOC_DIR)/html/gcode/gcode_fr.html $(DOC_DIR)/html/gcode/overview_fr.html $(DOC_DIR)/html/gcode/m-code_fr.html $(DOC_DIR)/html/gcode/o-code_fr.html $(DOC_DIR)/html/gcode/other-code_fr.html
	python $(DOC_SRCDIR)/checklinks.py $^

# These targets don't work yet since we don't have the associated translations
checkref_es: $(DOC_DIR)/html/gcode_es.html $(DOC_DIR)/html/gcode/gcode_es.html
	-python $(DOC_SRCDIR)/checklinks.py $^

checkref_de: $(DOC_DIR)/html/gcode_de.html $(DOC_DIR)/html/gcode/gcode_de.html
	-python $(DOC_SRCDIR)/checklinks.py $^

checkref_pl: $(DOC_DIR)/html/gcode_pl.html $(DOC_DIR)/html/gcode/gcode_pl.html
	-python $(DOC_SRCDIR)/checklinks.py $^

MAN_SRCS_NOSO := $(patsubst $(DOC_DIR)/man/%,%, \
			$(shell grep -L '^\.so ' $(sort $(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)/Master_Getting_Started_de.pdf
	@ln -f $< $@
$(DOC_DIR)/LinuxCNC_Getting_Started_es.pdf: $(DOC_SRCDIR)/Master_Getting_Started_es.pdf
	@ln -f $< $@
$(DOC_DIR)/LinuxCNC_Getting_Started_fr.pdf: $(DOC_SRCDIR)/Master_Getting_Started_fr.pdf
	@ln -f $< $@
$(DOC_DIR)/LinuxCNC_Getting_Started_pl.pdf: $(DOC_SRCDIR)/Master_Getting_Started_pl.pdf
	@ln -f $< $@
$(DOC_DIR)/LinuxCNC_User_Manual.pdf: $(DOC_SRCDIR)/Master_User.pdf
	@ln -f $< $@
$(DOC_DIR)/LinuxCNC_User_Manual_de.pdf: $(DOC_SRCDIR)/Master_User_de.pdf
	@ln -f $< $@
$(DOC_DIR)/LinuxCNC_User_Manual_es.pdf: $(DOC_SRCDIR)/Master_User_es.pdf
	@ln -f $< $@
$(DOC_DIR)/LinuxCNC_User_Manual_fr.pdf: $(DOC_SRCDIR)/Master_User_fr.pdf
	@ln -f $< $@
$(DOC_DIR)/LinuxCNC_User_Manual_pl.pdf: $(DOC_SRCDIR)/Master_User_pl.pdf
	@ln -f $< $@
$(DOC_DIR)/LinuxCNC_HAL_Manual.pdf: $(DOC_SRCDIR)/Master_HAL.pdf
	@ln -f $< $@
$(DOC_DIR)/LinuxCNC_HAL_Manual_de.pdf: $(DOC_SRCDIR)/Master_HAL_de.pdf
	@ln -f $< $@
$(DOC_DIR)/LinuxCNC_HAL_Manual_es.pdf: $(DOC_SRCDIR)/Master_HAL_es.pdf
	@ln -f $< $@
$(DOC_DIR)/LinuxCNC_HAL_Manual_fr.pdf: $(DOC_SRCDIR)/Master_HAL_fr.pdf
	@ln -f $< $@
$(DOC_DIR)/LinuxCNC_HAL_Manual_pl.pdf: $(DOC_SRCDIR)/Master_HAL_pl.pdf
	@ln -f $< $@
$(DOC_DIR)/LinuxCNC_Integrator_Manual.pdf: $(DOC_SRCDIR)/Master_Integrator.pdf
	@ln -f $< $@
$(DOC_DIR)/LinuxCNC_Integrator_Manual_de.pdf: $(DOC_SRCDIR)/Master_Integrator_de.pdf
	@ln -f $< $@
$(DOC_DIR)/LinuxCNC_Integrator_Manual_es.pdf: $(DOC_SRCDIR)/Master_Integrator_es.pdf
	@ln -f $< $@
$(DOC_DIR)/LinuxCNC_Integrator_Manual_fr.pdf: $(DOC_SRCDIR)/Master_Integrator_fr.pdf
	@ln -f $< $@
$(DOC_DIR)/LinuxCNC_Integrator_Manual_pl.pdf: $(DOC_SRCDIR)/Master_Integrator_pl.pdf
	@ln -f $< $@
$(DOC_DIR)/LinuxCNC_Developer_Manual.pdf: $(DOC_SRCDIR)/Master_Developer.pdf
	@ln -f $< $@

$(DOC_DIR)/html/man/%.html: $(DOC_DIR)/man/%
	@echo Formatting $(notdir $<) as HTML
	@mkdir -p $(dir $@)
	@(cd $(DOC_DIR)/man; groff -Thtml -man $(patsubst $(DOC_DIR)/man/%,%,$<)) > $@

ST := style=\"-moz-column-width: 25ex; -moz-column-gap: 4ex; list-style-type: none\"

objects/index.incl: objects/var-MAN_HTML_TARGETS $(DOC_SRCDIR)/Submakefile
	echo "<A NAME=\"man1\"><LI>Commands and userspace components<UL $(ST)>" > $@.tmp
	for i in $(filter %.1.html, $(MAN_HTML_TARGETS)); do j=$$(basename $$i .html); echo "<LI><A HREF=\"$${i#$(DOC_DIR)/html/}\">$$j</A>"; done >> $@.tmp
	echo "<A NAME=\"man9\"></UL><LI>Realtime components and kernel modules<UL $(ST)>" >> $@.tmp
	for i in $(filter %.9.html, $(MAN_HTML_TARGETS)); do j=$$(basename $$i .html); echo "<LI><A HREF=\"$${i#$(DOC_DIR)/html/}\">$$j</A>"; done >> $@.tmp
	echo "<A NAME=\"man3hal\"></UL><LI>HAL API calls<UL $(ST)>" >> $@.tmp
	for i in $(filter %.3hal.html, $(MAN_HTML_TARGETS)); do j=$$(basename $$i .html); echo "<LI><A HREF=\"$${i#$(DOC_DIR)/html/}\">$$j</A>"; done >> $@.tmp
	echo "<A NAME=\"man3rtapi\"></UL><LI>RTAPI calls<UL $(ST)>" >> $@.tmp
	for i in $(filter %.3rtapi.html, $(MAN_HTML_TARGETS)); do j=$$(basename $$i .html); echo "<LI><A HREF=\"$${i#$(DOC_DIR)/html/}\">$$j</A>"; done >> $@.tmp
	echo "</UL>" >> $@.tmp
	mv -f $@.tmp $@

$(DOC_DIR)/html/index_%.html: $(DOC_SRCDIR)/index_%.tmpl ../VERSION $(DOC_SRCDIR)/index.foot
	cat $(filter-out ../VERSION, $^) | \
	sed "s/@VERSION@/`cat ../VERSION`/" > $@	

$(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`/" > $@	

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

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


DOC_TARGETS_XML_DE := $(filter %_de.xml, $(DOC_TARGETS_XML))
DOC_TARGETS_XML_ES := $(filter %_es.xml, $(DOC_TARGETS_XML))
DOC_TARGETS_XML_FR := $(filter %_fr.xml, $(DOC_TARGETS_XML))
DOC_TARGETS_XML_PL := $(filter %_pl.xml, $(DOC_TARGETS_XML))
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_PL), $(DOC_TARGETS_XML)) ) ) )
DOC_TARGETS_HTML_DE := $(filter %_de.html, $(DOC_TARGETS_HTML))
DOC_TARGETS_HTML_ES := $(filter %_es.html, $(DOC_TARGETS_HTML))
DOC_TARGETS_HTML_FR := $(filter %_fr.html, $(DOC_TARGETS_HTML))
DOC_TARGETS_HTML_PL := $(filter %_pl.html, $(DOC_TARGETS_HTML))
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_PL), $(DOC_TARGETS_HTML)) ) ) )

objects/xref_de.xml: $(DOC_TARGETS_XML_DE) $(DOC_SRCDIR)/xref.py
	(cd objects; ../../docs/src/xref.py $(filter %.xml, $(patsubst objects/%,%,$^))) > $@
objects/xref_es.xml: $(DOC_TARGETS_XML_ES) $(DOC_SRCDIR)/xref.py
	(cd objects; ../../docs/src/xref.py $(filter %.xml, $(patsubst objects/%,%,$^))) > $@
objects/xref_fr.xml: $(DOC_TARGETS_XML_FR) $(DOC_SRCDIR)/xref.py
	(cd objects; ../../docs/src/xref.py $(filter %.xml, $(patsubst objects/%,%,$^))) > $@
objects/xref_pl.xml: $(DOC_TARGETS_XML_PL) $(DOC_SRCDIR)/xref.py
	(cd objects; ../../docs/src/xref.py $(filter %.xml, $(patsubst objects/%,%,$^))) > $@
objects/xref.xml: $(DOC_TARGETS_XML_EN) $(DOC_SRCDIR)/xref.py
	(cd objects; ../../docs/src/xref.py $(filter %.xml, $(patsubst objects/%,%,$^))) > $@

objects/%.links-stamp: $(DOC_SRCDIR)/%.txt
	@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 $@; 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 %.txt,objects/%.links,$(DOC_SRCS_EN_SMALL))
	$(PYTHON) $(DOC_SRCDIR)/links_db_gen.py objects/ $^ > $@

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

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

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

objects/xref_pl.links: $(patsubst %.txt,objects/%.links,$(DOC_SRCS_PL_SMALL))
	$(PYTHON) $(DOC_SRCDIR)/links_db_gen.py objects/ $^ > $@

$(DOC_TARGETS_HTML): $(DOC_DIR)/html/%.html: $(DOC_SRCDIR)/%.html
	@d=`dirname $*`; \
	for f in $(shell xsltproc --novalid --nonet $(DOC_SRCDIR)/html-images.xslt $<); do\
		mkdir -p $(DOC_DIR)/html/$$d/`dirname $$f`; \
		cp -f $(DOC_SRCDIR)/$$d/$$f $(DOC_DIR)/html/$$d/$$f; \
	done
	mkdir -p $(shell dirname $@)
	@cp $< $@
	mkdir -p objects/image-cache
	HTML_LATEX_CACHE=objects/image-cache $(DOC_SRCDIR)/html-latex-images $@ || \
				(X=$$?; rm $@; exit $$X)

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

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

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

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

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

default: docs
$(DOC_DIR)/html/xref_fr.html: objects/xref_fr.xml $(DOC_SRCDIR)/xref.xsl $(DOC_SRCDIR)/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/xref_es.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/xref_de.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 german --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)
install-kernel-indep: install-doc
else
docs:
	$(error Cannot build documents, missing AsciiDoc or some other required program, or explicitly disabled in configure)

endif

docclean:
	-rm -f $(DOC_DIR)/LinuxCNC_Getting_Started.pdf
	-rm -f $(DOC_DIR)/LinuxCNC_Getting_Started_de.pdf
	-rm -f $(DOC_DIR)/LinuxCNC_Getting_Started_es.pdf
	-rm -f $(DOC_DIR)/LinuxCNC_Getting_Started_fr.pdf
	-rm -f $(DOC_DIR)/LinuxCNC_Getting_Started_pl.pdf
	-rm -f $(DOC_DIR)/LinuxCNC_User_Manual.pdf
	-rm -f $(DOC_DIR)/LinuxCNC_User_Manual_de.pdf
	-rm -f $(DOC_DIR)/LinuxCNC_User_Manual_es.pdf
	-rm -f $(DOC_DIR)/LinuxCNC_User_Manual_fr.pdf
	-rm -f $(DOC_DIR)/LinuxCNC_User_Manual_pl.pdf
	-rm -f $(DOC_DIR)/LinuxCNC_HAL_Manual.pdf
	-rm -f $(DOC_DIR)/LinuxCNC_HAL_Manual_de.pdf
	-rm -f $(DOC_DIR)/LinuxCNC_HAL_Manual_es.pdf
	-rm -f $(DOC_DIR)/LinuxCNC_HAL_Manual_fr.pdf
	-rm -f $(DOC_DIR)/LinuxCNC_HAL_Manual_pl.pdf
	-rm -f $(DOC_DIR)/LinuxCNC_Integrator_Manual.pdf
	-rm -f $(DOC_DIR)/LinuxCNC_Integrator_Manual_de.pdf
	-rm -f $(DOC_DIR)/LinuxCNC_Integrator_Manual_es.pdf
	-rm -f $(DOC_DIR)/LinuxCNC_Integrator_Manual_fr.pdf
	-rm -f $(DOC_DIR)/LinuxCNC_Integrator_Manual_pl.pdf
	-rm -f $(DOC_DIR)/LinuxCNC_Developer_Manual.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_DIR)/html/*.png
	-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/*.*
	-rm -f $(DOC_DIR)/html/gcode/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/*.*
	-rm -f $(DOC_DIR)/html/quickstart/*.*
	-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

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:; $(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)

