#!/usr/bin/make -f

# generate documentation unless nodoc requested
ifeq (,$(filter nodoc,$(DEB_BUILD_OPTIONS)))
DOCS = README.html README.txt
CHANGELOGS = Changes.html Changes.txt
MANPAGES = debian/build/precious.1
endif

EXTRALIBS = path-clean-0.1.0

# generate manpage with help2man from --help option of executable
_mkman = help2man $(if $3,--name "$(strip $3)") --no-info --output $2 $1 \
 || { $1 --help; false; }

execute_after_dh_auto_configure:
	ln --symbolic --target-directory=debian/cargo_registry $(patsubst %,../extralibs/%,$(EXTRALIBS))

execute_after_dh_auto_build: $(DOCS) $(CHANGELOGS)

execute_after_dh_auto_install: $(MANPAGES)

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

override_dh_installchangelogs:
	dh_installchangelogs -- $(CHANGELOGS)

# build manpages
debian/build/precious.1: debian/build/%.1: debian/precious/usr/bin/%
	mkdir --parents $(dir $@)
	$(call _mkman, $<, $@, \
		one code quality tool to rule them all)

create-cargo-checksum:
	echo '{"package":"$(shell sha256sum Cargo.toml | grep -Po '^\S+')","files":{}}' > debian/cargo-checksum.json

fetch-extralibs:
	CARGO_HOME=debian/build/cargo-home cargo vendor --versioned-dirs debian/build/extralibs
	rm -rf debian/extralibs
	mkdir debian/extralibs
	mv --target-directory=debian/extralibs $(patsubst %,debian/build/extralibs/%,$(EXTRALIBS))
	rm -rf debian/build
	git restore Cargo.lock

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

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

%:
	dh $@ --buildsystem cargo
