#!/usr/bin/make -f

%:
	dh $@

include /usr/share/rustc/architecture.mk
export DEB_HOST_RUST_TYPE DEB_HOST_GNU_TYPE

PATH:=/usr/share/cargo/bin:$(PATH)
CARGO_HOME=$(shell pwd)/debian/cargo_home
DEB_CARGO_CRATE:=nsncd
export PATH CARGO_HOME DEB_CARGO_CRATE

TARGET_RELEASE_DIR = target/$(DEB_HOST_RUST_TYPE)/release
export TARGET_RELEASE_DIR

execute_before_dh_auto_build:
	if test -f Cargo.lock; then mv Cargo.lock Cargo.lock.upstream; fi
	cargo prepare-debian /usr/share/cargo/registry

execute_before_dh_auto_clean:
	if test -f Cargo.lock.upstream; \
	then mv Cargo.lock.upstream Cargo.lock; fi

override_dh_auto_build:
	cargo build --release

override_dh_auto_clean:
	cargo clean

# We use `__nss_disable_nscd` which is `GLIBC_PRIVATE`, which means the debian
# tools don't know how to pick a minimum version of libc6 for it. It instead
# picks a version range like `libc6 (>> 2.31), libc6 (<< 2.32)` if built on a
# system with 2.31.
#
# The effect of this that we don't like is that we can't install the .deb on an
# OS with a newer libc than the build machine, because that version bound is
# strict.
#
# This rule somewhat rudely edits them out of substvars after they're generated.
# We're taking a chance here that glibc isn't going to delete or change
# `__nss_disable_nscd` soon in the future.
#
# See https://github.com/twosigma/nsncd/pull/61#issuecomment-1529166183 for the
# original discovery of this.
override_dh_shlibdeps:
	dh_shlibdeps
	sed -i -e '/^shlibs:Depends=/s/, libc6 (\(<<\|>>\) [0-9\.]*)//g' debian/nsncd.substvars

# See README.source.
vendor:
	mkdir -p .cargo
	cargo vendor > .cargo/config
	cat debian/Cargo.toml.append >> Cargo.toml
	echo 'nsncd: source-is-missing vendor/*' > debian/source/lintian-overrides
