#! /bin/sh

# Shell script to start wpasupplicant prior to bringing up a network
# interface

# All of the following wpasupplicant conffiles must be present
DEFAULTS=/etc/default/wpasupplicant
CONFFILE=/etc/wpa_supplicant.conf
INITD=/etc/init.d/wpasupplicant

# Exit if any of the above conffiles are missing
if [ ! -x $INITD -o ! -f $DEFAULTS -o ! -f $CONFFILE ]; then
  exit 0
fi

# Sanity-check that the wireless interface is defined in $DEFAULTS ,
# else we'll erroneously start wpasupplicant each time any interface
# is brought up, which is wrong. We presume that if $IFACE is on a
# non-commented line beginning with "OPTIONS=", then it's a valid
# wireless interface.
if egrep -q ''^OPTIONS=\"*.*''$IFACE'' $DEFAULTS; then

# Otherwise start wpasupplicant
# Make sure "-w" is listed as an option in either $DEFAULTS or
# $CONFFILE . Also make sure ENABLED is set to a non-zero value in
# $DEFAULTS . Currently these sanity-checks are handled by $INITD .

if [ -x /usr/sbin/invoke-rc.d ]; then
  invoke-rc.d wpasupplicant start 2>&1 3>&1 || true
else
  /etc/init.d/wpasupplicant start 2>&1 3>&1 || true
fi

# End egrep clause
fi
exit 0
