#!/bin/sh

# 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"
PIDFILE_PPPD="/var/run/ppp-pppoe.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 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_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 test "$LINUX_PLUGIN" != "" ; then
    PLUGIN_OPTS="plugin $LINUX_PLUGIN nic-$ETH"
    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 0x00040A00 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="$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"

	$PPPD $PPP_STD_OPTIONS $DEMAND_OPTIONS &

    wait

	# if CNTTYPE != PPPOE_CNT_TYPE_MANUAL or CNT_TYPE=dial-on-deman
	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
	exit

