#!/bin/bash
#
# File name:  /etc/dhclient-exit-hooks
#

# The only system interface that can run as DHCP client is eth1.
# Make sure that the interface has a valid IP address. Return immediately if no IP address is found.
/sbin/ifconfig eth1 | /bin/grep "inet "
if [ "$?" != "0" ] ; then
   exit 0
fi

# Add non interface-specific static-routes.
if [ -f /nn/sbin/refreshStaticRoutes.sh ]; then
   . /nn/sbin/refreshStaticRoutes.sh
fi    

# Remember the old published IP address
/nn/bin/pdrcmd get "publishedip\default\ipaddress" > /tmp/old_pub_ip_addr

# Update the published IP address and determine if it has changed
/nn/bin/ifdebug 10

# Get the refreshed published IP address.
# Note: MSM will get informed of the address change via the Published IP address change callback.
/nn/bin/pdrcmd get "publishedip\default\ipaddress" > /tmp/new_pub_ip_addr

# Determine if the published IP address has changed after renewing the DHCP client address.
# If it has changed, then update the /etc/hosts file and restart voice mail.
diff /tmp/old_pub_ip_addr /tmp/new_pub_ip_addr &> /dev/null
if [ "$?" = "1" ] ; then
   /nn/bin/updatehosts ipaddress `cat /tmp/new_pub_ip_addr` &> /dev/null
   /nn/bin/Restart_VoIP_Services.sh &> /dev/null
fi

rm -f /tmp/*_pub_ip_addr
