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

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

# Paths to programs
PPPD=pppd
PPPOE=pppoe

CONFIG=/etc/ppp/pppoev6.conf
if test ! -f "$CONFIG" -o ! -r "$CONFIG" ; then
    echo "$0: Cannot read configuration file '$CONFIG'" >& 2
    exit 1
fi

export CONFIG
. $CONFIG

PIDFILE="/var/run/pppoev6.pidfile"
PIDFILE_START="$PIDFILE.start"
PIDFILE_CONNECT="$PIDFILE.connect"

if [ -z $MULTIWAN ] ; then
	PIDFILE_PPPD="/var/run/ppp-pppoev6.pid"
else
	PIDFILE_PPPD="/var/run/ppp-pppoev6$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
MTU_SIZE=$(rdcsman 0x00900303 u16)
if [ $MTU_SIZE -eq 0 ]; then
    MTU="mtu 1500 mru 1500" 
    MTU_SIZE=1500
else
    MTU="mtu $MTU_SIZE mru $MTU_SIZE"
fi
# Change MTU/MRU for Phy. Ethernet device would hangup the existed connection
#ifconfig $ETH up mtu $MTU_SIZE 
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, pppoev6.conf will set DEMAND to no
    DEMAND_OPTIONS=""
    LOCAL_LL_ADDR=""
    REMOTE_LL_ADDR=""
elif test "$MANUAL" = "yes" ; then
    #manual connect & force connect
	if [ $DEMAND -gt 0 ] ; then
        DEMAND_OPTIONS="idle $DEMAND"
	else
        DEMAND_OPTIONS=""
	fi		
    LOCAL_LL_ADDR=""
    REMOTE_LL_ADDR=""
else
    #only on dial-on-deman, set pppd idle time
	DEMAND_OPTIONS="demand nopersist idle $DEMAND holdoff 3 ipcp-accept-remote ipcp-accept-local ipv6cp-accept-remote ipv6cp-accept-local noipdefault ktune"
	LOCAL_LL_ADDR="::1:d:9:20"	# if OPTIONS has both ipv6 and demand options,
	REMOTE_LL_ADDR="::a:b:c:d"	# pppd needs the local and remote LL address for ipv6.
fi

# If we're using kernel-mode PPPoE on Linux...
if [ -z $MULTIWAN ] ; then
	CUR_WANTYPE=`rdcsman 0x00010003 u32`
else
	CUR_WANTYPE=`rdcsman 0x0001012$MULTIWAN 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
    #modprobe pppoe > /dev/null 2>&1
fi

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 test "$DEFAULTROUTE" != "no" ; then
    DEFAULTROUTE="defaultroute"
else
    DEFAULTROUTE=""
fi

PPP_MTU_SIZE=`rdcsman 0x00900303 u32`
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

if [ -z $MULTIWAN ] ; then
   PPPD_EXTRA=
else
   PPPD_EXTRA="$PPPD_EXTRA name $USER remotename wan$TMPA"
fi

if [ -z $MULTIWAN ] ; then
   PPPD_EXTRA="linkname pppoev6"
else
   PPPD_EXTRA="linkname pppoev6_$MULTIWAN"
fi

# ipv6 dual stack or not
BIND_V4V6=$(rdcsman 0x00900004 u32)
if [ $BIND_V4V6 -eq 1 ]; then
    NOIP=""
else
    NOIP="noip"
fi

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

# Jigger DNS if required... //set ipv4 dns
if test "$DNSTYPE" = "SERVER" ; then
	if [ ! -e /var/run/multiwan_enable ]; 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 test -n "$DNS1" ; then
        sl_append_dns_file "/etc/resolv.conf" "$DNS1"
        sl_append_dns_file "/etc/ppp/resolv.conf" "$DNS1" 
    fi

    if test -n "$DNS2" ; then
        sl_append_dns_file "/etc/resolv.conf" "$DNS2"
        sl_append_dns_file "/etc/ppp/resolv.conf" "$DNS2"         
    fi
    sl_uniq_file "/etc/resolv.conf"
    sl_uniq_file "/etc/ppp/resolv.conf"
fi

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

if [ $BIND_V4V6 -eq 1 ] ; then
	# 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$MULTIWAN 0x03"
			rm -rf $PIDFILE_CONNECT		 
		fi
	fi
fi

exit

