#!/bin/sh

DIR=$(cd `dirname $0`; pwd)
var_boardinfo_file="/mnt/jffs2/hw_boardinfo"
var_boardinfo_file_bak="/mnt/jffs2/hw_boardinfo.bak"
var_boardinfo_temp="/mnt/jffs2/hw_boardinfo.temp"

TEMPDATA=$(cat $DIR/fix/customizepara.txt)
SN=${TEMPDATA:0:17}
PASSWD=${TEMPDATA:18:5}
WIFI=${TEMPDATA:24:13}
WIFIPW=${TEMPDATA:38:8}

	rm -rf /mnt/jffs2/choose_xml
	rm -rf /mnt/jffs2/customize
	rm -rf /mnt/jffs2/choose_xml.tar.gz
	rm -rf /mnt/jffs2/customize.txt
	rm -rf /mnt/jffs2/cwmp_rebootsave
	rm -rf /mnt/jffs2/FTCRC
	rm -rf /mnt/jffs2/ftvoipcfgstate
	rm -rf /mnt/jffs2/oldcrc
	rm -rf /mnt/jffs2/recovername
	rm -rf /mnt/jffs2/customizepara.txt
	rm -rf /mnt/jffs2/hw_default_ctree.xml

	tar zxvf $DIR/e8c.tar.gz -C /mnt/jffs2

	cp -f $DIR/fix/customizepara.txt /mnt/jffs2/customizepara.txt
	/bin/aescrypt2 0 /mnt/jffs2/customizepara.txt tmp

	cp -f $DIR/fix/hw_ctree.xml /mnt/jffs2/hw_ctree.xml
	cp -f $DIR/fix/hw_ctree.xml /mnt/jffs2/hw_ctree_bak.xml
	cp -f $DIR/fix/hw_default_ctree.xml /mnt/jffs2/hw_default_ctree.xml

	#decrypt and unzip default_ctree
	cp -f /mnt/jffs2/hw_default_ctree.xml /tmp/test.gz
	/bin/aescrypt2 1 /tmp/test.gz /tmp/test.gz.tmp
	gzip -d /tmp/test.gz

	#set default_ctree
	cfgtool set /tmp/test InternetGatewayDevice.LANDevice.LANDeviceInstance.WLANConfiguration.WLANConfigurationInstance.1 SSID $WIFI
	cfgtool set /tmp/test InternetGatewayDevice.LANDevice.LANDeviceInstance.WLANConfiguration.WLANConfigurationInstance.1.PreSharedKey.PreSharedKeyInstance PreSharedKey $WIFIPW
	cfgtool set /tmp/test InternetGatewayDevice.LANDevice.LANDeviceInstance.WLANConfiguration.WLANConfigurationInstance.5 SSID $WIFI-5G
	cfgtool set /tmp/test InternetGatewayDevice.LANDevice.LANDeviceInstance.WLANConfiguration.WLANConfigurationInstance.5.PreSharedKey.PreSharedKeyInstance PreSharedKey $WIFIPW
	cfgtool set /tmp/test InternetGatewayDevice.Service.StorageService.StorageServiceInstance.UserAccount.UserAccountInstance Password $PASSWD
	cfgtool set /tmp/test InternetGatewayDevice.UserInterface.X_HW_WebUserInfo.X_HW_WebUserInfoInstance.1 Password $PASSWD
	cfgtool set /tmp/test InternetGatewayDevice.X_HW_ServiceManage FtpPassword $PASSWD
	cfgtool set /tmp/test InternetGatewayDevice.X_HW_AppSrvManage.X_HW_AppSrvManageInstance FtpPassword $PASSWD

	#encrypt var_default_ctree
	gzip /tmp/test
	/bin/aescrypt2 0 /tmp/test.gz /tmp/test.gz.tmp
	mv -f /tmp/test.gz /mnt/jffs2/hw_default_ctree.xml

	cat $var_boardinfo_file | while read -r line;
	do
		obj_id_temp=`echo $line | sed 's/\(.*\)obj.value\(.*\)/\1/g'`
		obj_id=`echo $obj_id_temp | sed 's/\(.*\)"\(.*\)"\(.*\)/\2/g'`

		if [ "0x00000001" == $obj_id ];then
			echo "obj.id = \"0x00000001\" ; obj.value = \"4\";"
		elif [ "0x00000003" == $obj_id ];then
			echo "obj.id = \"0x00000003\" ; obj.value = \"\";"
		elif [ "0x00000004" == $obj_id ];then
			echo "obj.id = \"0x00000004\" ; obj.value = \"\";"
		elif [ "0x00000005" == $obj_id ];then
			echo "obj.id = \"0x00000005\" ; obj.value = \"\";"
		elif [ "0x00000006" == $obj_id ];then
			echo "obj.id = \"0x00000006\" ; obj.value = \"\";"
		elif [ "0x00000009" == $obj_id ];then
			echo "obj.id = \"0x00000009\" ; obj.value = \"\";"
		elif [ "0x00000026" == $obj_id ];then
			echo "obj.id = \"0x00000026\" ; obj.value = \"\";"
		elif [ "0x00000027" == $obj_id ];then
			echo "obj.id = \"0x00000027\" ; obj.value = \"\";"
		elif [ "0x00000028" == $obj_id ];then
			echo "obj.id = \"0x00000028\" ; obj.value = \"\";"
		elif [ "0x00000029" == $obj_id ];then
			echo "obj.id = \"0x00000029\" ; obj.value = \"\";"
		elif [ "0x0000002a" == $obj_id ];then
			echo "obj.id = \"0x0000002a\" ; obj.value = \"\";"
		elif [ "0x0000002b" == $obj_id ];then
			echo "obj.id = \"0x0000002b\" ; obj.value = \"\";"
		elif [ "0x0000002c" == $obj_id ];then
			echo "obj.id = \"0x0000002c\" ; obj.value = \"\";"
		elif [ "0x00000016" == $obj_id ];then
			echo "obj.id = \"0x00000016\" ; obj.value = \"\";"
		elif [ "0x00000018" == $obj_id ];then
			echo "obj.id = \"0x00000018\" ; obj.value = \"0\";"
		elif [ "0x00000019" == $obj_id ];then
			echo "obj.id = \"0x00000019\" ; obj.value = \"$SN\";"
		elif [ "0x0000001a" == $obj_id ];then
			echo "obj.id = \"0x0000001a\" ; obj.value = \"E8C\";"
		elif [ "0x0000001b" == $obj_id ];then
			echo "obj.id = \"0x0000001b\" ; obj.value = \"E8C\";"
		elif [ "0x0000001c" == $obj_id ];then
			echo "obj.id = \"0x0000001c\" ; obj.value = \"0\";"
		elif [ "0x0000001d" == $obj_id ];then
			echo "obj.id = \"0x0000001d\" ; obj.value = \"1\";"
		elif [ "0x0000001e" == $obj_id ];then
			echo "obj.id = \"0x0000001e\" ; obj.value = \"1\";"
		elif [ "0x0000001f" == $obj_id ];then
			echo "obj.id = \"0x0000001f\" ; obj.value = \"1\";"
		elif [ "0x00000020" == $obj_id ];then
			echo "obj.id = \"0x00000020\" ; obj.value = \"\";"
		elif [ "0x00000021" == $obj_id ];then
			echo "obj.id = \"0x00000021\" ; obj.value = \"0\";"
		elif [ "0x00000031" == $obj_id ];then
			echo "obj.id = \"0x00000031\" ; obj.value = \"CHOOSE_XINAN\";"
		elif [ "0x00000034" == $obj_id ];then
			echo "obj.id = \"0x00000034\" ; obj.value = \"00:00:00:00:00:0C\";"
		elif [ "0x00000036" == $obj_id ];then
			echo "obj.id = \"0x00000036\" ; obj.value = \"\";"
		elif [ "0x00000037" == $obj_id ];then
			echo "obj.id = \"0x00000037\" ; obj.value = \"\";"
		elif [ "0x00000038" == $obj_id ];then
			echo "obj.id = \"0x00000038\" ; obj.value = \"\";"
		elif [ "0x00000035" == $obj_id ];then
			echo "obj.id = \"0x00000035\" ; obj.value = \"\";"
		elif [ "0x00000039" == $obj_id ];then
			echo "obj.id = \"0x00000039\" ; obj.value = \"0x00102001\";"
		elif [ "0x0000003a" == $obj_id ];then
			echo "obj.id = \"0x0000003a\" ; obj.value = \"@CN#Common&\";"
		elif [ "0x0000003b" == $obj_id ];then
			echo "obj.id = \"0x0000003b\" ; obj.value = \"\";"
		elif [ "0x0000003c" == $obj_id ];then
			echo "obj.id = \"0x0000003c\" ; obj.value = \"0x00102001\";"
		elif [ "0x0000003d" == $obj_id ];then
			echo "obj.id = \"0x0000003d\" ; obj.value = \"0x00000000\";"
		elif [ "0x0000003e" == $obj_id ];then
			echo "obj.id = \"0x0000003e\" ; obj.value = \"0x00000000\";"
		elif [ "0x0000003f" == $obj_id ];then
			echo "obj.id = \"0x0000003f\" ; obj.value = \"0x00000000\";"
		elif [ "0x00000040" == $obj_id ];then
			echo "obj.id = \"0x00000040\" ; obj.value = \"\";"
		elif [ "0x00000041" == $obj_id ];then
			echo "obj.id = \"0x00000041\" ; obj.value = \"\";"
		else
		    echo -E $line
		fi
	done  > $var_boardinfo_temp
	
	cp $var_boardinfo_temp $var_boardinfo_file_bak
	mv $var_boardinfo_temp $var_boardinfo_file

reboot
