#!/bin/bash

# Make sure that the interface has no state leftover, ifdown leaves the IP
# address on the interface, since it assumes the interface is going to be
# kept down.
#

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

# Skip if not inet or inet6
[ "$ADDRFAM" = "inet" -o "$ADDRFAM" = "inet6" ] || exit 0

# Skip if interface does no longer exist
[ -e /sys/class/net/"$IFACE" ] || exit 0

# Skip if not of Ethernet or WLAN type
[ "$(< /sys/class/net/$IFACE/type)" = 1 ] || 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

# Flush the IP addresses on the non-aliased device
# (avoid aliases by using the label)
if [ "$ADDRFAM" = "inet" ]; then
    ip -4 addr flush dev "$IFACE" label "$IFACE" 2>/dev/null
elif [ "$ADDRFAM" = "inet6" ]; then
    # we should leave alone automatic link-local address as well as any
    # auto-configured addresses (dynamic), hence the global scope and
    # permanent attribute
    ip -6 addr flush dev "$IFACE" label "$IFACE" scope global permanent 2>/dev/null
fi

exit 0
