#!/bin/sh

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

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

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

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

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

IP=`cat $script_path/wifiSetup.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 channel in config file"
	exit -1
fi

WG=`cat $script_path/wifiSetup.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 channel in config file"
	exit -1
fi

NETMASK=`cat $script_path/wifiSetup.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 channel in config file"
	exit -1
fi
echo ssid=$SSID >> test
echo psk=$PASSWORD >> test
sed "s/ssid=.*$/ssid=\"$SSID\"/g" -i $script_path/wpa.conf
sed "s/psk=.*$/psk=\"$PASSWORD\"/g" -i $script_path/wpa.conf
#sed "s/ip=.*$/psk=\"$PASSWD\"/g" -i $script_path/wpa.conf

#sed "s/channel=.*$/channel=$CHANNEL/g" -i $script_path/hostapd.conf

wifiType=`cat /usr/sbin/xmprop  | ./busybox grep "WifiType"`

echo "wifiType:$wifiType"

[ -z "$wifiType" ] && { echo "cann't find wifi type in xmprop file"; }

echo "wifiType:$wifiType"

wifiNanme=${wifiType##*:}

echo "wifiNanme:$wifiNanme"

[ "$wifiNanme" = "XM713G" ] && { ISXM713G=yes; }

[ "$wifiNanme" = "XM711" ] && { ISXM711=yes; }

[ "$wifiNanme" = "SSV6355" ] && { ISSSV6355=yes; }

[ "$wifiNanme" = "ATBM6012B" ] && { ISATBM6012B=yes; }

[ "$wifiNanme" = "SSV6158M" ] && { ISSSV6158M=yes; }

echo "ISXM713G:$ISXM713G,ISXM711:$ISXM711,ISSSV6355:$ISSSV6355,ISATBM6012B:$ISATBM6012B,ISSSV6158M:$ISSSV6158M"

if [ "$ISXM711" = "yes" ];then
	sleep 7
fi

sync

rm /dev/random
ln -s /dev/urandom /dev/random
mkdir -p /var/run/hostapd
mkdir /var/run/wpa_supplicant -p

ifconfig eth2 down
ifconfig eth2 up
#ifconfig eth0 down
#ifconfig eth2 $IP netmask 255.255.255.0 

$script_path/wifiList eth2 "$script_path"

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

echo $IP $WG $NETMASK  >> test
netinit eth2 $IP $NETMASK $WG  

#hostapd $script_path/hostapd.conf -B
#udhcpd -fS $script_path/udhcpd.conf &
if [ "$ISXM711" = "yes" -o "$ISXM713G" = "yes" -o "$ISSSV6355" = "yes" -o "$ISATBM6012B" = "yes" -o "$ISSSV6158M" = "yes" ];then
	echo "xm711 or xm713g or SSV6355 wifi"
	wpa_supplicant -ieth2 -Dnl80211 -c $script_path/wpa.conf >/dev/null 2>&1 &
else
	echo "8188eu wifi"
	wpa_supplicant -ieth2 -Dwext -c $script_path/wpa.conf >/dev/null 2>&1 &
fi
#udhcpc -i eth2 -s $script_path/udhcpc.script

./playOrRecord -p 0 audio/fastconfigstate2.pcm
 nPingCnt=0
while [ $nPingCnt -le 20 ]
do
    echo $nPingCnt
    ping -w 1 -c 1 $WG>/dev/null
    ret=$?
    if [ $ret -eq 0 ];then 
        #echo "OnLine"
        break; #Ping通了就退出了
    else 
        echo "Retry..."
        sleep 1 ; #1秒后再来       
        let "nPingCnt = $nPingCnt+1"
    fi   
done
if [ $nPingCnt -le 20 ];then 
    echo "OnLine"
	echo "1" > /var/ipisconfig
   	./playOrRecord -p 0 audio/fastconfigstate3.pcm
else 
    echo "OffLine"
    ./playOrRecord -p 0 audio/fastconfigfail.pcm
    exit -1;
fi   
