#!/usr/bin/make -f

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

# generate documentation unless nodoc requested
ifeq (,$(filter nodoc,$(DEB_BUILD_OPTIONS)))
DOCS = README.html README.txt
endif

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

# resolve crate version
# * get topmost version except prerelease suffix
#   from Cargo.toml file in dir passed as argument
# * source package version if matching upstream part modulo prerelease
# * otherwise append source package version
crate_version = \
 $(shell perl -n \
 -E '/^version\W+\K\d+\.\d+\.\d+/ ' \
 -E 'and say "$(DEB_VERSION_UPSTREAM)" =~ /^\Q$$&\E(?:~[a-z]+\d*)?$$/ ? "$(DEB_VERSION)" : "$$&+$(DEB_VERSION_UPSTREAM_REVISION)" and exit' \
 $1/Cargo.toml)

# generate manual pages from output of clap-based Rust binary
#  arguments: 1:MANFILEPATH 2:COMMANDPATH 3:SUBCOMMAND(S)
_help2man = \
 $(eval suffix = $(lastword $(subst ., ,$(1))))\
 $(eval stem = $(patsubst %.$(suffix),%,$(1)))\
 help2man --section $(suffix) --no-info \
 --version-string='$(DEB_VERSION_UPSTREAM)' \
 --name $(shell $(2) $(3) --help | head --lines=1 | xargs -0 shell-quote --) \
 --output '$(stem)$(if $(3),-$(subst $() ,-,$(strip $(3)))).$(suffix)' \
 '$(2)$(if $(3), $(3))' \
 || { $(2) $(3) --help; false; }

mainpkg = lightningcss
cmd = lightningcss

# generate documentation unless nodoc requested
ifeq (,$(filter nodoc,$(DEB_BUILD_OPTIONS)))
MANPAGES = debian/build/$(cmd).1
endif

%:
	dh $@

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

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

override_dh_auto_build:
	dh_auto_build --buildsystem=rust -- --features cli

execute_after_dh_auto_build: $(DOCS)

override_dh_auto_install:
	dh_auto_install --buildsystem=rust -- --features cli

execute_after_dh_auto_install-arch: $(MANPAGES)

override_dh_installdocs:
	dh_installdocs -- $(DOCS)

# declare version for virtually provided embedded crates
packages = $(patsubst %,librust-%-dev,\
 lightningcss)
packages_virtual = $(patsubst %,librust-%-dev,\
 lightningcss-derive parcel-selectors static-self static-self-derive)
override_dh_gencontrol:
	$(eval librust-lightningcss-dev_version = $(call crate_version,.))\
	$(eval librust-lightningcss-derive-dev_version = $(call crate_version,derive))\
	$(eval librust-parcel-selectors-dev_version = $(call crate_version,selectors))\
	$(eval librust-static-self-dev_version = $(call crate_version,static-self))\
	$(eval librust-static-self-derive-dev_version = $(call crate_version,static-self-derive))\
	set -e; $(foreach p,$(packages),\
	 dh_gencontrol -p$p -- \
	 -v'$($p_version)' \
	 $(foreach q,$(filter-out $p,$(packages) $(packages_virtual)),\
	 -V'rust:Version:$q=$($q_version)');)
	dh_gencontrol --remaining-packages

# build manpages
debian/build/$(cmd).1: debian/build/%.1: debian/$(mainpkg)/usr/bin/%
	mkdir --parents debian/build
	$(call _help2man,$@,$<)
