#!/bin/bash

[ -z "$1" ] && echo "Error: should be called from udhcpc" && exit 1

AUTOIP_DHCP_RETRY="120"  # 2 min

trap '' HUP

case "$1" in

	leasefail)
		# --force-bind is used to make sure autoip is requested even if a
		# previous routable address is assigned
		/usr/sbin/avahi-autoipd -wD --force-bind $interface 2> /dev/null

		[ -e /var/run/udhcpc-retry.$interface.pid ] && exit 0

		pkill -f "udhcpc .*-i $interface.*"

		# http://thread.gmane.org/gmane.linux.network.ifplugd.general/72
		# "ifplugd waits until all data has been read from STDOUT before assuming that the app finished to run."
		/sbin/udhcpc-retry $interface $AUTOIP_DHCP_RETRY > /dev/null 2> /dev/null < /dev/null &
		PID=$!
		echo ${PID} > /var/run/udhcpc-retry.$interface.pid
		disown ${PID}
		;;
esac

exit 0
