# vyatta dhclient exit script

log_v4_lease() {
    if [ -n "$new_ip_address" ] || [ -n "$old_ip_address" ] || [ -n "$alias_ip_address" ]; then
	LOG=/var/lib/dhcp/dhclient_"$interface"_lease
        echo `date` > $LOG

        for i in reason overlap interface new_expiry new_dhcp_lease_time medium \
                        alias_ip_address new_ip_address new_broadcast_address \
                        new_subnet_mask new_domain_name new_domain_search new_network_number \
                        new_domain_name_servers new_routers new_static_routes \
                        new_dhcp_server_identifier new_dhcp_message_type \
                        old_ip_address old_subnet_mask old_domain_name old_domain_search\
                        old_domain_name_servers old_routers \
                        old_static_routes; do
                echo $i=\'${!i}\' >> $LOG
        done
    fi
}

log_v6_lease() {
    if [ -n "$new_ip6_address" ] || [ -n "$old_ip6_address" ] ; then
        LOG=/var/lib/dhcp/dhclient_v6_"$interface"_lease
        echo `date` > $LOG

        for i in reason overlap interface new_expiry new_dhcp_lease_time \
                        new_ip6_address new_dhcp6_domain_search \
                        new_dhcp6_name_servers \
                        old_ip6_address old_dhcp6_domain_search \
                        old_dhcp6_name_servers ; do
                echo $i=\'${!i}\' >> $LOG
        done
    fi
}

do_vyatta_updates() {
    # call the helper script to amalgamate all interface specific resolv.conf to one master
    /opt/vyatta/sbin/vyatta_update_resolv.pl --dhclient-script 1 --interface $interface

    # if dns forwarding is using dhcp then call the dnsmasq helper script
    if cli-shell-api existsActive service dns forwarding dhcp; then
	dhcp_forwarders_list=$(cli-shell-api listActiveNodes service dns forwarding dhcp)
    fi
    if [ -n "`echo $dhcp_forwarders_list | grep $interface`" ]; then
	configure
	/opt/vyatta/sbin/vyatta-dns-forwarding.pl --dhcp-interface $interface --outside-cli --update-dnsforwarding
	end-configure
    fi
}


# Execute the operation
case "$reason" in

    BOUND|REBOOT)
	log_v4_lease

	do_vyatta_updates
	if [ -e "/var/run/.${interface}.tunnel_deferred.txt" ]; then
		/opt/vyatta/sbin/vyatta-tunnel-deferred.pl "DEFERRED_CREATE" "$interface" "$new_ip_address"
	fi
	;;

    RENEW|REBIND)
	log_v4_lease

	if [ "$new_domain_name" != "$old_domain_name" ] ||
	    [ "$new_domain_search" != "$old_domain_search" ] ||
	    [ "$new_domain_name_servers" != "$old_domain_name_servers" ]; then
	    do_vyatta_updates
	fi
	if [[ -e "/var/run/.${interface}.tunnel_deferred.txt"  &&  $new_ip_address != $old_ip_address ]]; then
		/opt/vyatta/sbin/vyatta-tunnel-deferred.pl "DEFERRED_CREATE" "$interface" "$new_ip_address"
	fi
	;;

    EXPIRE|RELEASE|STOP|TIMEOUT)
	log_v4_lease

	# delete interface specific resolve.conf
	if [ -z "$new_domain_name" ] && [ -z "$new_domain_search" ] &&
	   [ -z "$new_domain_name_servers" ]; then
	    rm -f $resolv_conf
	fi

	do_vyatta_updates
	;;

    FAIL|OVERLAPSUBNET)
	log_v4_lease

	# delete interface specific resolve.conf
	if [ -z "$new_domain_name" ] && [ -z "$new_domain_search" ] &&
	   [ -z "$new_domain_name_servers" ]; then
	    rm -f $resolv_conf
	fi
	;;

    BOUND6)
	log_v6_lease

	do_vyatta_updates
	if [ -e "/var/run/.${interface}.tunnel_deferred.txt" ]; then
		/opt/vyatta/sbin/vyatta-tunnel-deferred.pl "DEFERRED_CREATE" "$interface" "$new_ip_address"
	fi
	;;

    RENEW6|REBIND6)
	log_v6_lease

	if [ "${new_dhcp6_domain_search}" != "${old_dhcp6_domain_search}" ] ||
	    [ "${new_dhcp6_name_servers}" != "${old_dhcp6_name_servers}" ]; then
	    do_vyatta_updates
	fi
	if [[ -e "/var/run/.${interface}.tunnel_deferred.txt"  &&  $new_ip_address != $old_ip6_address ]]; then
		/opt/vyatta/sbin/vyatta-tunnel-deferred.pl "DEFERRED_CREATE" "$interface" "$new_ip_address"
	fi
	;;

    EXPIRE6|RELEASE6|STOP6)
	log_v6_lease

	# delete interface specific resolve.conf
	if [ -z "$new_dhcp6_domain_search" ] &&
	   [ -z "$new_dhcp6_name_servers" ]; then
	    rm -f $resolv_conf
	fi

	if [ -n "${old_ip6_address}" ] || [ -n "${old_ip6_prefixlen}" ]; then
	    do_vyatta_updates
	fi
	;;

    OVERLAPSUBNET6)
	log_v6_lease

	# delete interface specific resolve.conf
	if [ -z "$new_dhcp6_domain_search" ] &&
	   [ -z "$new_dhcp6_name_servers" ]; then
	    rm -f $resolv_conf
	fi
	;;

esac
