#!/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
ifeq (,$(filter nodoc,$(DEB_BUILD_OPTIONS)))
DOCS = README.html README.txt ARCHITECTURE.html ARCHITECTURE.txt
MANPAGES = debian/delta.1
endif

%:
	dh $@

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

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

execute_after_dh_auto_build: $(DOCS)

execute_after_dh_auto_install: $(MANPAGES)

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

# prepare zsh completion snippet as specially named file
execute_before_dh_install:
	cp etc/completion/completion.zsh debian/_delta

# build manpages
debian/delta.1: debian/%.1: debian/git-delta/usr/bin/%
	help2man --section 1 --no-info \
		--name "syntax-highlighting pager for git, diff, and grep output" \
		--output $@ $< \
		|| { $< --help; false; }
	perl -i -p -e 's/\e\[[0-9;]*[mM]//g' $@
