#!/bin/sh
set -eu

fail() {
    printf '%s\n' "rustd naming check: $*" >&2
    exit 1
}

grep -Fq 'name = "rustd-resolved"' Cargo.toml || fail 'Cargo package is not rustd-resolved'
grep -Fq 'name = "rustd_resolved"' Cargo.toml || fail 'library crate is not rustd_resolved'
for target in rustd-resolved rustd-resolvectl systemd-resolved resolvectl; do
    grep -Fq "name = \"$target\"" Cargo.toml || fail "missing Cargo binary target $target"
done
grep -Fq 'target/release/rustd-resolved' Makefile || fail 'native daemon is not installed'
grep -Fq 'target/release/rustd-resolvectl' Makefile || fail 'native resolver client is not installed'
grep -Fq '$(LIBDIR)/libnss_resolve.so.2' Makefile || fail 'NSS module is not installed'
grep -Fq 'rustd-resolved.service' Makefile || fail 'native service is not installed'
grep -Fq 'systemd-resolved.service' Makefile || fail 'compatibility service is missing'
grep -Fq 'ln -sfn rustd-resolvectl' Makefile || fail 'compatibility command aliases are missing'
grep -Fq '%{_libdir}/libnss_resolve.so.2' packaging/rpm/rustd-resolved.spec \
    || fail 'RPM payload is missing the NSS module'
grep -Fq 'LIBDIR=/usr/lib/$(DEB_HOST_MULTIARCH)' debian/rules \
    || fail 'Debian payload is missing the multiarch NSS module path'

legacy_a='systemd-resolved''-rs'
legacy_b='resolved''-rs'
legacy_c='SYSTEMD_RESOLVED''_RS'
legacy_d='systemd_resolved''_rs'
legacy_e='RESOLVED''_RS'
legacy_f='resolved''_rs'
legacy_g='resolved'':'':'
if git grep -n -I -e "$legacy_a" -e "$legacy_b" -e "$legacy_c" -e "$legacy_d" -e "$legacy_e" -e "$legacy_f" -- .; then
    fail 'legacy project identifiers remain'
fi
if git grep -n -I -E "(^|[^[:alnum:]_])${legacy_g}" -- '*.rs'; then
    fail 'legacy Rust crate references remain'
fi
if git ls-files | grep -F "$legacy_a"; then
    fail 'legacy project name remains in a tracked path'
fi
