# This Makefile creates PasDoc generated documentation for PasDoc itself.
# Documentation is created in subdirectories named after pasdoc's --format
# names (html/, latex/ etc.)
#
# Targets:
#
#   html latex pdf dvi ps html-latex htmlhelp rtf htmlhelp-chmcmd
#     Make documentation in requested format.
#
#   clean
#     Delete all generated documentation.
#
#   public
#     Generate documentation in all formats that we want to show
#     on https://pasdoc.github.io/autodoc/ .
#
#   upload
#     Copy documentation generated by `public'
#     to ../../../pasdoc.github.io/docs/ .
#     This assumes you have cloned
#     https://github.com/pasdoc/pasdoc.github.io alongside pasdoc repo.
#     After pushing this repo it will be visible on
#     https://pasdoc.github.io/autodoc/ .
#
# Some hints:
#   To completely remake all documentation for
#   https://pasdoc.github.io/autodoc/
#   just call `make clean public upload'
#   then in ../../../pasdoc.github.io/ do GIT commit + push with new docs.

# Making documentation ----------------------------------------

# Name of pasdoc binary. Remember that you can override it at
# make's command-line, like `make PASDOC=my_pasdoc'
PASDOC=pasdoc

# These options will be passed to every pasdoc invocation,
# i.e. for every output format.
PASDOC_OPTIONS=--include ../component \
  ../component/*.pas \
  ../component/tipue/*.pas \
  ../console/*.pas \
  -DFPC \
  -DVER3 \
  -DPASDOC \
  --title "PasDoc's autodoc" \
  --write-uses-list  \
  --auto-abstract \
  --introduction=introduction.txt \
  --show-source-position \
  --source-url-pattern "https://github.com/pasdoc/pasdoc/blob/master/{FILE}\#L{LINE}" \
  --source-root ../../ \
	--visible-members='public,published,automated,protected?' \
	--inherited-members=default-hide

# These options will be passed only when making docs in format that
# supports including graphviz graphs.
PASDOC_GRAPHVIZ_OPTIONS=--graphviz-classes --graphviz-uses \
  --link-gv-classes jpg --link-gv-uses jpg

# How to call hhc (html help compiler)?
# Default value means that it must be available on path.
# As usual, remember you can override it at make's command-line.
HHC=hhc

# Run after making autodoc: processes .dot files generated by pasdoc to .jpg files.
.PHONY: internal-post-graphviz
internal-post-graphviz:
	cd $(GRAPHVIZ_DIR) && dot -Grankdir=LR -Tjpg -oGVClasses.jpg GVClasses.dot
	cd $(GRAPHVIZ_DIR) && dot -Grankdir=LR -Tjpg -oGVUses.jpg GVUses.dot

# Generate HTML docs.
.PHONY: html
html:
	mkdir -p html/
	$(PASDOC) $(PASDOC_OPTIONS) $(PASDOC_GRAPHVIZ_OPTIONS) \
	  --output=html/ --use-tipue-search
	$(MAKE) internal-post-graphviz GRAPHVIZ_DIR=html/

# Common part of htmlhelp and htmlhelp-chmcmd targets.
.PHONY: internal-htmlhelp-common
internal-htmlhelp-common:
	mkdir -p htmlhelp/
	$(PASDOC) $(PASDOC_OPTIONS) $(PASDOC_GRAPHVIZ_OPTIONS) \
	  --format=htmlhelp --output=htmlhelp/
	$(MAKE) internal-post-graphviz GRAPHVIZ_DIR=htmlhelp/

# Make htmlhelp docs, using hhc (MS HTML Help Workshop's compiler).
.PHONY: htmlhelp
htmlhelp:
	$(MAKE) internal-htmlhelp-common
# Ignore exit code from hhc (it's always 1 ???)
	-cd htmlhelp/ && $(HHC) docs.hhp
# Since we had to ignore hhc's exit code, let's check if .chm file was generated
	if [ -f htmlhelp/docs.chm ]; then \
	  echo "CHM file generated successfully"; \
	else \
	  echo "Error: CHM file was not generated"; exit 1; \
	fi

# Make htmlhelp docs, using chmcmd (FPC cross-platform tool to make chm files).
.PHONY: htmlhelp-chmcmd
htmlhelp-chmcmd:
	$(MAKE) internal-htmlhelp-common
	cd htmlhelp && chmcmd --no-html-scan docs.hhp

# Just shortcuts to make documentation in various formats.
.PHONY: latex pdf dvi ps html-latex rtf
latex: latex/docs.tex
pdf: latex/docs.pdf
dvi: latex/docs.dvi
ps: latex/docs.ps
html-latex: latex/docs.html
rtf: latex2rtf/docs.rtf

latex/docs.tex:
	mkdir -p latex/
	$(PASDOC) $(PASDOC_OPTIONS) \
	  --format=latex --output=latex/

# Make PDF version.
# Note: We must do `cd latex' before calling pdflatex, we can't call
# `pdflatex latex/docs.tex' because then many output files of pdflatex
# would be placed in current dir.
latex/docs.pdf: latex/docs.tex
	cd latex && pdflatex --file-line-error-style -interaction=nonstopmode docs.tex
	cd latex && pdflatex --file-line-error-style -interaction=nonstopmode docs.tex

latex/docs.dvi: latex/docs.tex
	cd latex && latex --file-line-error-style -interaction=nonstopmode docs.tex
	cd latex && latex --file-line-error-style -interaction=nonstopmode docs.tex

latex/docs.ps: latex/docs.dvi
	cd latex && dvips docs.dvi -o docs.ps

# Make HTML version from LaTeX (PasDoc's direct HTML output is much better;
# but using LaTeX->HTML remains possible).
#
# When working htlatex overrides latex/docs.dvi with a version
# that is not really a useful to browse (it's only for htlatex internals).
# That's why I `rm' it afterwards.
#
# As far as I understand how htlatex works, it's not needed to run
# htlatex twice (like it's needed with pdflatex and latex).
latex/docs.html: latex/docs.tex
	cd latex && htlatex docs.tex
	rm latex/docs.dvi

latex2rtf/docs.tex:
	mkdir -p latex2rtf/
	$(PASDOC) $(PASDOC_OPTIONS) \
	  --format=latex2rtf --output=latex2rtf/

# Make RTF version from LaTeX.
#
# I must have latex/docs.tex as a prerequisite, to run latex over it
# to generate docs.aux file. Then I can use latex/docs.aux file with latex2rtf
# so that latex2rtf can make appropriate links.
latex2rtf/docs.rtf: latex2rtf/docs.tex latex/docs.tex
	cd latex/ && latex --file-line-error-style -interaction=nonstopmode docs.tex
	cd latex2rtf/ && latex2rtf -a ../latex/docs.aux docs.tex

.PHONY: clean
clean:
	rm -Rf html/ latex/ latex2rtf/ htmlhelp/ html_by_pasdoc_gui/

# ------------------------------------------------------------
# Making and uploading autodoc for https://pasdoc.github.io/autodoc/

.PHONY:	public
public: html pdf latex htmlhelp-chmcmd

OUTPUT_DIR:=../../../pasdoc.github.io/docs/
AUTODOC_DIR:=$(OUTPUT_DIR)autodoc/

.PHONY: upload
upload:
	rm -Rf $(AUTODOC_DIR)
	mkdir -p $(AUTODOC_DIR) $(AUTODOC_DIR)latex/ $(AUTODOC_DIR)htmlhelp/
	cp -Rf index.html html/ $(AUTODOC_DIR)
	cp -Rf latex/docs.pdf latex/docs.tex $(AUTODOC_DIR)latex/
	cp -Rf htmlhelp/docs.chm $(AUTODOC_DIR)htmlhelp/

# eof ------------------------------------------------------------
