#!/bin/sh
#Called by wpa_cli daemon. Wpa_cli is executed like this 'wpa_cli -a dhcpcd-wpagui'.
#Wpa_cli calls dhcpcd-wpagui with: $1 is the interface (eth0, ath0, etc), and $2 is the event - either "CONNECTED" or "DISCONNECTED". 

TIMEOUT=120	# in seconds

if [ "$2" = "CONNECTED" ] ; then 
	dhcpcd -t $TIMEOUT -L -h $(hostname) $1		# timeout, no ipv4ll, pass hostname to dhcp server
	cat /var/run/dhcpcd-$1.pid > /tmp/dhcpcd-wpagui.pid
fi

if [ "$2" = "DISCONNECTED" ] && [ -f /tmp/dhcpcd-wpagui.pid ]; then
	read pid < /tmp/dhcpcd-wpagui.pid
	kill $pid; sleep 1; kill -0 $pid && kill -9 $pid
	rm -f /tmp/dhcpcd-wpagui.pid
	ifconfig $1 0.0.0.0
fi
