#!/bin/sh

[ "$ACTION" = "ifup" ] || exit 0

. /usr/share/libubox/jshn.sh
. /sbin/common_wifi

json_load "`cat /tmp/cache/network/network.conf`"
json_get_var opmode "opmode"
json_get_var apmode_proto "lan_protocol"
json_get_var apmode_iface "lan_iface"
json_get_var apmode_interface "lan_interface"

[ "$opmode" = "apmode" ] || exit 0

wieless_restart_cmd()
{
	common_down_all
	common_hyfi_bidging_stop
	common_hyfi_bidging_stat
	common_up
#	/sbin/ntg_disconnect_sta AllFh
}

echo ========apmode iface check ====== >/dev/console

if [ "$INTERFACE" = "$apmode_interface" -a "$DEVICE" = "$apmode_iface" ]; then
		if [ "$apmode_proto" = "dhcp" ]; then
			json_load "`cat /tmp/cache/network/dhcp.conf`"
			json_get_var ip_new "ip"
			if [ -f "/tmp/cache/network/dhcp.conf.old" ]; then
				json_load "`cat /tmp/cache/network/dhcp.conf.old`"
				json_get_var ip_old "ip"
			else
				ip_old=$ip_new
			fi
			
			echo $ip_new to $ip_old >/dev/console
			ip_new=`echo $ip_new | cut -d'.' -f 1-3`
			ip_old=`echo $ip_old | cut -d'.' -f 1-3`
			
			echo [AP IP Check]: apmode IP update check from $ip_old to $ip_new !!! >/dev/console
			if [ "$ip_new" = "$ip_old" ]; then
				echo "[AP IP Check]: ip not update, don't need to estat wifi & down/up LAN pots" >/dev/console
				exit
			fi
			
			/sbin/eth_handle restart &

			if [ ! -f /tmp/boot_status ]; then
				echo "[AP IP Check]: Restarting wifi ..." > /dev/console
				wieless_restart_cmd &
			else
				echo "[AP IP Check]: No need restart wifi ..." > /dev/console
			fi
		fi
fi

