#!/usr/bin/make -f

# resolve DEB_VERSION
include /usr/share/dpkg/pkg-info.mk

# generate documentation unless nodoc requested
ifeq (,$(filter nodoc,$(DEB_BUILD_OPTIONS)))
DOCS = README.html README.txt
MANPAGES = debian/pikchr-cli.1
endif

# resolve crate version
# * append package version if upstream part and crate version differ
crate_version = \
 $(shell perl -n \
 -E '/version\W+\K[^"]+/ and $$_ = $$& =~ s/-PRE/~pre/r ' \
 -E 'and say $$_ eq "$(DEB_VERSION_UPSTREAM)" ? "$(DEB_VERSION)" : "$$_+$(DEB_VERSION_UPSTREAM_REVISION)" and exit' \
 $1/Cargo.toml)

%:
	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)

override_dh_gencontrol:
	dh_gencontrol -ppikchr-cli -- \
	 -v'$(call crate_version,pikchr-cli)'
	dh_gencontrol --remaining-packages

# build manpages
debian/pikchr-cli.1: debian/%.1: debian/pikchr-cli/usr/bin/%
	mkdir --parents $(dir $@)
	help2man --section 1 --no-info \
		--name "convert from PIC-like diagramming language to SVG" \
		--output $@ $< \
		|| { $1 --help; false; }
