#!/bin/sh /etc/rc.common
# Copyright (C) 2022 Li Wenbo

START=20

DEVICE_INFO_PATH="/tmp/device-info.json"
WLAN_CFG_PATH="/etc/wlan"
WLAN_COM_CFG_PATH=${WLAN_CFG_PATH}"/Dev_Common"
WLAN_PRI_CFG_PATH=${WLAN_CFG_PATH}"/Dev_Private"
WLAN_PRI_CFGS=" productinfo.bin   
				radio_config.json "
WLAN_PRI_REGIONCFGS="
				dev_config.json
				mt2g.dat
				mt5g.dat"				
				

load_device_config()
{
	if [ ! -e "${DEVICE_INFO_PATH}" ]; then
		echo "device-info.json does not exit, cannot load device config"
		exit 0
	fi
	#Load the corresponding wlan config according to the model name
	device_info=`cat ${DEVICE_INFO_PATH}`
	product_name=`echo ${device_info} | sed 's/,/\n/g' | grep "product_name" | sed 's/:/\n/g'| sed '1d' |sed 's/[}\" ]//g'`
	product_version=`echo ${device_info} | sed 's/,/\n/g' | grep "product_version" | sed 's/:/\n/g'| sed '1d' |sed 's/[}\" ]//g'`
	product_region=`echo ${device_info} | sed 's/,/\n/g' | grep "region" | sed 's/:/\n/g'| sed '1d' |sed 's/[}\" ]//g'`
	product=`echo ${product_name}_${product_version} | tr [A-Z] [a-z]`

	/* if not set, Use the EU dat file */
	if [ "$product_region" == "UN" ]; then
		product_region="eu"
	fi
	
	wirelessregion=`echo wireless_${product_region} | tr [A-Z] [a-z]`
	
	for file in ${WLAN_PRI_CFGS}
	do
		if [ "${file}" == "productinfo.bin" ]; then
			cp ${WLAN_PRI_CFG_PATH}"/"${product}"/"${file} ${WLAN_CFG_PATH}"/"${file}
		else
			cp ${WLAN_PRI_CFG_PATH}"/"${product}"/"${file} ${WLAN_COM_CFG_PATH}"/"${file}
		fi
	done
	
    for file in ${WLAN_PRI_REGIONCFGS}
    do
        if [ "${file}" == "dev_config.json" ]; then
            cp ${WLAN_PRI_CFG_PATH}"/"${product}"/"${wirelessregion}"/"${file} ${WLAN_COM_CFG_PATH}"/"${file}
        else
            cp ${WLAN_PRI_CFG_PATH}"/"${product}"/"${wirelessregion}"/"${file} ${WLAN_COM_CFG_PATH}/wireless"/"${file}
        fi
    done

	mv ${WLAN_COM_CFG_PATH} ${WLAN_CFG_PATH}"/"${product}
	rm ${WLAN_PRI_CFG_PATH} -rf
}

ip2hex()
{
	ip=$1
	a=`echo $ip | awk -F'.' '{print $1}'`
	b=`echo $ip | awk -F'.' '{print $2}'`
	c=`echo $ip | awk -F'.' '{print $3}'`
	d=`echo $ip | awk -F'.' '{print $4}'`
	printf 0x%02x%02x%02x%02x $a $b $c $d
}

tpdomain_add()
{
	local cfg="$1"
	config_get system "$cfg" system
	
	if [ "$system" == "1" ];then
		config_get ipaddr "$cfg" ipaddr
		ipaddr=`ip2hex $ipaddr`
		insmod tp_domain.ko lan_ip=$ipaddr lan_domain=tplinker.net
	fi
}

insmod_tp_domain()
{
	config_load vnetwork
	config_foreach tpdomain_add vinterface
}

wlan_tplink_start() {

	mkdir -p /var/run/srv
	chmod 777 /var/run/srv
	mkdir -p /var/run/client
	chmod 777 /var/run/client
	mkdir -p /tmp/log/error
	mkdir -p /tmp/log/rec
	mkdir -p /tmp/log/coredump
	mkdir -p /tmp/logdump
	chmod 777 /tmp/logdump
	mkdir -p /tmp/wireless
	mkdir -p /tmp/wireless/wlan_data
	mkdir -p /tmp/wireless/wlan_bk_data
	# mknod /dev/tp_domain c 260 0

	load_device_config

	cp /etc/wlan/nrd.conf /tmp/nrd.conf

	/sbin/mount_wlandata

	[ -f /tmp/wireless/wlan_data/radio ] && cp -f /tmp/wireless/wlan_data/radio /tmp/wireless/caldata.bin
	[ ! -f /tmp/wireless/caldata.bin ] && cp -f /tmp/wireless/wlan_bk_data/radio /tmp/wireless/caldata.bin

	[ -f /tmp/wireless/wlan_data/region ] && cp -f /tmp/wireless/wlan_data/region /tmp/wireless/region
	[ ! -f /tmp/wireless/region ] && cp -f /tmp/wireless/wlan_bk_data/region /tmp/wireless/region

	[ -f /tmp/wireless/wlan_data/wlan_factory_default.bin ] && mv -f /tmp/wireless/wlan_data/wlan_factory_default.bin /tmp/userconfig/etc/config/wlancfg.bin
	[ -f /tmp/wireless/wlan_bk_data/wlan_factory_default.bin ] && mv -f /tmp/wireless/wlan_bk_data/wlan_factory_default.bin /tmp/userconfig/etc/config/wlancfg.bin

	insmod conninfra.ko
	insmod mtkhnat.ko
	insmod mesh.ko
	insmod wlan_portal.ko
	insmod dhcp_capture.ko
	insmod rate_limit.ko
	# insmod_tp_domain

	/usr/sbin/detect >/dev/null &
	/usr/sbin/msg-center >/dev/null &
	/usr/sbin/radius >/dev/null &
	/usr/sbin/auth-time >/dev/null &
	/usr/sbin/rssift >/dev/null &
	/usr/sbin/apscan >/dev/null &
	/usr/sbin/wscheduler >/dev/null &
	/usr/sbin/client-state >/dev/null &
	/usr/sbin/eap-mesh >/dev/null &
	/usr/sbin/cluster-master >/dev/null &
	/usr/sbin/tdpd >/dev/null &
	/usr/sbin/cluster_elect >/dev/null &
	#/usr/sbin/cluster-slave >/dev/null &
	/usr/sbin/tdpcli >/dev/null &
	/usr/sbin/channeldeploy >/dev/null &
	/usr/sbin/uclited >/dev/null &
	/usr/sbin/tpsyslogd >/dev/null &
	/usr/sbin/httpd_portal >/dev/null &
	/usr/sbin/backgroundscan >/dev/null &
}

start() {
	wlan_tplink_start
}

stop() {
	echo "Do not stop wlan_tplink" > /dev/console
}

