#!/bin/sh
. /usr/bin/scriptlib

# Check if exist multiwan index
if [ -n $1 ] ; then
    MULTIWAN=$1
	TMPA=`printf "%x" $((MULTIWAN+1))`
	TMPB=`printf "%x" $((MULTIWAN-4))`
	MULTIHEX=`printf "%x" $MULTIWAN`
else
    MULTIWAN=
fi

# Paths to programs
PPPD=pppd
PPPOE=pppoe

CONFIG=/etc/ppp/pppoe.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"

if [ -z $MULTIWAN ] ; then
	PIDFILE_PPPD="/var/run/ppp-pppoe.pid"
else
	PIDFILE_PPPD="/var/run/ppp-pppoe$MULTIWAN.pid"
fi

PLUGIN_ATM="/etc/ppp/plugins/pppoatm.so"
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 mtu 1500 
ifconfig $ETH up

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_OPTIONS=""
elif test "$MANUAL" = "yes" ; then
    #manual connect & force connect
	if [ $DEMAND -gt 0 ] ; then
        DEMAND_OPTIONS="idle $DEMAND"
	else
    	DEMAND_OPTIONS=""
	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 [ -z $MULTIWAN ] ; then
	CUR_WANTYPE=`rdcsman 0x00010003 u32`
else
	CUR_WANTYPE=`rdcsman 0x0001012$MULTIHEX u32`
fi

if test "$LINUX_PLUGIN" != "" ; then
    PLUGIN_OPTS="plugin $LINUX_PLUGIN nic-$ETH"
    if [ $CUR_WANTYPE -eq 80 ] ; then
        PLUGIN_OPTS="plugin $LINUX_PLUGIN nic-$ETH rp_pppoe_sess 0154:00:aa:bb:01:23:45 "
        # 0154 is session id , 00:aa:bb:01:23:45 is trendchip adsl default mac address
    fi
    
    if test -n "$SERVICENAME" ; then
        PLUGIN_OPTS="$PLUGIN_OPTS rp_pppoe_service $SERVICENAME"
    fi

    # Interface name MUST BE LAST!!
case $ETH in
	  nas*) PLUGIN_OPTS="$PLUGIN_OPTS" ;;
	     *)PLUGIN_OPTS="$PLUGIN_OPTS $ETH";;
esac
fi
    #modprobe pppoe > /dev/null 2>&1

if [ -e $PLUGIN_ATM ] && [ $CUR_WANTYPE -eq 80 ]; then
	if [ -z $MULTIWAN ] ; then
		VPI=`rdcsman 0x00410102 u32`
		VCI=`rdcsman 0x00410103 u32`
		ENC=`rdcsman 0x00410104 u32`
		if [ $ENC -eq 0 ]; then
			ENC="llc-encaps"
		else
			ENC="vc-encaps"
		fi
	else
		VPI=`rdcsman 0x004101"$TMPB"2 u32`
		VCI=`rdcsman 0x004101"$TMPB"3 u32`
		ENC=`rdcsman 0x004101"$TMPB"4 u32`
		if [ $ENC -eq 0 ]; then
			ENC="llc-encaps"
		else
			ENC="vc-encaps"
		fi
	fi
    PLUGIN_OPTS="plugin $PLUGIN_ATM $ENC $VPI.$VCI"
fi

if [ -z $MULTIWAN ] ; then
	LOCAL_IP="`rdcsman 0x00040300 ipv4`"
else
	LOCAL_IP="`rdcsman 0x0004030$TMPA ipv4`"
fi

if [ "$LOCAL_IP" != "0.0.0.0" ]; then
    PLUGIN_OPTS="$PLUGIN_OPTS $LOCAL_IP: "
fi  

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

if [ -z $MULTIWAN ] ; then
	PPP_MTU_SIZE=`rdcsman 0x00040A00 u32`
else
	PPP_MTU_SIZE=`rdcsman 0x00040A0$TMPA u32`
fi

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="$PLUGIN_OPTS noipdefault noauth \
                 default-asyncmap $DEFAULTROUTE user $USER hide-password \
                 nodetach $PEERDNS $SET_MTU debug \
                 noaccomp nodeflate nopcomp novj novjccomp noccp nomppe \
                 lcp-echo-interval $LCP_INTERVAL lcp-echo-failure $LCP_FAILURE \
                 $PPPD_EXTRA"

# Jigger DNS if required...
if test "$DNSTYPE" = "SERVER" ; then
    # Sorry, dude...
	FAILOVER_STATUS=`rdcsman 0x80430600 u32`
	if [ ! -e /var/run/multiwan_enable ] && [ "$FAILOVER_STATUS" -eq "0" ]; then
		rm -f /etc/resolv.conf /etc/ppp/resolv.conf
	fi
    #ln -s /etc/ppp/resolv.conf /etc/resolv.conf
elif test "$DNSTYPE" = "SPECIFY" ; then
    if [ "$DNS1" != "0.0.0.0"  ] || [ "$DNS2" != "0.0.0.0" ]; then
        sl_insert_dns_file "/etc/resolv.conf" "$DNS1" "$DNS2"
        sl_insert_dns_file "/etc/ppp/resolv.conf" "$DNS1" "$DNS2"
    fi
    sl_uniq_file "/etc/resolv.conf"
    sl_uniq_file "/etc/ppp/resolv.conf"
fi

echo $$ > $PIDFILE_CONNECT
echo "DEMAND_OPT -> $DEMAND_OPTIONS"
$PPPD $PPP_STD_OPTIONS $DEMAND_OPTIONS &
wait

# if CNTTYPE != PPPOE_CNT_TYPE_MANUAL or CNT_TYPE=dial-on-deman
if [ -z $MULTIWAN ] ; then
    CNT_TYPE=`rdcsman 0x00040900 u32`
    if [ $MANUAL != "yes" -o $CNT_TYPE -eq 0 ] ; then
        #killed by commander 
        wrcsman "0x80040700 0x03 && \
                0x80010007 0x03"
        rm -rf $PIDFILE_CONNECT		 
    fi
else
    CNT_TYPE=`rdcsman 0x0004090$TMPA u32`
    if [ $MANUAL != "yes" -o $CNT_TYPE -eq 0 ] ; then
        #killed by commander 
        wrcsman "0x8004070$TMPA 0x03 && \
                0x8001016$MULTIHEX 0x03"
            #	0x8001016$MULTIWAN 0x03"
        rm -rf $PIDFILE_CONNECT		 
    fi
fi

exit

