#!/bin/sh

killall smbd proftpd afpd minidlna luns_scan.sh detcable send_wol check_time_machine wifison-monitor.sh
sync

killall nmrp-client 2> /dev/null
ifconfig ath02 up

check=300
tm=0
#link_status=`cat /tmp/link_status` 2> /dev/null
link_status=`iwconfig ath02 | grep Not-Associated`
while [ -n "$link_status" -a "$tm" -le $check ]
do
	#link_status=`cat /tmp/link_status` 2> /dev/null
	link_status=`iwconfig ath02 | grep Not-Associated`
	ssid=`iwconfig ath02 | grep '##-NTGR-NMRP-ONLY-##' ` 2> /dev/null
	[ "x$ssid" = "x" ] && link_status=0
	let "tm=$tm+1"
	sleep 1
done

if [ -z "$link_status" ]; then
	/sbin/led_ring stop
	#amber =4, nmrp color = 100+4
	#/sbin/ledcontrol -n all -c 104 -s on -l 255
	echo "## START 2.4G NMRP ##"
	/sbin/ledcontrol -n all -c amber -s on 
	/usr/sbin/nmrp-client &
	exit 0
else
	/sbin/led_ring stop
	#red =0, nmrp color = 100+0
	/sbin/ledcontrol -n all -c red -s on
	killall nmrp-client 2> /dev/null
	echo "## Connect Fail STOP NMRP ##"
	ifconfig ath02 down
fi

exit 0
# exit, Spec defined not support 5G

ifconfig ath02 down
ifconfig ath21 up

check=300
tm=0
link_status=`cat /tmp/link_status_5g` 2> /dev/null
while [ "$link_status" != "1" -a "$tm" -le $check ]
do
	link_status=`cat /tmp/link_status_5g` 2> /dev/null
	let "tm=$tm+1"
	sleep 1
done

if [ "$link_status" = "1" ]; then
	killall led_ring_action 2> /dev/null 
	/sbin/ledcontrol -n all -c amber -s on
	echo "## START 5G NMRP ##"
	/usr/sbin/nmrp-client & 
else
	killall led_ring_action 2> /dev/null 
	/sbin/ledcontrol -n all -c red -s on
	killall nmrp-client 2> /dev/null
	echo "## Connect Fail STOP NMRP ##"
	ifconfig ath21 down
fi

exit 0
