#!/bin/sh

script_path=`echo $0 | awk -F '/' '{for(i=2;i<NF;i++) printf"/"$i}'`
echo  $script_path

wireNetwork=`cat $script_path/wireNetwork.ini | ./busybox grep "wireNetwork:" | ./busybox grep -v grep | sed 's/wireNetwork://g' | sed 's/\r//g' | sed 's/\n//g' | sed "s/$(echo -e '\015')//g"`
if [ $? != 0 ]; then
	echo "cann't found wireNetwork in config file"
	exit -1
fi
IP=`cat $script_path/wireNetwork.ini  | ./busybox grep -m 1 "IP:" | ./busybox grep -v grep | sed 's/IP://g' | sed 's/\r//g' | sed 's/\n//g'`
if [ $? != 0 ]; then
	echo "cann't found IP in config file"
	exit -1
fi

WG=`cat $script_path/wireNetwork.ini  | ./busybox grep "WG:" | ./busybox grep -v grep | sed 's/WG://g' | sed 's/\r//g' | sed 's/\n//g'`
if [ $? != 0 ]; then
	echo "cann't found WG in config file"
	exit -1
fi

NETMASK=`cat $script_path/wireNetwork.ini  | ./busybox grep "NETMASK:" | ./busybox grep -v grep | sed 's/NETMASK://g' | sed 's/\r//g' | sed 's/\n//g'`
if [ $? != 0 ]; then
	echo "cann't found NETMASK in config file"
	exit -1
fi

echo "wireNetwork:$wireNetwork  IP:$IP  WG:$WG  NETMASK:$NETMASK"

if [ "$wireNetwork" = "yes" ]; then
	/usr/sbin/load_wired_network.sh &
	sleep 1
	ifconfig eth0 $IP netmask $NETMASK
	route add default gw $WG
	sleep 3
	timetest xmforever
	sleep 2
	# Driver Log Level
	if [ ! -f /mnt/mtd/Log/libXmLog.conf ]; then
		echo "logLevel:INFO" > /mnt/mtd/Log/libXmLog.conf
	fi
	# start app
	/sbin/AppRun.sh &
	echo 1024 > /proc/sys/vm/min_free_kbytes
fi
