#!/usr/bin/make -f

export DEB_BUILD_MAINT_OPTIONS = hardening=+all
export DEB_CFLAGS_MAINT_APPEND  = -Wall -pedantic
export DEB_CXXFLAGS_MAINT_APPEND  = -Wall -pedantic

# trust upstream optimization level unless explicitly disabled
ifeq (,$(filter noopt,$(DEB_BUILD_OPTIONS)))
export DEB_CFLAGS_MAINT_STRIP=-O2
export DEB_CXXFLAGS_MAINT_STRIP=-O2
endif

# 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

%:
	dh $@

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

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

execute_after_dh_auto_build: $(DOCS) $(CHANGELOGS)

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; }
