#!/bin/sh

# Paths to programs
PPPD=pppd
#PPPOE=pppoe

CONFIG=/etc/ppp/iburst.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_PPPD="/var/run/ppp-iburst.pid"


PPPD_PID=0

# MTU of Ethernet card attached to modem MUST be 1500.  This apparently
# fails on some *BSD's, so we'll only do it under Linux

ifconfig $ETH up
#ifconfig $ETH up mtu 1500


if test -n "$ACNAME" ; then
    ACNAME="-C $ACNAME"
fi

if test -n "$SERVICENAME" ; then
    SERVICENAMEOPT="-S $SERVICENAME"
else
    SERVICENAMEOPT=""
fi

if test "$CLAMPMSS" = "no" ; then
    CLAMPMSS=""
else
    CLAMPMSS="-m $CLAMPMSS"
fi

# 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

# Backward config file compatibility -- PEERDNS used to be USEPEERDNS
if test "$USEPEERDNS" = "yes" ; then
    PEERDNS="usepeerdns"
fi
if test "$USEPEERDNS" = "no" ; then
    PEERDNS=""
fi


# Backward config file compatibility
if test "$DEMAND" = "" ; then
    DEMAND=no
fi

if test "$DEMAND" = "no" ; then
#auto-reconncet connect, do not setup pppd idle time
#	DEMAND=`rdcsman 0x00040b00 u16`
#	DEMAND="idle $DEMAND"
    DEMAND_OPTIONS=""
elif test "$MANUAL" = "yes" ; then
#manual connect
	CNT_TYPE=`rdcsman 0x00340900 u16`
	if [ $CNT_TYPE -eq 0 -o $CNT_TYPE -eq 2 ] ; then
		#Manual connect or dial-on-demand (manual trigger)still need idle time parameter 
		IDLE_TIME=`rdcsman 0x00340b00 u16`
		if [ $IDLE_TIME -gt 0 ] ; then
            DEMAND_OPTIONS="idle $IDLE_TIME"
		else
            DEMAND_OPTIONS=""
		fi
	fi
else
#only on dial-on-deman, set pppd idle time
    DEMAND_OPTIONS="demand nopersist idle $DEMAND holdoff 3 ipcp-accept-remote ipcp-accept-local noipdefault ktune"
fi

# If we're using kernel-mode PPPoE on Linux...
if test "$LINUX_PLUGIN" != "" ; then
    PLUGIN_OPTS="plugin $LINUX_PLUGIN nic-$ETH"
    #PLUGIN_OPTS="plugin $LINUX_PLUGIN"
    if test -n "$SERVICENAME" ; then
	PLUGIN_OPTS="$PLUGIN_OPTS rp_pppoe_service $SERVICENAME"
    fi

    # Interface name MUST BE LAST!!
    #PLUGIN_OPTS="$PLUGIN_OPTS $ETH"
    #modprobe pppoe > /dev/null 2>&1
fi

if test "$DEFAULTROUTE" != "no" ; then
    DEFAULTROUTE="defaultroute"
else
    DEFAULTROUTE=""
fi

PPP_MTU_SIZE=`rdcsman 0x00340A00 u16`

if [ $PPP_MTU_SIZE -eq 0 ]; then
	SET_MTU="mtu 1492 mru 1492"
else
	SET_MTU="mtu $PPP_MTU_SIZE mru $PPP_MTU_SIZE"
fi

# Standard PPP options we always use
PPP_STD_OPTIONS="ttyS4 $PLUGIN_OPTS debug noipdefault noauth $DEFAULTROUTE $PEERDNS $SET_MTU user $USER $PPPD_EXTRA"
#PPP_STD_OPTIONS="$PLUGIN_OPTS noipdefault noauth default-asyncmap $DEFAULTROUTE hide-password nodetach $PEERDNS $SET_MTU noaccomp nodeflate nopcomp novj novjccomp user $USER lcp-echo-interval $LCP_INTERVAL lcp-echo-failure $LCP_FAILURE $PPPD_EXTRA"

# Jigger DNS if required...
if test "$DNSTYPE" = "SERVER" ; then
    # Sorry, dude...
    rm -f /etc/resolv.conf /etc/ppp/resolv.conf
    #ln -s /etc/ppp/resolv.conf /etc/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
echo "DEMAND_OPT -> $DEMAND_OPTIONS"

echo $PPPD $PPP_STD_OPTIONS $DEMAND_OPTIONS > /tmp/iburst_ppp_options
$PPPD $PPP_STD_OPTIONS $DEMAND_OPTIONS &

wait

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

