#!/bin/sh
#
# This script is invoked by zeroconf
# during three phases of it setup:
# init, config, deconfig
# the phase will be the first argument
#
# init might be used for 'plumbing' or
# modprobing the interface
#
# config is used to assign an IP address
# to an interface
#
# deconfig is used to remove an IP address
# from an interface

# ip is supplied by the zeroconf program in the environment

PATH=/usr/bin:/bin:/usr/sbin:/sbin
export PATH

umask 0022


GREP=/bin/grep
CUT=/bin/cut
XARGS=/usr/bin/xargs
IP_PROG=/sbin/ip

if [ $# -lt 2 ]; then
    /usr/bin/printf "$0: error. insufficient arguments\n"
    /usr/bin/printf "usage: $0 <phase> <interface>\n"
    /usr/bin/printf "\tphase    \tis one of init, config or deconfig\n"
    /usr/bin/printf "\tinterface\tis the network device to add or delete\n"
    /usr/bin/printf "\t         \tIPv4 Link-Local addresses from / to \n"
    exit 1
fi

PHASE=$1
IFACE=$2

remove_linklocal_addrs() 
{
    # XXX #dep/parse: ip
    $IP_PROG addr show $IFACE | $GREP "inet.*169.254" | $CUT -d" " -f6 | $XARGS --replace -n 1 $IP_PROG addr del {} dev $IFACE
}

case $PHASE in
    init)
        $IP_PROG link set $IFACE up
        exit 0
        ;;
    config)
        remove_linklocal_addrs
#          $IP_PROG addr add $ip/16 scope link dev $IFACE
        /opt/tms/bin/mdreq -qq action /net/interface/actions/override/set \
        ifname string $IFACE         \
        ip ipv4addr $ip              \
        netmask ipv4addr 255.255.0.0 \
        enabled bool true
        EXIT_CODE=$?
        exit ${EXIT_CODE}
        ;;
    deconfig)
        remove_linklocal_addrs
        exit 0
    ;;
esac

exit 0
