#!/bin/bash
#
# /etc/rc.d/wpa_supplicant: start/stop wpa_supplicant
#

IFNAME=(wlan0)

SSD=/sbin/start-stop-daemon
PROG=/usr/sbin/wpa_supplicant
OPTS="-B -D nl80211,wext -c /etc/wpa_supplicant.conf"


case $1 in
	start)
		for i in ${!IFNAME[@]}; do
			PID=/run/wpa_supplicant.${IFNAME[$i]}.pid
			$SSD --start --pidfile $PID --exec $PROG -- $OPTS -P $PID -i ${IFNAME[$i]}
		done
		;;
	stop)
		for i in ${!IFNAME[@]}; do
			$SSD --stop --retry 10 --pidfile /run/wpa_supplicant.${IFNAME[$i]}.pid
		done
		;;
	restart)
		$0 stop
		$0 start
		;;
	status)
		$SSD --status --pidfile $PID
		case $? in
		0) echo "$PROG is running with pid $(cat $PID)" ;;
		1) echo "$PROG is not running but the pid file $PID exists" ;;
		3) echo "$PROG is not running" ;;
		4) echo "Unable to determine the program status" ;;
		esac
		;;
	*)
		echo "Usage: $0 [start|stop|restart|status]"
		;;
esac

# End of file
