#!/bin/sh /etc/rc.common
# Copyright (C) 2006-2011 OpenWrt.org

START=10
STOP=98

boot() {
	local commit_flag=0

	/etc/init.d/mount-ubi start
#	[ -f /proc/mounts ] || /sbin/mount_root
	[ -f /proc/jffs2_bbc ] && echo "S" > /proc/jffs2_bbc
	[ -f /proc/net/vlan/config ] && vconfig set_name_type DEV_PLUS_VID_NO_PAD

	# Disable Kernel Hotplug Call 
	[ -f /proc/sys/kernel/hotplug ] && echo "" > /proc/sys/kernel/hotplug

	mkdir -p /var/run
	mkdir -p /var/log
	mkdir -p /var/lock
	mkdir -p /var/state
	mkdir -p /tmp/.jail
	touch /var/log/wtmp
	touch /var/log/lastlog
	touch /tmp/resolv.conf
	ln -s /usr/sbin/rmmod /sbin/rmmod
	[ "$FAILSAFE" = "true" ] && touch /tmp/.failsafe
	echo 1 > /tmp/boot_status

	# Check the boarddata1 and boarddata2 data integrity
	/usr/sbin/backup_restore_partition.sh boarddata1 boarddata2

	# datalib must starts up first
	ifconfig lo up
	/bin/datalib

	# generate conf for rax70/rax78
	/sbin/generate_board_conf.sh

	# generate wifi conf
	/sbin/wifi_init

	# Enable collect console in boot script
	[ "$($CONFIG get debug_boot_up)" = "1" ] && /etc/init.d/dni-debug.init start &

	# preset hostname
	echo "$(/bin/config get netbiosname)" > /proc/sys/kernel/hostname
	/sbin/printhosts

	#start watchdog
	[ ! -f /bin/wdtctl ] && watchdog -t 5 /dev/watchdog &

	# init gpio
	/sbin/init-gpio

	if [ -f /lib/ufsd/ufsd.ko ]; then
		insmod /lib/ufsd/jnl.ko
		insmod /lib/ufsd/ufsd.ko
	fi

	# Tmp to support FTP via Internet Feature
	insmod /lib/modules/4.1.52/kernel/net/netfilter/nf_conntrack_ftp.ko
	insmod /lib/modules/4.1.52/kernel/net/netfilter/nf_nat_ftp.ko

	#/sbin/wifi detect > /tmp/wireless.tmp
	#[ -s /tmp/wireless.tmp ] && {
#		cat /tmp/wireless.tmp >> /etc/config/wireless
#	}
#	rm -f /tmp/wireless.tmp

	# For USB Setting
	[ -d /tmp/samba ] || mkdir -p /tmp/samba
	[ -d /tmp/usbdbg_log ] || mkdir -p /tmp/usbdbg_log
	[ -d /tmp/private ] || mkdir -p /tmp/private
	[ -d /tmp/config ] || mkdir -p /tmp/config
	[ -d /mnt ] || mkdir -p /mnt

	rm -rf /etc/samba
	ln -s /tmp/samba /etc/samba
	rm -rf /usr/private
	ln -s /tmp/private /usr/private
	rm -rf /usr/lib/smb.conf
	ln -s /tmp/samba/smb.conf /usr/lib/smb.conf
	rm -rf /etc/gshadow
	ln -s /tmp/config/gshadow /etc/gshadow
	rm -rf /home
	ln -s /tmp /home
	rm -rf /etc/passwd
	ln -s /tmp/config/passwd /etc/passwd
	rm -rf /etc/passwd+
	ln -s /tmp/config/passwd+ /etc/passwd+
	rm -rf /etc/group
	ln -s /tmp/config/group /etc/group
	rm -rf /etc/shadow
	ln -s /tmp/config/shadow /etc/shadow
	rm -rf /tmp/mnt
	ln -s /mnt /tmp/mnt

	killall -q hotplug2
	[ -x /sbin/hotplug2 ] && /sbin/hotplug2 --override --persistent \
		--set-rules-file /etc/hotplug2.rules \
		--set-coldplug-cmd /sbin/udevtrigger \
		--max-children 4 >/dev/null 2>&1 &

	# create /dev/root if it doesn't exist
	[ -e /dev/root -o -h /dev/root ] || {
		rootdev=$(awk 'BEGIN { RS=" "; FS="="; } $1 == "root" { print $2 }' < /proc/cmdline)
		[ -n "$rootdev" ] && ln -s "$rootdev" /dev/root
	}

	# generate the firewall rule file after loading the configuration data.
	/usr/sbin/net-wall rule

	# start SIP ALG module
	/sbin/cmd_sipalg

	# indicate the system first boot for first log
	$CONFIG set syslog_up_first=0

	# Load Default QoS rules if needed.
	qos_dft="$(/bin/config get qos_list_default)"
	if [ "$qos_dft" = "1" ]; then
		count=1
		while :
		do
			qos_rule="$(/bin/config get qos_dft_list$count)"
			if [ "x$qos_rule" = "x" ]; then
				break;
			fi
			/bin/config set qos_list$count="$qos_rule"

			count=`expr $count + 1`
		done

		count=`expr $count - 1`
		echo "$count QoS default rules are Loaded!"

		/bin/config set qos_list_default="0"
		commit_flag=1
	fi

	# fetch language to /tmp/lang_file
	mtdn=`grep language /proc/mtd | awk -F ':' '{print $1}' | awk -F 'd' '{print $2}'`
	nanddump /dev/mtd$mtdn -f /tmp/lang_file &
	#read wpspin from flash to /tmp/wpspin
	/sbin/artmtd -r wpspin
	#read serial number from flash to /tmp/Seria_Number
	/sbin/artmtd -r sn
	#read wan mac from flash to /tmp/wan_mac
	/sbin/artmtd -r mac
	#read hw id from flash to /tmp/board_hw_id
	/sbin/artmtd -r board_hw_id | cut -f 2 -d ':' > /tmp/board_hw_id
	#read model id from flash to /tmp/board_model_id
	/sbin/artmtd -r board_model_id | cut -f 2 -d ":" > /tmp/board_model_id
	# Check the Board Data region and reset Time Zone & Wirless Region & GUI Region.
	if [ "x$(/bin/config get board_region_default)" = "x1" ]; then
		region="$(/sbin/artmtd -r region | grep REGION | awk '{print $2}')"

		/bin/config set endis_wl_radio="1"
		/bin/config set endis_wla_radio="1"
		/bin/config set endis_wla_2nd_radio="1"

		case "$region" in
		#when region setting on flash's board data area is RU
		RU)
			/bin/config set wla_country="19"
			/bin/config set wl_country="19"
			/bin/config set wla_2nd_country="19"
			/bin/config set ntp_server="GMT-4"
			/bin/config set ntpserver_select="GMT-4"
			/bin/config set ntp_hidden_select="27"
			/bin/config set time_zone="GMT-4"
			/bin/config set email_ntpserver="GMT-4"
			/bin/config set region_flag="DISABLED"
			;;
		#when region setting on flash's board data area is GR
		GR)
			/bin/config set wla_country="4"
			/bin/config set wl_country="4"
			/bin/config set wla_2nd_country="4"
			/bin/config set ntp_server="GMT-1"
			/bin/config set ntpserver_select="GMT-1"
			/bin/config set ntp_hidden_select="19"
			/bin/config set time_zone="GMT-1"
			/bin/config set email_ntpserver="GMT-1"
			;;
		#when region setting on flash's board data area is PR
		PR)
			/bin/config set wla_country="11"
			/bin/config set wl_country="11"
			/bin/config set wla_2nd_country="11"
			/bin/config set ntp_server="GMT-8"
			/bin/config set ntpserver_select="GMT-8"
			/bin/config set ntp_hidden_select="33"
			/bin/config set time_zone="GMT-8"
			/bin/config set email_ntpserver="GMT-8"
			/bin/config set wla_2nd_hidden_channel="153"
			;;
		#when region setting on flash's board data area is BZ
		BZ)
			/bin/config set wla_country="9"
			/bin/config set wl_country="9"
			/bin/config set wla_2nd_country="9"
			/bin/config set ntp_server="GMT+3"
			/bin/config set ntpserver_select="GMT+3"
			/bin/config set ntp_hidden_select="14"
			/bin/config set time_zone="GMT+3"
			/bin/config set email_ntpserver="GMT+3"
			;;
		#when region setting on flash's board data area is IN
		IN)
			/bin/config set wla_country="12"
			/bin/config set wl_country="12"
			/bin/config set wla_2nd_country="12"
			/bin/config set ntp_server="GMT-5:30"
			/bin/config set ntpserver_select="GMT-5:30"
			/bin/config set ntp_hidden_select="30"
			/bin/config set time_zone="GMT-5:30"
			/bin/config set email_ntpserver="GMT-5:30"
			;;
		#when region setting on flash's board data area is KO
		KO)
			/bin/config set wla_country="7"
			/bin/config set wl_country="7"
			/bin/config set wla_2nd_country="7"
			/bin/config set ntp_server="GMT-9"
			/bin/config set ntpserver_select="GMT-9"
			/bin/config set ntp_hidden_select="35"
			/bin/config set time_zone="GMT-9"
			/bin/config set email_ntpserver="GMT-9"
			;;
		#when region setting on flash's board data area is JP
		JP)
			/bin/config set wla_country="6"
			/bin/config set wl_country="6"
			/bin/config set wla_2nd_country="6"
			/bin/config set ntp_server="GMT-9"
			/bin/config set ntpserver_select="GMT-9"
			/bin/config set ntp_hidden_select="35"
			/bin/config set time_zone="GMT-9"
			/bin/config set email_ntpserver="GMT-9"
			/bin/config set region_flag="DISABLED"
			;;
		#when region setting on flash's board data area is NA
		NA|US)
			/bin/config set wla_country="3"
			/bin/config set wl_country="3"
			/bin/config set wla_2nd_country="3"
			/bin/config set wla_2nd_hidden_channel="153"
			if [ "$(/bin/config get radio_number)" = "0x3" ]; then
				/bin/config set wla_hidden_channel="153"
			else
				/bin/config set wla_hidden_channel="44"
			fi
			/bin/config set region_flag="DISABLED"
			/bin/config set ntp_server="GMT+8"
			/bin/config set ntpserver_select="GMT+8"
			/bin/config set ntp_hidden_select="4"
			/bin/config set time_zone="GMT+8"
			/bin/config set email_ntpserver="GMT+8"
			;;
		#when region setting on flash's board data area is AU
		AU)
			/bin/config set wla_country="2"
			/bin/config set wl_country="2"
			/bin/config set wla_2nd_country="2"
			/bin/config set ntp_server="GMT-10"
			/bin/config set ntpserver_select="GMT-10"
			/bin/config set ntp_hidden_select="37"
			/bin/config set time_zone="GMT-10"
			/bin/config set email_ntpserver="GMT-10"
			;;
		#when region setting on flash's board data area is CA
		CA)
			/bin/config set wla_country="3"
			/bin/config set wl_country="3"
			/bin/config set wla_2nd_country="3"
			/bin/config set wla_hidden_channel="153"
			/bin/config set ntp_server="GMT+5"
			/bin/config set ntpserver_select="GMT+5"
			/bin/config set ntp_hidden_select="10"
			/bin/config set time_zone="GMT+5"
			/bin/config set email_ntpserver="GMT+5"
			;;
		WW)
			/bin/config set wla_country="4"
			/bin/config set wl_country="4"
			/bin/config set wla_2nd_country="4"
			/bin/config set wla_hidden_channel="44"
			/bin/config set ntp_server="GMT-0"
			/bin/config set netserver_select="GMT-0"
			/bin/config set time_zone="GMT-0"
			/bin/config set email_ntpserver="GMT-0"
			;;
		AP)
			/bin/config set wla_country="4"
			/bin/config set wl_country="4"
			/bin/config set wla_2nd_country="4"
			/bin/config set wla_hidden_channel="44"
			/bin/config set ntp_server="GMT-8"
			/bin/config set ntpserver_select="GMT-8"
			/bin/config set ntp_hidden_select="33"
			/bin/config set time_zone="GMT-8"
			/bin/config set email_ntpserver="GMT-8"
			/bin/config set ap_region_default="1"
			;;
		PA)
			/bin/config set wla_country="3"
			/bin/config set wl_country="3"
			/bin/config set wla_2nd_country="3"
			/bin/config set wla_hidden_channel="153"
			/bin/config set ntp_server="GMT+8"
			/bin/config set ntpserver_select="GMT+8"
			/bin/config set ntp_hidden_select="4"
			/bin/config set time_zone="GMT+8"
			/bin/config set email_ntpserver="GMT+8"
			;;
		*)
			/bin/config set wla_country="10"
			/bin/config set wl_country="10"
			/bin/config set wla_2nd_country="10"
			/bin/config set wla_2nd_hidden_channel="153"
			/bin/config set region_flag="DISABLED"
			/bin/config set ntp_server="GMT+8"
			/bin/config set ntpserver_select="GMT+8"
			/bin/config set ntp_hidden_select="4"
			/bin/config set time_zone="GMT+8"
			/bin/config set email_ntpserver="GMT+8"
			#If firmware can not recornize
			#/bin/echo "The firmware can not recognize the Region in flash!" > /dev/console
			#/bin/config set endis_wl_radio="0"
			#/bin/config set endis_wla_radio="0"
			#/bin/echo "Disable wireless function!" > /dev/console
			#/bin/config set wla_country="-1" 
			#/bin/config set wl_country="-1"
			#/bin/config set ntp_server="GMT-0"
			#/bin/config set netserver_select="GMT-0"
			#/bin/config set time_zone="GMT-0"
			#/bin/config set email_ntpserver="GMT-0"
			;;
		esac

		if [ "x$(/bin/cat /module_name)" == "xRAX36S" ] || [ "x$(/bin/cat /module_name)" == "xRAX36" ]; then
			/bin/config set wla_ht160=1
			/bin/config set wla_htmode=HT160
			/bin/config set wla_simple_mode=10
		fi

		/bin/config set board_region_default="0"
		commit_flag=1
	fi

	# Wireless security pre-set
	id_dft="$(/bin/config get default_ssphrase)"
	if [ "$id_dft" = "1" ]; then
		/sbin/artmtd -r ssid
		/sbin/artmtd -r passphrase

		if [ -s /tmp/ssid-setted ] && [ -s /tmp/passphrase-setted ]; then
			id_set=$(awk '{print $1}' /tmp/ssid-setted)
			ps_set=$(awk '{print $1}' /tmp/passphrase-setted)

			/bin/config set wl_ssid="$id_set"
			/bin/config set wla_ssid="$id_set-5G"
			/bin/config set wla_2nd_ssid="$id_set-5G2"
			/bin/config set wl_wpa2_psk="$ps_set"
			/bin/config set wla_wpa2_psk="$ps_set"
			/bin/config set wla_2nd_wpa2_psk="$ps_set"
			/bin/config set wl_sectype="4"
			/bin/config set wla_sectype="4"
			/bin/config set wla_2nd_sectype="4"
			/bin/config set wps_status="5"
			/bin/config set wla_wps_status="5"
			/bin/config set wla_2nd_wps_status="5"

			# set SSID of guest netwroks according to Home Router GUI Redesign Specification Rev10 section 6.3
			/bin/config set wlg1_ssid="NETGEAR-Guest"
			/bin/config set wla1_ssid="NETGEAR-5G-Guest"
			/bin/config set wla1_2nd_ssid="NETGEAR-5G2-Guest"
			/bin/config set wlg1_sectype="4"
			/bin/config set wla1_sectype="4"
			/bin/config set wla1_2nd_sectype="4"
		fi
		/bin/config set default_ssphrase="0"
		commit_flag=1
	fi

	# generate supported channels for each region
	sh /usr/sbin/gen_wifi_channel_info.sh setup &

	[ "$commit_flag" = "1" ] && /bin/config commit
	
	#Enable factory test mode
	if [ "x$(/bin/config get factory_mode)" = "x1" ]; then
		/bin/config set endis_wl_radio="1"
		/bin/config set endis_wla_radio="1"
		/bin/config set endis_wla_2nd_radio="1"
		/bin/touch /tmp/factory_test
		/bin/config set led_blinking_setting="2"
		echo "reset" > /proc/simple_config/button_test
	fi

	# Disable USB power supply
	echo 0 > /proc/simple_config/usb5v_0
	echo 0 > /proc/simple_config/usb5v_1
	
	module_name=`/bin/cat /module_name`
	if [ "$(/bin/config get radio_number)" != "0x3" ]; then
		hw_revision=$(artmtd -r hw_revision | awk '{print $2}' )
		if [ "$hw_revision" = "01" ] ;then
			cp -f /etc/wlan/nvram/FEM2nd.nvm /etc/wlan/nvram/"$module_name".nvm
		fi
	fi
	cp /etc/wlan/nvram/"$module_name".nvm /etc/wlan/nvram/kernel.nvm
	mkdir /tmp/nvram
	insmod /lib/modules/*/extra/wlcsm.ko
	/bin/calibration_tool setup
    # download AQR firmware to AQR phy
#	aq-fw-download /lib/firmware/AQR_phy_firmware miireg 7
#	sleep 1
#	ssdk_sh debug phy set 7 0x4004c441 0x8
	echo "root:!:0:0:root:/tmp:/bin/ash" >> /tmp/config/passwd
	
	#for ping localhost successfully
	[ -f /etc/nsswitch.conf ] || echo "hosts: files dns" > /etc/nsswitch.conf

#Check config file
#
#If remote management is enabled → set NVram value “remotemgmt = 1” → keeps function work and show GUI page 
#
#If remote management is disabled
#-> set Nvram value “remotemgmt = 0”
#-> remove GUI page (req1)
#
#*default set Nvram value “remotemgmt = 0”
#remote_endis

	if [ "$(/bin/config get remote_endis)" = "1" ];then
		/bin/config set remotemgmt=1
	else
		/bin/config set remotemgmt=0
	fi

}

start()
{
	boot
}
