#!/bin/bash

# Make sure that the interface was no state leftover from any previous
# network settings.
#

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

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

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

# Skip if zeroconf, it uses addresses on aliased (or labeled) interfaces
# and manages them correctly itself and we do not want to interfere
# with addresses on the non-aliased interface.
[ "$METHOD" = "zeroconf" ] && exit 0

# Avoid running this for nfsroot systems
grep -q nfsroot= /proc/cmdline && exit 0

# Make sure that no leftover IPv4 addresses from previously half
# configured or half unconfigured interfaces are associated with the
# interface; note that if all IPv4 addresses are removed then all routes
# associated with the interface are removed as well.. We keep other
# addresses (e.g., IPv6, IPv4LL) available.
ip -4 addr flush dev "$IFACE" label "$IFACE" 2>/dev/null

# Remove all routes which have not been truly statically assigned, as
# since other IPv4 addresses may be present the command above does
# not always remove the IPv4 routes through the interface (the boot
# protocol is the default protocol for routes added from user space).
ip -4 route flush proto boot 2>/dev/null

# Bring up the interface, as some methods (e.g., manual) will not do it
if ! ip link set "$IFACE" up; then
    echo "ERROR: cannot enable $IFACE" >&2
    exit 1
fi

exit 0
