#!/bin/sh /etc/rc.common
START=30

flash_ecm_record() {
	if [ -f /tmp/orbi_type ]; then
		# for Orbi series
		return
	elif [ -f /sys/kernel/debug/ecm/ecm_db/defunct_all ]; then
		# for RAX120 project
		echo 1 > /sys/kernel/debug/ecm/ecm_db/defunct_all
	elif [ -x /bin/fc ]; then
		# for RAX70, RAX10 etc BCM project
		/bin/fc flush
	fi
}

start() {
	if [ -f /tmp/boot_status ]; then
		PS=`ps |grep acld | grep -v grep | grep -v killall`
		if [ "x$PS" = "x" ]; then
			acld
		fi
		PS=`ps |grep aclhijackdns | grep -v grep`
		if [ "x$PS" = "x" ]; then
			aclhijackdns &
		fi
	fi

	[ "$($CONFIG get i_opmode)" != "brmode" ] && $CONFIG set old_enable_acl_status=$($CONFIG get enable_block_device)

	if [ "x$($CONFIG get enable_block_device)" = "x" ]; then
		echo "No valid configuration for NETGEAR blocking function, please reset to factory default!"
		exit 0
	fi

	if [ "x$($CONFIG get enable_block_device)" = "x0" ]; then
		aclctl status off
		exit 0
	fi

	if [ "x$($CONFIG get new_device_statue_by_default)" = "xAllow" ]; then
		aclctl type allow
		num=1
		while [ 1 ]; do
			ACCESS_CONTROL=$($CONFIG get access_control$num)
			type_value=`echo $ACCESS_CONTROL | cut -f 1 -d ' '`
			mac=`echo $ACCESS_CONTROL | cut -f 2 -d ' '`
			if [ "x$mac" = "x" ]; then
				break
			fi
			if [ "$type_value" = "1" ]; then
				aclctl add $mac
			fi
			num=$(($num + 1))
		done
	else
		aclctl type block
		num=1
		while [ 1 ]; do
			ACCESS_CONTROL=$($CONFIG get access_control$num)
			type_value=`echo $ACCESS_CONTROL | cut -f 1 -d ' '`
			mac=`echo $ACCESS_CONTROL | cut -f 2 -d ' '`
			if [ "x$mac" = "x" ]; then
				break
			fi
			if [ "$type_value" = "0" ]; then
				aclctl add $mac
			fi
			num=$(($num + 1))
		done
	fi
	aclctl status on

	#flash ecm contrack and record to fix can't block QQ,WeChat and Skype issue
	flash_ecm_record
}

boot() {
	acld
	aclhijackdns &
	start
}

stop() {
	aclctl status off
	aclctl clean
}

restart() {
	killall -SIGUSR1 acld
	stop
	start
}
