#!/bin/sh /etc/rc.common
START=21
. /lib/cfgmgr/cfgmgr.sh
. /lib/cfgmgr/opmode.sh

vlan_iptv_wl_enable()
{
	local i
	local value
	local wl

	local vlan_iptv_enable=$($CONFIG get i_opmode)

	case $vlan_iptv_enable in
	vlan)
		for i in 1 2 3 4 5 6 7 8 9 10; do
			value=$($CONFIG get vlan_tag_$i)
			if [ "x$value" = "x" ] ;then
				break
			fi
			wl=`echo $value|awk '{print $6}'`
			[ "x$wl" != "x0" ] && return 0
		done
		;;
	iptv)
		[ "$($CONFIG get wan_brig_ssid1)" = "1" ] && return 0
		[ "$($CONFIG get wan_brig_ssid2)" = "1" ] && return 0
		[ "$($CONFIG get wan_brig_guest_ssid1)" = "1" ] && return 0
		[ "$($CONFIG get wan_brig_guest_ssid2)" = "1" ] && return 0

	;;
	*) return 1 ;;
	esac
	return 1
}

bridge_stop_nss()
{
	case "$($CONFIG get i_opmode)" in
	apmode|extmode)
		return 0
		;;
	*)
		return 1
		;;
	esac
}

start_stage0() # $1: boot/start
{
	op_set_induced_configs
	op_create_brs_and_vifs all

	#For AP mode and Bridge mode turn off access control
	local opmode=$($CONFIG get i_opmode)
	if [ "$opmode" = "brmode" ]; then
		$CONFIG set enable_block_device=0
	else
		[ "x$($CONFIG get old_enable_acl_status)" != "x" ] && $CONFIG set enable_block_device=$($CONFIG get old_enable_acl_status)
	fi

	#For AP mode circled no start
	if [ "$1" != "boot" ]; then
		circle_version=`/usr/bin/get_circle_status circle_version`
		if [ "$circle_version" = "2" ];then
			spc_enable=`/usr/bin/get_circle_status activate`
			gui_acl=`/bin/config get enable_block_device`
			acl_change=0
			if [ "$opmode" = "apmode"  -o "$opmode" = "brmode" ]; then
				d2 -c general[0].RouterMode ap
				if [ "$spc_enable" = "1" -a "$gui_acl" != "1" ]; then
					/bin/config set enable_block_device=1
					acl_change=1
				fi
			else
				d2 -c general[0].RouterMode router
				if [ "$spc_enable" = "1" -a "$gui_acl" != "0" ]; then
					/bin/config set enable_block_device=0
					acl_change=1
				fi
			fi
			echo "[opmode]The spc status is $spc_enable, acl change is $acl_change!!!" > /dev/console
			if [ "$acl_change" = "1" ]; then
				/etc/init.d/acl restart > /dev/console
			fi
		fi
	fi

	if [ "$1" != "boot" ]; then
		if [ "$opmode" = "apmode" -o "$opmode" = "brmode" ]; then
			/etc/init.d/ASH stop
			rm /tmp/check_bdagent
			/opt/bitdefender/bin/bd stop
			rmmod guster.ko
		else
			/opt/bitdefender/bin/bd start
			touch /tmp/check_bdagent
			/etc/init.d/ASH start
			insmod /lib/modules/4.1.52/extra/guster.ko
		fi
	fi

	/etc/init.d/acl restart


	case "$($CONFIG get i_opmode)" in
	normal|iptv|vlan)
		if [ "$1" = "start" ]; then
			/sbin/cmd_traffic_meter restart
		fi
		/etc/init.d/net-lan $1 mode
		if [ "$1" = "start" ]; then
			/etc/init.d/net-wan start_for_opmode
		else
			/etc/init.d/net-wan $1 mode
		fi
		;;
	*)
		/etc/init.d/net-br $1
		;;
	esac

	if vlan_iptv_wl_enable || bridge_stop_nss; then
		/etc/init.d/qca-nss-ecm stop &
	fi
	ethswctl -c pause -n 0 -p 0 -v 2 >/dev/null 
	ethswctl -c pause -n 0 -p 5 -v 2 >/dev/null 
	ethswctl -c pause -n 1 -p 0 -v 2 >/dev/null 
	ethswctl -c pause -n 1 -p 1 -v 2 >/dev/null 
	ethswctl -c pause -n 1 -p 2 -v 2 >/dev/null 
	ethswctl -c pause -n 1 -p 3 -v 2 >/dev/null 
	ethswctl -c pause -n 1 -p 5 -v 2 >/dev/null 
	ethswctl -c pause -n 1 -p 8 -v 2 >/dev/null 
}

stop0()
{
	case "$($CONFIG get i_opmode)" in
	normal|iptv|vlan)
		/etc/init.d/init6 stop
		/etc/init.d/net-wan stop_for_opmode
		/etc/init.d/net-lan stop
		;;
	*)
		/etc/init.d/net-br stop
		;;
	esac
	op_del_brs_vifs all
}

start_ping_netgear()
{
	if [ "x$(/bin/config get factory_mode)" = "x1" ]; then
		/bin/rm /sbin/ping-netgear
		echo "in factory mode" > /dev/console
	else
		sleep 10 && /sbin/ping-netgear &
	fi
}

boot()
{
	op_create_br0_brwan
	start_stage0 boot
}

start()
{
	if [ -f /tmp/boot_status ]; then
		boot
		start_ping_netgear
		return
	fi
	start_stage0 start
}

stop()
{
	/etc/init.d/wlan-common stop
	stop0
}

restart()
{
	stop
	start
}

