# Copyright (C) Internet Systems Consortium, Inc. ("ISC")
#
# SPDX-License-Identifier: MPL-2.0
#
# This Source Code Form is subject to the terms of the Mozilla Public
# License, v. 2.0.  If a copy of the MPL was not distributed with this
# file, you can obtain one at https://mozilla.org/MPL/2.0/.
#
# See the COPYRIGHT file distributed with this work for additional
# information regarding copyright ownership.

srcdir =	@srcdir@
VPATH =		@srcdir@
top_srcdir =	@top_srcdir@
abs_srcdir =	@abs_srcdir@
builddir =	@builddir@
top_builddir =	@top_builddir@

@BIND9_MAKE_RULES@

man1_MANS =			\
	arpaname.1		\
	delv.1			\
	dig.1			\
	host.1			\
	mdig.1			\
	named-rrchecker.1	\
	nslookup.1		\
	nsupdate.1

man5_MANS =			\
	named.conf.5		\
	rndc.conf.5

man8_MANS =			\
	ddns-confgen.8		\
	dnssec-cds.8		\
	dnssec-checkds.8	\
	dnssec-coverage.8	\
	dnssec-dsfromkey.8	\
	dnssec-importkey.8	\
	dnssec-keyfromlabel.8	\
	dnssec-keygen.8		\
	dnssec-keymgr.8		\
	dnssec-revoke.8		\
	dnssec-settime.8	\
	dnssec-signzone.8	\
	dnssec-verify.8		\
	filter-aaaa.8		\
	named-checkconf.8	\
	named-checkzone.8	\
	named-compilezone.8	\
	named-journalprint.8	\
	named.8			\
	nsec3hash.8		\
	rndc-confgen.8		\
	rndc.8			\
	tsig-keygen.8

MANPAGES_RST =			\
	arpaname.rst		\
	ddns-confgen.rst	\
	delv.rst		\
	dig.rst			\
	dnssec-cds.rst		\
	dnssec-checkds.rst	\
	dnssec-coverage.rst	\
	dnssec-dsfromkey.rst	\
	dnssec-importkey.rst	\
	dnssec-keyfromlabel.rst	\
	dnssec-keygen.rst	\
	dnssec-keymgr.rst	\
	dnssec-revoke.rst	\
	dnssec-settime.rst	\
	dnssec-signzone.rst	\
	dnssec-verify.rst	\
	dnstap-read.rst		\
	filter-aaaa.rst		\
	host.rst		\
	mdig.rst		\
	named-checkconf.rst	\
	named-checkzone.rst	\
	named-compilezone.rst	\
	named-journalprint.rst	\
	named-nzd2nzf.rst	\
	named-rrchecker.rst	\
	named.conf.rst		\
	named.rst		\
	nsec3hash.rst		\
	nslookup.rst		\
	nsupdate.rst		\
	rndc-confgen.rst	\
	rndc.conf.rst		\
	rndc.rst		\
	tsig-keygen.rst		\
	pkcs11-destroy.rst	\
	pkcs11-keygen.rst	\
	pkcs11-list.rst		\
	pkcs11-tokens.rst

MANPAGES_IN = \
	arpaname.1in		\
	ddns-confgen.8in	\
	delv.1in		\
	dig.1in			\
	dnssec-cds.8in		\
	dnssec-checkds.8in	\
	dnssec-coverage.8in	\
	dnssec-dsfromkey.8in	\
	dnssec-importkey.8in	\
	dnssec-keyfromlabel.8in	\
	dnssec-keygen.8in	\
	dnssec-keymgr.8in	\
	dnssec-revoke.8in	\
	dnssec-settime.8in	\
	dnssec-signzone.8in	\
	dnssec-verify.8in	\
	dnstap-read.1in		\
	filter-aaaa.8in		\
	host.1in		\
	mdig.1in		\
	named-checkconf.8in	\
	named-checkzone.8in	\
	named-compilezone.8in	\
	named-journalprint.8in	\
	named-nzd2nzf.8in	\
	named-rrchecker.1in	\
	named.conf.5in		\
	named.8in		\
	nsec3hash.8in		\
	nslookup.1in		\
	nsupdate.1in		\
	rndc-confgen.8in	\
	rndc.conf.5in		\
	rndc.8in		\
	tsig-keygen.8in		\
	pkcs11-destroy.8in	\
	pkcs11-keygen.8in	\
	pkcs11-list.8in		\
	pkcs11-tokens.8in

dnstap_man1_MANS =		\
	dnstap-read.1

nzd_man8_MANS =			\
	named-nzd2nzf.8

pkcs11_man8_MANS =		\
	pkcs11-destroy.8	\
	pkcs11-keygen.8		\
	pkcs11-list.8		\
	pkcs11-tokens.8

BIND9_VERSION=@BIND9_VERSION@
RELEASE_DATE=@RELEASE_DATE@
BIND9_VERSIONSTRING=@BIND9_VERSIONSTRING@

# You can set these variables from the command line.
SPHINXBUILD = @SPHINX_BUILD@
SPHINXBUILDDIR = ${builddir}/_build
SPHINX_W = -W

common_SPHINXOPTS =		\
	$(SPHINX_W)		\
	-a			\
	-v			\
	-c "${abs_srcdir}"

ALLSPHINXOPTS =					\
	$(common_SPHINXOPTS)			\
	-D version="${BIND9_VERSION}"		\
	-D today="${RELEASE_DATE}"		\
	-D release="${BIND9_VERSIONSTRING}"	\
	$(SPHINXOPTS)				\
	${srcdir}

