# Minimal makefile for Sphinx documentation
#

# You can set these variables from the command line.
SPHINXOPTS    =
SPHINXBUILD   = sphinx-build
SOURCEDIR     = .
BUILDDIR      = _build
MODULES       = $(sort \
    socket_protocol\
    state_machine\
    stringtools\
    stringtools.csp\
    stringtools.stp\
    task\
    tcp_socket\
)

# Put it first so that "make" without argument is like "make help".

.PHONY: all help Makefile

all: index.rst $(MODULES) html

help:
	@$(SPHINXBUILD) -M help "$(SOURCEDIR)" "$(BUILDDIR)" $(SPHINXOPTS) $(O)

# Catch-all target: route all unknown targets to Sphinx using the new
# "make mode" option.  $(O) is meant as a shortcut for $(SPHINXOPTS).
html: Makefile
	@$(SPHINXBUILD) -M $@ "$(SOURCEDIR)" "$(BUILDDIR)" $(SPHINXOPTS) $(O)

clean: Makefile
	rm -f *.rst
	@for module in $(MODULES) ; do \
		if [ -d "$$module/_examples_" ]; then make --no-print-directory -C $$module/_examples_ clean; fi \
	done
	@$(SPHINXBUILD) -M $@ "$(SOURCEDIR)" "$(BUILDDIR)" $(SPHINXOPTS) $(O)

index.rst: Makefile
	@echo Generating $@
	@echo "Indices and tables\n==================\n\n* :ref:\`genindex\`\n* :ref:\`modindex\`\n* :ref:\`search\`\n" > $@
	@echo "Modules\n=======\n\n.. toctree::\n   :maxdepth: 2\n" >> $@

%: Makefile
	@echo "Preparing $@"
	@echo ".. automodule:: $@\n   :members:" > $@.rst
	@if [ "$(findstring .,$@)" != "." ]; then \
		git -C $@ checkout master; \
		git -C $@ pull; \
		make -C $@/_examples_ all; \
	fi
	@echo "   $@.rst" >> index.rst; \

update:
	git pull
	git submodule init
	git submodule foreach git checkout master
	git submodule foreach git pull

copy:
	rsync -av --delete _build/html/ /data/webroot/pylib_docs/
	chown www-data.www-data -R /data/webroot/pylib_docs/