#!/bin/sh

if [ -z "$1" -o -z "$2" ]; then
	echo "Insufficient parameters" > /dev/stderr
	exit 1
fi

# network interface is first argument (eg. eth1)
IFACE="$1"
# wpa action in CONNECTED or DISCONNECTED is second argument
ACTION="$2"

# The WPA_CLI_ACTFILE file is a flag detected by the ifupdown scripts,
# indicating when the action script has successfully configured the interface
# when using the manual inet METHOD (see interfaces(5) manpage). It also
# prevents all CONNECTED action events thereafter, from re-configuring the
# interface. Do not change the path of WPA_CLI_ACTFILE, unless you also plan
# to edit /etc/wpa_supplicant/ifupdown.sh
#
# It is created after successfully configuring the interface after a CONNECTED
# event, and removed once the interface is deconfigured after a DISCONNECTED
# event.
WPA_CLI_ACTFILE="/var/run/wpa_cli.$IFACE.action"

# See /usr/share/doc/wpasupplicant/examples/wpacli-action-dhclient for a simple
# action script that invokes dhclient.

case "$ACTION" in

	CONNECTED)
	if [ ! -e "$WPA_CLI_ACTFILE" ]; then
		# Add here commands required to configure your interface, $IFACE
	fi
	touch $WPA_CLI_ACTFILE
	;;

	DISCONNECTED)
	if [ -e "$WPA_CLI_ACTFILE" ]; then
		# Add here commands required to deconfigure your interface, $IFACE
	fi
	rm -f $WPA_CLI_ACTFILE
	;;

	*)
	echo "Unknown action" > /dev/stderr
	exit 1
	;;
esac
