#!/bin/sh

### BEGIN INIT INFO
# Provides:		wpa-ifupdown
# Required-Start:	$network
# Required-Stop:	$network
# Should-Start:
# Should-Stop:
# Default-Start:
# Default-Stop:		0 6
# Short-Description:	Stop wpa_supplicant processes started via ifupdown
# Description:		Run ifdown on interfaces authenticated via
#			wpa_supplicant.	Sendsigs terminates wpa_supplicant
#			processes before networking is stopped causing each
#			network interface authenticated via a wpa_supplicant
#			daemon to be terminated abrubtly.
### END INIT INFO

PATH=/usr/sbin:/usr/bin:/sbin:/bin

test -d /var/run || exit 0

test -x /sbin/ifdown || exit 0

. /lib/lsb/init-functions

stop_wpa_action () {
	test -x /sbin/wpa_action || return
	unset IFACES
	IFACES=$(find /var/run -maxdepth 1 -type f -name 'wpa_action.*.pid' -printf '%P\n' | cut -d'.' -f2)
	if test -n "$IFACES"; then
		log_daemon_msg "Stopping wpa_action roaming interfaces"
		for iface in $IFACES; do
			log_progress_msg "$iface"
			# wpa_action executes /sbin/ifdown
			wpa_action "$iface" stop >/dev/null 2>&1
		done
		log_end_msg 0
	fi
}

stop_wpa_supplicant () {
	unset IFACES
	IFACES=$(find /var/run -maxdepth 1 -type f -name 'wpa_supplicant.*.pid' -printf '%P\n' | cut -d'.' -f2)
	if test -n "$IFACES"; then
		log_daemon_msg "Stopping wpa_supplicant interfaces"
		for iface in $IFACES; do
			log_progress_msg "$iface"
			ifdown $iface >/dev/null 2>&1
		done
		log_end_msg 0
	fi
}

case "$1" in
	start|restart|force-reload)
		# No-op
		;;
	stop)
		stop_wpa_action
		stop_wpa_supplicant
		;;
	*)
		echo "Usage: $0 {start|stop|restart|force-reload}" >&2
		exit 3
		;;
esac

exit 0