man_SPHINXOPTS =				\
	$(common_SPHINXOPTS)			\
	-D version="@""BIND9_VERSION""@"	\
	-D today="@""RELEASE_DATE""@"		\
	-D release="@""BIND9_VERSIONSTRING""@"	\
	$(SPHINXOPTS)				\
	${srcdir}

# Put it first so that "make" without argument just builds manpages
all: man
	@:

man:: ootsetup $(man1_MANS) $(man5_MANS) $(man8_MANS) @DNSTAP_MANS@ @NZD_MANS@ @PKCS11_MANS@

doc:: @HTMLTARGET@ @PDFTARGET@

html dirhtml:
	$(SPHINXBUILD) -b $@ -d "$(SPHINXBUILDDIR)"/.doctrees/$@ $(ALLSPHINXOPTS) "$(SPHINXBUILDDIR)"/$@

# copy in out-of-tree files in case sphinx-build isn't available
.NOTPARALLEL:
ootsetup: $(MANPAGES_IN)
	for man in $(MANPAGES_IN); do \
	  [ -e "$$man" ] || cp -f ${srcdir}/"$$man" .; \
	done

$(MANPAGES_IN): $(MANPAGES_RST)
	$(SPHINXBUILD) -b man -d "$(SPHINXBUILDDIR)"/.doctrees/$@ $(man_SPHINXOPTS) "$(SPHINXBUILDDIR)"/man
	-for man in $(MANPAGES_IN); do \
	  [ -e "$(SPHINXBUILDDIR)"/man/"$$(basename $$man in)" ] && \
	    cp -f "$(SPHINXBUILDDIR)"/man/"$$(basename $$man in)" "$$man"; \
	done

man_SUBST = sed							\
	-e 's,[@]BIND9_VERSION[@],$(BIND9_VERSION),'			\
	-e 's,[@]RELEASE_DATE[@],$(RELEASE_DATE),'			\
	-e 's,[@]BIND9_VERSION_STRING[@],$(BIND9_VERSION_STRING),'	\
	-e 's,[@]sysconfdir[@],$(sysconfdir),'				\
	-e 's,[@]plugindir[@],$(plugindir),'

$(man1_MANS): @MANSRCS@
	for m in $(man1_MANS); do \
		$(man_SUBST) $${m}in > $$m; \
	done

$(man5_MANS): @MANSRCS@
	for m in $(man5_MANS); do \
		$(man_SUBST) $${m}in > $$m; \
	done

$(man8_MANS): @MANSRCS@
	for m in $(man8_MANS); do \
		$(man_SUBST) $${m}in > $$m; \
	done

$(dnstap_man1_MANS): @MANSRCS@
	for m in $(dnstap_man1_MANS); do \
		$(man_SUBST) $${m}in > $$m; \
	done

$(nzd_man8_MANS): @MANSRCS@
	for m in $(nzd_man8_MANS); do \
		$(man_SUBST) $${m}in > $$m; \
	done

$(pkcs11_man8_MANS): @MANSRCS@
	for m in $(pkcs11_man8_MANS); do \
		$(man_SUBST) $${m}in > $$m; \
	done

.PHONY: help Makefile doc pdf man

installdirs:
	$(SHELL) ${top_srcdir}/mkinstalldirs ${DESTDIR}${mandir}/man1
	$(SHELL) ${top_srcdir}/mkinstalldirs ${DESTDIR}${mandir}/man5
	$(SHELL) ${top_srcdir}/mkinstalldirs ${DESTDIR}${mandir}/man8

install:: installdirs
	for m in $(man1_MANS); do ${INSTALL_DATA} $$m ${DESTDIR}${mandir}/man1/; done
	for m in $(man5_MANS); do ${INSTALL_DATA} $$m ${DESTDIR}${mandir}/man5/; done
	for m in $(man8_MANS); do ${INSTALL_DATA} $$m ${DESTDIR}${mandir}/man8/; done
	for m in @DNSTAP_MANS@; do ${INSTALL_DATA} $$m ${DESTDIR}${mandir}/man1/; done
	for m in @NZD_MANS@; do ${INSTALL_DATA} $$m ${DESTDIR}${mandir}/man8/; done
	for m in @PKCS11_MANS@; do ${INSTALL_DATA} $$m ${DESTDIR}${mandir}/man8/; done

uninstall::
	for m in $(man1_MANS); do rm -f ${DESTDIR}${mandir}/man1/$$m; done
	for m in $(man5_MANS); do rm -f ${DESTDIR}${mandir}/man5/$$m; done
	for m in $(man8_MANS); do rm -f ${DESTDIR}${mandir}/man8/$$m; done
	for m in @DNSTAP_MANS@; do rm -f ${DESTDIR}${mandir}/man1/$$m; done
	for m in @NZD_MANS@; do rm -f ${DESTDIR}${mandir}/man8/$$m; done
	for m in @PKCS11_MANS@; do rm -f ${DESTDIR}${mandir}/man8/$$m; done

docclean manclean maintainer-clean::
	rm -f $(MANPAGES_IN)

clean::
	-rm -rf $(SPHINXBUILDDIR)
	-rm -f $(man1_MANS) $(man5_MANS) $(man8_MANS) @DNSTAP_MANS@ @NZD_MANS@ @PKCS11_MANS@
