#!/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

mainpkg = tailspin
cmd = tspin

# generate documentation unless nodoc requested
ifeq (,$(filter nodoc,$(DEB_BUILD_OPTIONS)))
DOCS = README.html README.txt
CHANGELOGS = CHANGELOG.html CHANGELOG.txt
MANPAGES = debian/build/$(cmd).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-arch: debian/$(mainpkg)/usr/bin/$(cmd) $(MANPAGES)
	mkdir --parents debian/build
	$< --generate-bash-completions > debian/build/completion.bash
	$< --generate-bash-completions > debian/build/$(cmd).fish
	$< --generate-zsh-completions > debian/build/_tspin

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

override_dh_installchangelogs:
	dh_installchangelogs -- $(CHANGELOGS)

# generate manpage
debian/build/$(cmd).1: debian/build/%.1: debian/$(mainpkg)/usr/bin/%
	mkdir --parents debian/build
	help2man --section 1 --no-info \
		--name "log file highlighter and a drop-in replacement for tail -f" \
		--output $@ $< \
		|| { $< --help; false; }

# avoid cleaning unless source is patched
override_dh_auto_clean:
	grep -q '"1.93"' Cargo.toml || dh_auto_clean --buildsystem rust
