#!/bin/sh /etc/rc.common

CONFIG="/bin/config"
flash_type="$($CONFIG get dgc_flash_type)"
orbi_project=`cat /tmp/orbi_project 2>/dev/null`

#EMMC flash data
devtable_mtd_dev=$(part_dev device_table 2>/dev/null)

#nond flash data
ubidev=$(ubinfo -a | grep -B5 vol_devtable | sed -nr 's/Volume ID:.*\(on (ubi.*)\)/\1/p')
LOCAL_DEVICE_TABLE=/tmp/device_tables/local_device_table

copy_devicetables_files()
{
	[ ! -e /tmp/device_tables/oui.csv ] && mkdir -p /tmp/device_tables
	[ ! -e /tmp/device_tables/oui.csv ] && cp /etc/oui.csv /tmp/device_tables/
	[ ! -e /tmp/device_tables/mam.csv ] && cp /etc/mam.csv /tmp/device_tables/
	[ ! -e /tmp/device_tables/oui36.csv ] && cp /etc/oui36.csv /tmp/device_tables/
	[ ! -e /tmp/device_tables/UA_String_Match_Table.txt ] && cp /etc/UA_String_Match_Table.txt /tmp/device_tables/
	[ ! -e /tmp/device_tables/device_type_table.json ] && cp /etc/device_type_table.json /tmp/device_tables/
	[ ! -e /tmp/device_tables/local_device_table ] && touch /tmp/device_tables/local_device_table
}

mount_device_tbales_flash()
{
	local flag=0
	[ "$flash_type" = "" ] && return
	if [ "x$flash_type" == "xEMMC" ];then
		if [ "x$(df |grep $devtable_mtd_dev)" = "x" ]; then
			[ ! -d /tmp/device_tables ] && mkdir -p /tmp/device_tables
			mount $devtable_mtd_dev /tmp/device_tables -t ext4
			if [ $? -ne 0 ]; then
				mkfs.ext4 $devtable_mtd_dev
				mount $devtable_mtd_dev /tmp/device_tables -t ext4
				[ $? -eq 0 ] && flag=1 || flag=0
			else
				flag=1
			fi
		fi
	else
		if [ "x$(ubinfo -a|grep vol_ntgrdata)" != "x" ]; then
			ln -s /tmp/ntgrdata/devtable /tmp/device_tables
		elif [ "x$(df |grep $ubidev:vol_devtable)" = "x" ]; then
			[ ! -d /tmp/device_tables ] && mkdir -p /tmp/device_tables
			mount $ubidev:vol_devtable /tmp/device_tables -t ubifs
			if [ $? -ne 0 ]; then
				mount $ubidev:vol_devtable /tmp/device_tables -t ubifs
				[ $? -eq 0 ] && flag=1 || flag=0
			else
				flag=1
			fi

			check_file_format

		fi
	fi
}

bcm_boot()
{
	if [ "x$flash_type" == "xEMMC" ];then
		local flag
		if [ "x$(df |grep $devtable_mtd_dev)" = "x" ]; then
			[ ! -d /tmp/device_tables ] && mkdir -p /tmp/device_tables
			mount $devtable_mtd_dev /tmp/device_tables -t ext4
			if [ $? -ne 0 ]; then
				mkfs.ext4 $devtable_mtd_dev
				mount $devtable_mtd_dev /tmp/device_tables -t ext4
				[ $? -eq 0 ] && flag=1 || flag=0
			else
				flag=1
			fi
			copy_devicetables_files 
		fi
	fi

	maintain_acl_conf
	check_file_format
	set_dal_info_again
	PS=`ps |grep check_dal_acl.sh | grep -v grep`
	[ "x$PS" = "x" ] && /usr/sbin/check_dal_acl.sh &
}

start()
{
	if [ -f /tmp/boot_status ]; then
		bcm_boot
	fi

	mkdir -p /tmp/netscan
	touch /tmp/device_user_defined
	echo 1 > /tmp/netscan/support_fing
	mount_device_tbales_flash
	/etc/init.d/netscan_if.init
	copy_devicetables_files 
	PS=`ps |grep fing-devices | grep -v grep`
	[ "x$PS" != "x" ] && killall fing-devices
	PS=`ps |grep net-scan | grep -v grep|grep -v etc`
	[ "x$PS" != "x" ] && killall -9 net-scan
	/usr/sbin/net-scan
	/usr/sbin/fing-devices 2>/dev/null
}

stop()
{
	killall -9 net-scan
	killall fing-devices
	if [ "x$flash_type" == "xEMMC" ];then
		umount $devtable_mtd_dev 2>/dev/null
	else
		umount /tmp/device_tables 2>/dev/null
	fi
}

