#!/bin/sh
# Updates information whenever a network interface is brought up.
# Note that this script is not called when interface with dhcpv6 is coming up.

[ "$IFACE" = "lo" ] && exit 0
RESOLV_CONF="/etc/resolv.conf"
MODE="$1"

case $MODE in

    start|stop)
	[ -x /usr/sbin/dhcp6ctl ] || exit 0

	IAP_BASE=/system/osso/connectivity/IAP
	IFACES=`gconftool-2 -g "$IAP_BASE/ipv6_interfaces" 2> /dev/null`
	INTERFACES=`echo "$IFACES" | sed -e 's/\[//g' -e 's/\]//g'`

	for i in $INTERFACES ; do
	    if [ "$interface" = "$i" ] ; then
		/usr/sbin/dhcp6ctl -k /etc/wide-dhcpv6/dhcp6cctlkey $MODE interface $interface

		# Make sure the client is stopped if stop was requested, this can be done
		# because only one interface is active at a time.
		if [ "$MODE" = stop ]; then
		    PIDFILE=/var/run/dhcp6c.pid
		    if [ -s $PIDFILE ]; then
			PID=`cat $PIDFILE`
			# We need to make sure the pid is a number
			expr "$PID" + 0 > /dev/null 2>&1
			if [ $? -eq 0 ]; then
			    if [ -d /proc/$PID/ ]; then
				sleep 1
				kill $PID > /dev/null 2>&1
				if [ -d /proc/$PID/ ]; then
				    sleep 1
				    kill -KILL $PID > /dev/null 2>&1
				fi
			    fi
			fi
		    fi
		fi
		exit 0
	    fi
	done
	exit 1
    ;;

    static)
        #[ -n "$broadcast" ] && BROADCAST="broadcast $broadcast"
	#[ -n "$subnet" ] && NETMASK="netmask $subnet"

	/sbin/ip addr add $ip dev $interface

	if [ -n "$router" ]; then
	    while /sbin/ip route del default dev $interface
	      do :
	    done

	    # XXX: This is not really working, fixme! 20070925
	    for i in $router
	      do
	      /sbin/ip route add default dev $interface
	    done
	fi

	/sbin/ip link set $interface up

# XXX: DNS to be done later. 20070924
#        if [ ! -k "$RESOLV_CONF" ]; then
#	     echo -n > $RESOLV_CONF
#	     [ -n "$domain" ] && echo domain $domain >> $RESOLV_CONF
#	     for i in $dns
#	     do
#	         echo nameserver $i >> $RESOLV_CONF
#	     done
#        fi

	/usr/bin/dbus-send --system --dest=com.nokia.icd /com/nokia/icd/autoconf com.nokia.icd.autoconf.autoconf_changed string:$interface string:DHCP6 string:CONNECTED
    ;;

    deconfig)
        /sbin/ip link set $interface down
	/usr/bin/dbus-send --system --dest=com.nokia.icd /com/nokia/icd/autoconf com.nokia.icd.autoconf.autoconf_changed string:$interface string:DHCP6 string:DISCONNECTED
    ;;

    *)
        if [ "$VERBOSITY" = "1" ] ; then
	    echo "dhcp6c-ifupdown: unknown mode \"$MODE\""
	fi
	exit 1
    ;;
esac

exit
