#!/bin/sh

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

if [ ! -f $script_path/eth0Setup.ini ]; then
	echo "cann't found eth0Setup file"
	exit -1
fi

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

IP=`cat $script_path/eth0Setup.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/eth0Setup.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/eth0Setup.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

if [ "$Eth0Setup" = "yes" ];then
	ifconfig eth0 down
	ifconfig eth0 up

	echo "IP:$IP,WG:$WG,NETMASK:$NETMASK"

	echo $IP $WG $NETMASK  >> test
	netinit eth0 $IP $NETMASK $WG 
fi
