#!/bin/sh
#File: /usr/sbin/ppp-on
#Purpose: a simple PPP dial-up script
#
#PPP_Dev=$1
#WAIT_TIME=$2
#DNS_Ser=$3

if [ -z "$1" ]; then
        PPP_Dev=ttyUSB3
        else
        PPP_Dev=$1
fi

if [ -z "$2" ]; then
        ETH_If=eth0
        else
        ETH_If=$2
fi

if [ -e "/var/lock/LCK..$PPP_Dev" ]; then
	rm -f /var/lock/LCK..$PPP_Dev
fi

#RUN=1

#while [ $RUN -eq 1 ]  
#do  
#	ifconfig $ETH_If | grep UP > /dev/null 2>&1
#	DECT=$?

#	if [ $DECT -eq 0 ]; then	#dectect eth0
#		ifconfig $ETH_If down
#		RUN=0
#	fi
#done

RUN=1
while [ $RUN -eq 1 ]  
do  
	if [ -e /sys/bus/usb-serial/devices/ttyUSB3 ]; then
		RUN=0
fi
done

#sleep 10

#ifconfig $ETH_If | grep UP > /dev/null 2>&1
#DECT=$?

#if [ $DECT -eq 0 ]; then	#dectect eth0
#	ifconfig $ETH_If down
#fi

if [ -f ~/.ppprc ]; then
	ACC=$(cat ~/.ppprc | grep name | awk '{print $2}')
	if [ ! -z "$ACC" ]; then
		MYNAME="$ACC"
		echo ""
		echo "'.ppprc' found in $USER's home directory,"
		echo " trying to use user specified account..."
	fi
fi
if [ -z "$MYNAME" ]; then
	MYNAME=guest
	echo ""
	echo -n "Warning: no account name specified," 
	echo "using default account '${MYNAME}"
fi

echo "PPP is using account ${MYNAME} to connect to ISP ${ISP} ..."
DIALER_SCRIPT=/etc/ppp/ppp-on-dialer

#Deleate Default route
/sbin/route del default

#If DNS is true, set user's DNS server
#if [ -n "$DNS_Ser" ]; then
#	echo "nameserver $DNS_Ser" > /etc/resolv.conf
#fi

echo "Startup pppd daemon ..."
pppd -detach lock modem nocrtscts \
/dev/$PPP_Dev 115200 noipdefault defaultroute \
noauth name ${MYNAME} connect $DIALER_SCRIPT &

echo "3G light state..."
for ((i=0; i <16; i++)) ;
do
	ppp_on=`ifconfig | grep "ppp0" | awk '{print $1}'`
	if [ "$ppp_on" == "ppp0" ]; then
		led -l1 -g1
		break
	elif [ $i == 15 ]; then
		led -l1 -g0
	fi
	sleep 1
done