reload()
{
	cp /tmp/netscan_if_conf /tmp/netscan_if_conf.old
	cp /tmp/netscan_if_typechar_name /tmp/netscan_if_typechar_name.old
	/etc/init.d/netscan_if.init
	conf_change=`diff -ruN /tmp/netscan_if_conf.old /tmp/netscan_if_conf`
	name_change=`diff -ruN /tmp/netscan_if_typechar_name.old /tmp/netscan_if_typechar_name`
	if [ "$conf_change" != "" ] || [ "$name_change" != "" ]; then
		stop
		start
	fi
	rm -rf /tmp/netscan_if_conf.old /tmp/netscan_if_typechar_name.old 
}

enable()
{
	if [ "$1" = "start" ]; then
		/usr/bin/killall -SIGUSR1 net-scan
		sleep 1
	elif [ "$1" = "refresh" ]; then
		/usr/bin/killall -SIGUSR1 net-scan && sleep 1 && /usr/bin/killall -SIGUSR2 net-scan &
	fi
}

set_dal_info_again()
{
	#set some default value
	model=`cat /module_name`
	firmware=`cat /firmware_version`
	d2 -c general.devicemodel $model
	d2 -c general.fwrevision "$firmware"
	d2 -c dalmoduleversion.RELEASEVersion "`cat /etc/version.txt`"
	d2 -c general.fsn "`artmtd -r sn|head -1 |cut -d ':' -f 2-`"
	d2 -c general.defaultmac "`artmtd -r mac | grep "lan mac" | awk '{print $3}'`"
	d2 -c general.wan_ifname         "$(/bin/config get wan_ifname)"
	d2 -c general.lan_ifname         "$(/bin/config get lan_ifname)"
}

boot()
{
	if [ "x$flash_type" == "xEMMC" ];then
		local flag
		if [ "x$(df |grep $devtable_mtd_dev)" = "x" ]; then
			[ ! -d /tmp/device_tables ] && mkdir -p /tmp/device_tables
			mount $devtable_mtd_dev /tmp/device_tables -t ext4
			if [ $? -ne 0 ]; then
				mkfs.ext4 $devtable_mtd_dev
				mount $devtable_mtd_dev /tmp/device_tables -t ext4
				[ $? -eq 0 ] && flag=1 || flag=0
			else
				flag=1
			fi
			copy_devicetables_files 
		fi
	fi

	maintain_acl_conf
	check_file_format
	set_dal_info_again
	PS=`ps |grep check_dal_acl.sh | grep -v grep`
	[ "x$PS" = "x" ] && /usr/sbin/check_dal_acl.sh &
	start
}

check_file_format()
{
	[ ! -e $LOCAL_DEVICE_TABLE ] && return

	while read line
	do
		var=`echo $line | awk -F "," '{printf $2}'`
		var1=`echo $line | awk -F "," '{printf $8}'`
		if [ "$(echo $var | bc 2>/dev/null)" != "$var" -o "x$var1" = "x" ]; then
			rm -f $LOCAL_DEVICE_TABLE
			touch $LOCAL_DEVICE_TABLE
			break
		fi
	done < $LOCAL_DEVICE_TABLE

}

maintain_acl_conf()
{
	flag=false
	num=1
	var=access_control_value

	while [ "x$var" != "x" ]
	do
		var=`$CONFIG get access_control$num`
		echo $var >> /tmp/acl_file1
		num=$(($num + 1))
	done

	awk '!x[$2]++' /tmp/acl_file1 >> /tmp/acl_file2

	num=1
	count_1=`cat /tmp/acl_file1 | wc -l`
	count_2=`cat /tmp/acl_file2 | wc -l`
	if [ $count_1 -ne $count_2 ]; then
		flag=true
		while [ $num -ne $count_2 ]
		do
			$CONFIG unset access_control$num
			num=$(($num +1 ))
		done
	fi

	if [ $flag ]; then
		num=1
		while read line
		do
			if [ ${#line} -gt 32 ]; then
				$CONFIG set access_control$num="$line"
				num=$(($num + 1))
			fi
			if [ $num -eq 254 ]; then
				break;
			fi
		done < /tmp/acl_file2
	fi

	num_t=254
	num=1
	while [ $num_t -lt 3000 ]
	do
		var=`$CONFIG get access_control$num_t`
		if [ "x$var" != "x" ]; then
			while [ $num -lt $num_t ]
			do
				$CONFIG unset access_control$num
				num=$(($num + 1))
			done
		fi
		num_t=$(($num_t + 100))
	done
	rm -f /tmp/acl_file1 /tmp/acl_file2
}
