#!/usr/bin/make -f

# generate documentation unless nodoc requested
DOCS_stem = README $(basename $(wildcard doc/*.md))
ifeq (,$(filter nodoc,$(DEB_BUILD_OPTIONS)))
DOCS = $(DOCS_stem:=.html) $(DOCS_stem:=.txt)
CHANGELOGS = Changelog.html Changelog.txt
MANPAGES = debian/build/settle.1
endif

# use local fork of dh-cargo and cargo wrapper
PATH := $(CURDIR)/debian/dh-cargo/bin:$(PATH)
PERL5LIB = $(CURDIR)/debian/dh-cargo/lib
export PATH PERL5LIB

%:
	dh $@ --buildsystem cargo

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

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

execute_after_dh_auto_build: $(DOCS) $(CHANGELOGS)

# TODO: drop when bug#1038873 is fixed
override_dh_auto_install:
	dh_auto_install || true

execute_after_dh_auto_install: $(MANPAGES)
	mkdir --parents debian/build
	debian/settle/usr/bin/settle compl bash > debian/build/bash-completion
	debian/settle/usr/bin/settle compl zsh > debian/build/_settle

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

override_dh_installchangelogs:
	dh_installchangelogs -- $(CHANGELOGS)

# build manpage
debian/build/settle.1: debian/build/%.1: debian/settle/usr/bin/%
	mkdir --parents $(dir $@)
	help2man --section 1 --no-info \
		--name "a CLI tool for managing a digital Zettelkasten" \
		--output $@ $< \
		|| { $1 --help; false; }
