#!/usr/bin/make -f

# generate documentation unless nodoc requested
DOCS_stem = README $(basename $(wildcard docs/*.md))
ifeq (,$(filter nodoc,$(DEB_BUILD_OPTIONS)))
DOCS = $(addsuffix .html,$(DOCS_stem)) $(addsuffix .txt,$(DOCS_stem))
MANPAGES = regalloc2-tool.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)

# TODO: enable when upstream code supports non-tty execution
#execute_after_dh_auto_install: #$(MANPAGES)
override_dh_installman:

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

# build manpages
# TODO: enable when upstream code supports non-tty execution
#regalloc2-tool.1: %.1: debian/regalloc2-tool/usr/bin/%
#	mkdir --parents $(dir $@)
#	help2man --section 1 --no-info \
#		--name "tool for testing regalloc2" \
#		--output $@ $< \
#		|| { $1 --help; false; }
