#!/bin/bash

# For main Ethernet interface (i.e. eth0) we do miscellaneous
# configurations:
# - update resolv.conf for static config
# - make sure nscd sees any changed resolv.conf
# - announce our new address with ARP
# - check ntpd so that it has all its server names resolved

# The resolv.conf file used for static IP config
STATICRESOLVCONF=/etc/network/resolv.conf.static

[ -n "$IFACE" ] || exit 1

# Skip if not inet
[ "$ADDRFAM" = "inet" ] || exit 0

# Skip if not eth0
[ "$IFACE" = "eth0" ] || exit 0

# Setup /etc/resolv.conf for static configuration
if [ "$METHOD" = "static" ]; then
    if [ -f "$STATICRESOLVCONF" ]; then
	cat "$STATICRESOLVCONF" > /etc/resolv.conf
    else
	cat /dev/null > /etc/resolv.conf
    fi
fi

# Make sure nscd catches a changed /etc/resolv.conf
# (it can be changed by us above or by dhcp client before or something else)
nscd -i hosts

# Announce ourselves if we know the IP address, zeroconf handles this itself
if [ "$IF_ADDRESS" -a "$METHOD" != "zeroconf" ]; then
    # RFC3927 recommends 2 ARP packets 2 seconds apart
    arping -q -U -c 1 -I "$IFACE" "$IF_ADDRESS"
    sleep 2
    arping -q -U -c 1 -I "$IFACE" "$IF_ADDRESS"
fi

# Check that ntpd got all its servers OK, if not the script will restart ntpd,
# if using dhcp or zeroconf we need to also check for IP address change
if [ "$METHOD" = "dhcp" -o "$METHOD" = "zeroconf" ]; then
    NTPCHECK_OPT=--force
else
    NTPCHECK_OPT=
fi
/etc/spinetix/ntp-check $NTPCHECK_OPT

exit 0
