#!/bin/sh

. /etc/wifi/variscite-wifi.conf
. /etc/wifi/variscite-wifi-common.sh

# Return true if WIFI interface exists
wifi_interface_exists()
{
	for i in $(seq 1 20); do
		[ -d /sys/class/net/wlan0 ] && return 0
		sleep 1
	done

	return 1
}

# Start WIFI hardware
wifi_start()
{
	# Exit if WIFI should not be started
	wifi_should_not_be_started && exit 0

	# Setup WIFI control GPIOs
	wifi_pre_up

	# Try starting WIFI hardware
	for i in $(seq 1 3); do
		# Up WIFI
		wifi_up

		# Exit if WIFI interface exists
		wifi_interface_exists && exit 0

		# Down WIFI
		wifi_down

		# Wait enough time for discharge
		sleep 5
	done

	exit 1
}

# Stop WIFI hardware
# Note that on DART-MX8M this also stops Ethernet
wifi_stop()
{
	# Exit if WIFI should not be stopped
	wifi_should_not_be_stopped && exit 0

	wifi_down
}

###########################
#  Execution starts here  #
###########################
case $1 in
start)
	wifi_start
	;;
stop)
	wifi_stop
	;;
esac

exit 0
