#!/bin/sh
#
# Start up wpa_supplicant on interface

[ "$WIFIROAMD_WPA_SUPPLICANT" != 1 ] && exit 0

if [ "${WIFIROAMD_WPA_SUPPLICANT_USE_SERVICE:-0}" == "1" ]
then
	[ "$WIFIROAMD_VERBOSE_LEVEL" -gt 0 ] && \
		logger -t 'wifiroamd_wpa_supplicant' "Starting wpa_supplicant service"

	/etc/init.d/wpa_supplicant restart
else
	#  set the config file if not already set
	if [ -z "${WIFIROAMD_WPA_SUPPLICANT_CONF_FILE}" ]
	then
		if [ -f "/etc/wpa_supplicant/mac:${WIFIROAMD_MACADDRESS}.conf" ]
		then
			WIFIROAMD_WPA_SUPPLICANT_CONF_FILE="/etc/wpa_supplicant/mac:${WIFIROAMD_MACADDRESS}.conf"
		else
			WIFIROAMD_WPA_SUPPLICANT_CONF_FILE=/etc/wpa_supplicant/wpa_supplicant.conf
		fi
	fi

	[ "$WIFIROAMD_VERBOSE_LEVEL" -gt 0 ] && \
		logger -t 'wifiroamd_wpa_supplicant' "Starting wpa_supplicant " \
			"with configuration ${WIFIROAMD_WPA_SUPPLICANT_CONF_FILE}"

	killall wpa_supplicant

	wpa_supplicant -B \
		-i ${WIFIROAMD_WPA_SUPPLICANT_DEVICE:-${WIFIROAMD_DEVICE}} \
		-D ${WIFIROAMD_WPA_SUPPLICANT_DRIVER:-wext} \
		-c ${WIFIROAMD_WPA_SUPPLICANT_CONF_FILE:-/etc/wpa_supplicant/wpa_supplicant.conf}
fi
