#!/bin/sh

# Load ipmodule variables
. /opt/gira/share/devicestack/ipmodule-vars

# only for use as a "zcip" callback script
if [ "x$interface" = x ]
then
	printf "zcip: error: no interface.\\n"
	exit 1
fi

# zcip should start on boot/resume and various media changes
case "$1" in
	init)
		# for now, zcip requires the link to be already up,
		# and it drops links when they go down.  that isn't
		# the most robust model...
		exit 0
		;;
	config)
		if [ "x$ip" = x ]
		then
			printf "zcip-config: error: no ip.\\n"
			exit 1
		fi
		# remember $ip for $interface, to use on restart
		if [ "x$IP" != x ] && [ -w "$IP.$interface" ]
		then
			printf "%s\\n" "$ip" > "$IP.$interface"
		fi
		ip address add dev $interface scope link local "$ip/16" broadcast + || exit 1
		route add -net 224.0.0.0 netmask 240.0.0.0 $interface
		${IPC_SEND} netcfg-autoip-address-acquired
		logger zcip config $interface $ip
		exit 0
		;;
	deconfig)
		if [ x$ip = x ]
		then
			printf "zcip-deconfig: error: no ip.\\n"
			exit 1
		fi
		${IPC_SEND} netcfg-requesting-dhcp-address
		ip address del dev $interface local $ip
		logger zcip deconfig $interface $ip
		exit 0
		;;
esac
exit 1
