#!/bin/sh

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

action=$1

handle_deconfig() {
    logger dhcp deconfig $interface
    ifconfig $interface 0.0.0.0
    rm -f ${ETH0_SERVERID_FILE}
    ${IPC_SEND} netcfg-requesting-autoip-address
    if [ -f "/etc/zcip.script" ]; then
        zcip $interface /etc/zcip.script
    fi
}

handle_renew() {
    ifconfig $interface $ip netmask $subnet
    route del default $interface 2> /dev/null
    [ -n "$router" ] && route add default gw $router
    route add -net 224.0.0.0 netmask 240.0.0.0 $interface
    rm -rf ${RESOLV_CONF}
    [ -n "$domain" ] && echo domain $domain >> ${RESOLV_CONF}
    for i in $dns ; do
        echo nameserver $i >> ${RESOLV_CONF}
    done
    echo $serverid > ${ETH0_SERVERID_FILE}
    ${IPC_SEND} netcfg-dhcp-address-acquired
    logger dhcp renew $interface $ip
    if [ -f "/etc/init.d/S60openvpn" ]; then
        /etc/init.d/S60openvpn dhcp_renew
    fi
}

handle_bound() {
    logger dhcp bound
    killall -q zcip
    handle_renew
}

case $action in
    'deconfig')
        handle_deconfig
        ;;
    'bound')
        handle_bound
        ;;
    'renew')
        handle_renew
        ;;
esac
