# Minimal makefile for Sphinx documentation
#

# You can set these variables from the command line.
SPHINXOPTS    =
SPHINXBUILD   = sphinx-build
SOURCEDIR     = .
BUILDDIR      = _build

VERSION_FILES = conf.py installation/basic.rst
VERSION      := $(shell perl -e 'require "../../lib/perl/OVH/Bastion.pm"; print $$OVH::Bastion::VERSION')

all: autogenerated default

autogenerated: plugins config_doc_ref

default: Makefile
	@for i in $(VERSION_FILES); do cp $$i $$i.bak; done
	@sed -i -e "s/__VERSION__/$(VERSION)/g" $(VERSION_FILES)
	@sed -i -e "s/__YEAR__/$(shell date +%Y)/g" $(VERSION_FILES)
	@$(SPHINXBUILD) -M html "$(SOURCEDIR)" "$(BUILDDIR)" $(SPHINXOPTS) $(O)
	@for i in $(VERSION_FILES); do test -e $$i.bak && mv $$i.bak $$i; done
	@rsync -a --delete _build/html/ ../../docs/
	@echo "HTML documentation copied to ../../docs/"

# Put it first so that "make" without argument is like "make help".
help:
	@$(SPHINXBUILD) -M help "$(SOURCEDIR)" "$(BUILDDIR)" $(SPHINXOPTS) $(O)

plugins:
	@bash build-plugins-help.sh

config_doc_ref:
	env GLOBAL_REFS=1 perl build-config-doc-ref.pl bastion.conf                       < ../../etc/bastion/bastion.conf.dist                       > administration/configuration/bastion_conf.rst
	                  perl build-config-doc-ref.pl osh-backup-acl-keys.conf           < ../../etc/bastion/osh-backup-acl-keys.conf.dist           > administration/configuration/osh-backup-acl-keys_conf.rst
	                  perl build-config-doc-ref.pl osh-cleanup-guest-key-access.conf  < ../../etc/bastion/osh-cleanup-guest-key-access.conf.dist  > administration/configuration/osh-cleanup-guest-key-access_conf.rst
	                  perl build-config-doc-ref.pl osh-encrypt-rsync.conf             < ../../etc/bastion/osh-encrypt-rsync.conf.dist             > administration/configuration/osh-encrypt-rsync_conf.rst
	                  perl build-config-doc-ref.pl osh-http-proxy.conf                < ../../etc/bastion/osh-http-proxy.conf.dist                > administration/configuration/osh-http-proxy_conf.rst
	                  perl build-config-doc-ref.pl osh-lingering-sessions-reaper.conf < ../../etc/bastion/osh-lingering-sessions-reaper.conf.dist > administration/configuration/osh-lingering-sessions-reaper_conf.rst
	                  perl build-config-doc-ref.pl osh-orphaned-homedir.conf          < ../../etc/bastion/osh-orphaned-homedir.conf.dist          > administration/configuration/osh-orphaned-homedir_conf.rst
	                  perl build-config-doc-ref.pl osh-piv-grace-reaper.conf          < ../../etc/bastion/osh-piv-grace-reaper.conf.dist          > administration/configuration/osh-piv-grace-reaper_conf.rst
	                  perl build-config-doc-ref.pl osh-remove-empty-folders.conf      < ../../etc/bastion/osh-remove-empty-folders.conf.dist      > administration/configuration/osh-remove-empty-folders_conf.rst
	                  perl build-config-doc-ref.pl osh-sync-watcher.sh                < ../../etc/bastion/osh-sync-watcher.sh.dist                > administration/configuration/osh-sync-watcher_sh.rst

reflist:
	@python3 -m sphinx.ext.intersphinx _build/html/objects.inv

.PHONY: help Makefile plugins all autogenerated default config_doc_ref reflist

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