#!/bin/sh

# Paths to programs
HSO=hso.sh

CONFIG=/etc/ppp/3g.conf

if test ! -f "$CONFIG" -o ! -r "$CONFIG" ; then
    echo "$0: Cannot read configuration file '$CONFIG'" >& 2
    exit 1
fi

export CONFIG
. $CONFIG

PIDFILE_START="$PIDFILE.start"
PIDFILE_CONNECT="$PIDFILE.connect"
PIDFILE_HSO="/var/run/hso-3g.pid"

HSO_PID=0
BACKUP=`rdcsman 0x001e000d u16`
APN=`rdcsman 0x001e0003 str`

# If DNSTYPE is SERVER, we must use "usepeerdns" option to pppd.
if test "$DNSTYPE" = "SERVER" ; then
    PEERDNS=yes
    USEPEERDNS=yes
fi

if test "$PEERDNS" = "yes" ; then
    PEERDNS="usepeerdns"
else
    PEERDNS=""
fi

# Jigger DNS if required...
if test "$DNSTYPE" = "SERVER" ; then
    # Sorry, dude...
    rm -f /etc/resolv.conf /etc/ppp/resolv.conf
elif test "$DNSTYPE" = "SPECIFY" ; then
    # Sorry, dude...
    rm -f /etc/resolv.conf /etc/ppp/resolv.conf
    echo "nameserver $DNS1" > /etc/resolv.conf
    if test -n "$DNS2" ; then
    echo "nameserver $DNS2" >> /etc/resolv.conf
    fi
fi

echo $$ > $PIDFILE_CONNECT

if [ $BACKUP -eq 1 ] ; then
	GW=`rdcsman 0x80010004 ipv4`
	IFNAME=`rdcsman 0x8001000B str`
	route del default gw $GW $IFNAME
fi

################################################################################

TMPFIL=/tmp/HSO-connect

if [ -n "$APN" ]; then
	send_ATcmd /dev/modem AT+CGDCONT=1,\"IP\",\"$APN\"
fi

if [ -n "$USER" ]; then
	PASSWORD=`rdcsman 0x001e0002 str`
	send_ATcmd /dev/modem AT\$QCPDPP=1,1,$PASSWORD,$USER
fi

send_ATcmd /dev/modem AT_OWANCALL=1,0,0
sleep 3
send_ATcmd /dev/modem AT_OWANCALL=1,1,0
sleep 3
send_ATcmd /dev/modem AT_OWANDATA=1 > $TMPFIL
sleep 3
PIP="`grep '^_OWANDATA' $TMPFIL | cut -d, -f2`"
NS1="`grep '^_OWANDATA' $TMPFIL | cut -d, -f4`"
NS2="`grep '^_OWANDATA' $TMPFIL | cut -d, -f5`"

if [ -z "$PIP" ]; then
#	rm -f $TMPFIL
#	echo  0 > /var/run/3g_link_status	
#	if [ -z "$BACKUP" ]; then
#		sleep 2
#		echo 93 > /var/run/monitor.fifo
#	fi
	return 0	
fi	

	IFNAME=hso0
	echo "Got IP: $PIP"
	ifconfig $IFNAME $PIP netmask 255.255.255.255 up
	route add default gw $PIP dev $IFNAME
#	route add default dev hso0

#	if [ "$USEPEERDNS" = "1" ]; then
#		echo "set nameserver"
#		(							# update the DNS
#			echo "nameserver	$NS1"
#			echo "nameserver	$NS2"
#		) > /etc/resolv.conf
#	fi

#	rm -f $TMPFIL	
#	echo hso0 > /var/run/3g_ifname
#	echo 90 > /var/run/monitor.fifo
#	echo  1 > /var/run/3g_link_status
#	if [ "$BACKUP" = "1" ]; then
#		echo 1 > /var/run/backup_status
#	fi

################################################################################

	. /usr/bin/scriptlib

	sl_get_IP_NM_GW_in_ifconfig $IFNAME IF_IP IF_NM IF_GW
#	sl_get_DNS_in_resolv_conf IF_DNS1 IF_DNS2
	sl_get_uptime CNTTIME

	wrcsman "0x801e0100 $CNTTIME && \
		0x801e0200 {$IF_IP} && \
		0x801e0300 {$IF_NM} && \
		0x801e0400 {$IF_GW} && \
		0x801e0500 {$NS1} && \
		0x801e0600 {$NS2} && \
		0x801e0700 0x02 && \
		0x80010002 {$IF_IP} && \
		0x80010003 {$IF_NM} && \
		0x80010004 {$IF_GW} && \
		0x80010005 {$NS1} && \
		0x80010006 {$NS2} && \
		0x8001000B \"$IFNAME && \
		0x80010007 0x02 && \
		0x801e1100 0x00"

	WANTYPE=`rdcsman 0x00010003 u16`
	BACKUP=`rdcsman 0x001e000d u16`
	
	if [ $BACKUP -eq 1 ] && [ $WANTYPE -ne 16 ]; then
		wrcsman "0x801e0700 0x07 && \
			0x801e1200 0x02"
	fi


	echo "nameserver $NS1" > /etc/resolv.conf
	echo "nameserver $NS2" >> /etc/resolv.conf
echo 

################################################################################

#wait # wait for all child process

#if [ $MANUAL != "yes"  ] ; then
#	#killed by commander
#	wrcsman "0x801e0700 0x03 && \
#			0x80010007 0x03"
#	rm -rf $PIDFILE_CONNECT
#fi
exit

