#!/bin/bash

# For main Ethernet interface (i.e. eth0) we remove the entry in /etc/hosts
# that was added when the interface was brought up.

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

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

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

# Skip if zeroconf, we do not want to associate the hostname with
# a zeroconf address (we leave that for the main address)
[ "$METHOD" = "zeroconf" ] && exit 0

# Make sure we have a hostname
host="$(hostname)"
[ $? -eq 0 ] || exit 1
[ -n "$host" ] || exit 0

# Remove entry in /etc/hosts (the file is atomically updated)

tmphosts=$(mktemp /etc/hosts.XXXXXX)
[ $? -eq 0 ] || exit 1
chmod 644 $tmphosts

grep -v -P "\b${host}\b" /etc/hosts >> $tmphosts

mv -f $tmphosts /etc/hosts

exit 0
