#!/bin/sh

# This example script activates an interface based on the specified
# configuration.
#
# In the interest of keeping the KVP daemon code free of distro specific
# information; the kvp daemon code invokes this external script to configure
# the interface.
#
# The only argument to this script is the configuration file that is to
# be used to configure the interface.
#
# Each Distro is expected to implement this script in a distro specific
# fashion. For instance on Distros that ship with Network Manager enabled,
# this script can be based on the Network Manager APIs for configuring the
# interface.
#
# This example script is based on a RHEL environment.
#
# Here is the format of the ip configuration file:
#
# HWADDR=macaddr
# DEVICE=interface name
# BOOTPROTO=<protocol> (where <protocol> is "dhcp" if DHCP is configured
#                       or "none" if no boot-time protocol should be used)
#
# IPADDR0=ipaddr1
# IPADDR1=ipaddr2
# IPADDRx=ipaddry (where y = x + 1)
#
# NETMASK0=netmask1
# NETMASKx=netmasky (where y = x + 1)
#
# GATEWAY=ipaddr1
# GATEWAYx=ipaddry (where y = x + 1)
#
# DNSx=ipaddrx (where first DNS address is tagged as DNS1 etc)
#
# IPV6 addresses will be tagged as IPV6ADDR, IPV6 gateway will be
# tagged as IPV6_DEFAULTGW and IPV6 NETMASK will be tagged as
# IPV6NETMASK.
#
# The host can specify multiple ipv4 and ipv6 addresses to be
# configured for the interface. Furthermore, the configuration
# needs to be persistent. A subsequent GET call on the interface
# is expected to return the configuration that is set via the SET
# call.
#

. shell-ip-address

if ! test -f "$1"
then
    : expect configuration datafile as first argument
    exit 1
fi
. "$1"

if test -z "${DEVICE}"
then
    echo "Missing DEVICE= in ${cfg}"
    exit 1
fi

t_ifacedir=$(mktemp -d --tmpdir "$DEVICE.XXXXXXXXXX")
[ -n "$t_ifacedir" ] || exit 1

# Create options file
(
echo "TYPE=eth"
echo "CONFIG_WIRELESS=no"
echo "DISABLED=no"
echo "NM_CONTROLLED=no"

if test "${BOOTPROTO}" = "dhcp"
    then
	echo "BOOTPROTO=dhcp"
    else
	echo "BOOTPROTO=static"
fi
) >> "$t_ifacedir/options"

# Create ipv4address file
(
    # loop through all ipv4 adresses
    for index in $(set | sed -r -n 's;^IPADDR([[:digit:]]+)=.+$;\1;p')
    do
	pfx=
	# find corresponding NETMASK variable
	eval nm=\$NETMASK${index}
	# if specified, calculate prefix
	if test -n "${nm}"
	then
	    pfx="$(ipv4_mask2prefix "${nm}")"
	fi
	# if not specified, force prefix
	if test -z "${pfx}"
	then
	    pfx="32"
	fi
	# construct actual value
	eval val=\$IPADDR${index}
	# write config variable
	echo "${val}/${pfx}"
    done
) >> "$t_ifacedir/ipv4address"

# Create ipv6address file
(
    for index in $(set | sed -r -n 's;^IPV6ADDR([[:digit:]]+)=.+$;\1;p')
    do
	# find corresponding IPV6NETMASK variable
	eval pfx=\$IPV6NETMASK${index}
	# if not specified, force prefix
	if test -z "${pfx}"
	then
	    pfx=128
	fi
	# construct actual value
	eval val=\$IPV6ADDR${index}
	# write config variable
	echo "${val}/${pfx}"
    done
) >> "$t_ifacedir/ipv6address"

# Create ipv4route file
(
    if test -n "${GATEWAY}"
    then
	echo "default via $GATEWAY"
    fi
) >> "$t_ifacedir/ipv4route"

# Create ipv6route file
(
    if test -n "${IPV6_DEFAULTGW}"
    then
	echo "default via $IPV6_DEFAULTGW"
    fi
) >> "$t_ifacedir/ipv6route"


# Create DNS info
(
    for addr in $(set | sed -r -n 's/^DNS[[:digit:]]+=//p')
    do
	# write config variable
	echo "nameserver ${addr}"
    done
) >> "$t_ifacedir/resolv.conf"

echo "$0: working on network interface ${DEVICE}"

mkdir -p "/etc/net/ifaces/${DEVICE}/"
for f in "$t_ifacedir"/*; do
	[ -s "$f" ] || continue
	mv -fb -- "$f" "/etc/net/ifaces/${DEVICE}/"
done

rm -r -- "$t_ifacedir"

ifdown ${DEVICE}
ifup ${DEVICE}
