#!/usr/bin/make -f

export DEB_BUILD_MAINT_OPTIONS=hardening=+all

include /usr/share/dpkg/default.mk

CFLAGS += $(CPPFLAGS)

%:
	dh $@

override_dh_auto_clean:
	dh_auto_clean
	dh_auto_clean --buildsystem=rust

override_dh_auto_build:
	dh_auto_build -- PREFIX=/usr LIBDIR=/usr/lib/$(DEB_HOST_MULTIARCH)
	dh_auto_build --buildsystem=rust

# tree-sitter-cli's tests require downloading fixtures at build time, so we can't run those.
# xtask is an internal crate used for release automation
override_dh_auto_test:
	dh_auto_test --buildsystem=rust -- --tests --workspace --exclude tree-sitter-cli --exclude xtask

override_dh_auto_install:
	dh_auto_install -- PREFIX=/usr LIBDIR=/usr/lib/$(DEB_HOST_MULTIARCH)
# Upstream creates symlinks .so -> .so.0 -> .so.$SOVER. .so.0 shouldn't exist, since
# that isn't the $SOVER and causes cross-SOVER file conflicts. Shuffle the symlinks
# around so .so -> .so.$SOVER
	rm -f debian/tmp/usr/lib/$(DEB_HOST_MULTIARCH)/libtree-sitter.so
	mv debian/tmp/usr/lib/$(DEB_HOST_MULTIARCH)/libtree-sitter.so.0 debian/tmp/usr/lib/$(DEB_HOST_MULTIARCH)/libtree-sitter.so
	dh_auto_install --buildsystem=rust

define crate_ver =
$(shell perl -n -E 'if (/^version\s*=\s*"([\d.]+)["~]/) {say "$$1";}' crates/language/Cargo.toml)
endef

override_dh_gencontrol:
	dh_gencontrol -p librust-tree-sitter-language-dev -- -v$(crate_ver)+$(DEB_VERSION_UPSTREAM_REVISION)
	dh_gencontrol --remaining-packages
