#!/usr/bin/make -f

# generate documentation unless nodoc requested
ifeq (,$(filter nodoc,$(DEB_BUILD_OPTIONS)))
DOCS_stem = README $(filter-out documentation/docker,$(basename $(wildcard documentation/*.md)))
DOCS = $(addsuffix .html,$(DOCS_stem)) $(addsuffix .txt,$(DOCS_stem))
MANPAGES = debian/$(cmd).1
endif

PROLOG_ABI = $(shell swipl --abi_version | sed 's/^swipl-abi-//')

cmd = eye

# generate manpage with help2man from --help option of executable
_mkman = help2man $(if $3,--name "$(strip $3)") --no-info --no-discard-stderr --output $2 $1 \
 || { $1 --help; false; }

%:
	dh $@

%.html: %.md
	cmark-gfm $< > $@

%.txt: %.md
	cmark-gfm --to plaintext $< > $@

override_dh_auto_build:
	swipl -o eye --emulator=/usr/bin/swipl -g main -c eye.pl -- --image $(cmd)

execute_after_dh_auto_build: $(DOCS) $(MANPAGES)

override_dh_installdocs:
	dh_installdocs --all -- $(DOCS)

override_dh_installchangelogs:
	dh_installchangelogs RELEASE

override_dh_gencontrol:
	dh_gencontrol -- -V"prolog:ABI=$(PROLOG_ABI)"

# build manpage
debian/$(cmd).1: debian/%.1: debian/fake_bin/% %
	$(call _mkman, $<, $@, \
		Euler Yet another proof Engine (EYE) semantic web reasoning engine)
