# Copyright (C) 2009 Stelios Bounanos, M0GLD (m0gld AT enotty DOT net)
# License: GPLv3+: GNU GPL version 3 or later.

ASCIIDOC_CONF = asciidoc.conf

ASCIIDOC_SRC = dl-fldigi.1.txt guide.txt
ASCIIDOC_MAN_DIST = dl-fldigi.1
ASCIIDOC_MAN_INST =
ASCIIDOC_HTML =

if WANT_FLDIGI
    ASCIIDOC_MAN_INST += dl-fldigi.1
    ASCIIDOC_HTML += guide.html
endif

if WANT_FLARQ
    ASCIIDOC_MAN_INST += flarq.1
endif

EXTRA_DIST = $(ASCIIDOC_CONF) $(ASCIIDOC_SRC) $(ASCIIDOC_MAN_DIST)

man_MANS = $(ASCIIDOC_MAN_INST)
MOSTLYCLEANFILES = *.xml
DISTCLEANFILES = $(ASCIIDOC_HTML)
MAINTAINERCLEANFILES = $(ASCIIDOC_MAN_INST)

A2X_ARGS = --format=manpage
if HAVE_ASCIIDOC_NO_XMLLINT
    A2X_ARGS += --no-xmllint
endif

.SECONDEXPANSION:

# Define the custom silent rule function
@SILENT_CMDS@

%.1: $$@.txt $(ASCIIDOC_CONF)
if HAVE_ASCIIDOC
	$(call silent,ADOC  ,$@)$(A2X) $(A2X_ARGS) $<
else
	$(call silent,ADOC  ,$@)echo '.TH "NOT-BUILT" "1" "" "" ""' > $@
endif
# Without .SECONDEXPANSION we would use something like this:
#$(ASCIIDOC_MAN_INST): asciidoc.conf
#	a2x $(A2X_ARGS) $@.txt
#fldigi.1: fldigi.1.txt

# The rest of the Makefile is enabled only if we have asciidoc
if HAVE_ASCIIDOC
# Try to discover the correct iconsdir.  Use the first one that
# contains an entry matching `tip.*'.
ASCIIDOC_ICONS_DIRS := /etc/asciidoc/icons /etc/asciidoc/images /etc/asciidoc/images/icons /etc/asciidoc/images/../icons \
                       /usr/share/asciidoc/icons /usr/share/asciidoc/images /usr/share/asciidoc/images/icons
ASCIIDOC_ICONS_DIR ?= $(dir $(firstword $(strip $(foreach f,$(addsuffix /tip*,$(ASCIIDOC_ICONS_DIRS)),$(wildcard $(f))))))
# Additional attributes are defined in guide.conf
ASCIIDOC_ARGS = --unsafe -a toc -a data-uri -a badges -a icons -a iconsdir=$(ASCIIDOC_ICONS_DIR)

%.html: $$*.txt $(ASCIIDOC_CONF)
	@if ! test -d $(ASCIIDOC_ICONS_DIR); then \
	    echo "  *** The default asciidoc icons directory, \"$(ASCIIDOC_ICONS_DIR)\", does not exist." >&2; \
	    echo "  *** Please rerun make with the correct ASCIIDOC_ICONS_DIR, e.g.:" >&2; \
	    echo "  ***   make ASCIIDOC_ICONS_DIR=/path/to/icons" >&2; \
	    false; \
	fi
	$(call silent,ADOC  ,$@)$(ASCIIDOC) $(ASCIIDOC_ARGS) --out-file $@ $<

all-local: $(ASCIIDOC_MAN_DIST) $(ASCIIDOC_HTML)

endif
