#!/bin/sh
HOSTAPD_CONF="/etc/hostapd/$IFACE.conf"
HOSTAPD_BIN="/usr/sbin/hostapd"
HOSTAPD_PIDFILE="/var/run/hostapd/$IFACE.pid"
HOSTAPD_OPTIONS="-B -P $HOSTAPD_PIDFILE -i $IFACE"

# run supplicant only on enabled wireless devices
iw dev | grep -q "Interface $IFACE" || exit 0

if [ "$MODE" = "start" ] ; then

	[ -e $HOSTAPD_PIDFILE -a -e /proc/`cat $HOSTAPD_PIDFILE` ] && exit 0

	if [ "$MANUAL" = "" ] ; then

		# make sure interface is autostarted
		sys_params network/wifi/autostartInterfaces 2>/dev/null | grep -q "$IFACE" || exit 0

		# AP: apply volatile mode if defined, otherwise persistent
		VMODE=$(sys_params -l -V services/wifi/interfaces/$IFACE/mode)
		if [ -n "$VMODE" ]; then
			[ "$VMODE" = "AP" ] || exit 0
		else
			[ "$(sys_params -l network/wifi/interfaces/[name=$IFACE]/mode)" = "AP" ] || exit 0
		fi
	fi

	if [ -n "$HOSTAPD_CONF" ]; then
		# BSP-8464 Fix specific issue for Intel AX210 (regional settings not applied)
		if dmesg | grep -q iwlwifi; then
			C=$(grep "^country=" /etc/EPAD/system.ini | cut -d '=' -f 2)
			if [ -n "$C" ] && ! iw reg get | grep -q "^country $C"; then
				iw reg set "$C"
				ifconfig wlan0 down; ifconfig wlan0 up
				for i in `seq 1 10`; do
					iw reg get | grep -q "^country $C" && break
					sleep 1
				done
			fi
		fi
		start-stop-daemon --start --quiet \
			--startas $HOSTAPD_BIN --pidfile $HOSTAPD_PIDFILE \
			--  $HOSTAPD_OPTIONS $HOSTAPD_CONF 
		exit $?
	fi

elif [ "$MODE" = "stop" ]; then

	if [ -f "$HOSTAPD_PIDFILE" ]; then
		
		start-stop-daemon --stop --quiet --pidfile	$HOSTAPD_PIDFILE
			
		if [ -f "$HOSTAPD_PIDFILE" ]; then
			rm -f $HOSTAPD_PIDFILE
		fi
	fi

fi

exit 0
