#!/bin/sh

set -e

. debian/tests/lib.sh

require_apt

distinctive_hostname="nss-mdns-test-$(dbus-uuidgen)"

assert_succeeds avahi-set-host-name "$distinctive_hostname"
sleep 3

pkg=libnss-mdns
if [ -n "$DEB_HOST_ARCH" ]; then
    pkg="$pkg:$DEB_HOST_ARCH"
fi
assert_succeeds apt-get -y purge "$pkg"

assert_succeeds perl -i -pe 's/^hosts:.*/hosts: files dns/' /etc/nsswitch.conf

assert_succeeds apt-get -y install "$pkg"
assert_hosts "hosts: files mdns4_minimal [NOTFOUND=return] dns"

# This is going to fail on a cross-test because 'getent' is from the testbed arch
if [ -z "$DEB_HOST_ARCH" ]; then
    assert_succeeds getent hosts "$distinctive_hostname.local"
fi

assert_succeeds apt-get -y remove "$pkg"
# No assertion about whether it's in nsswitch.conf: debian/tests/nss-behaviour
# demonstrates that it doesn't actually matter
assert_status 2 getent hosts "$distinctive_hostname.local"

echo "# https://bugs.debian.org/cgi-bin/bugreport.cgi?bug=500478"
echo "# https://bugs.debian.org/cgi-bin/bugreport.cgi?bug=782282"
assert_succeeds apt-get -y install "$pkg"
assert_hosts "hosts: files mdns4_minimal [NOTFOUND=return] dns"
if [ -z "$DEB_HOST_ARCH" ]; then
    assert_succeeds getent hosts "$distinctive_hostname.local"
fi

assert_succeeds apt-get -y purge "$pkg"
assert_hosts "hosts: files dns"
assert_status 2 getent hosts "$distinctive_hostname.local"

finish

# vim:set sw=4 sts=4 et:
