#!/bin/sh
# Paths to programs

RUNNING="/var/run/pppoe-action-running"
# Make sure only one default script running.
while :
do
    if [ ! -f "$RUNNING" ] ; then
        break
    fi
    echo "exist $RUNNING"
    sleep 1
done
echo 1 > $RUNNING

# Check if exist multiwan index
if [ -n $2 ] ; then
    MULTIWAN=$2
else
    MULTIWAN=
fi

if [ -z $MULTIWAN ] ; then
	ETH=`rdcsman 0x8000F231 str`
	CNT_FORCE=`rdcsman 0x8001000E u32`
else
	TMP1=$((MULTIWAN+1))
	TMP2=$((MULTIWAN+2))

	ETH=`rdcsman 0x8000F36$TMP2 str`
	CNT_FORCE=`rdcsman 0x800101C$MULTIWAN u32`
fi

#ORG_CNT_TYPE=`rdcsman 0x80040900 u16`
usage () {
        echo "$0 [start|stop|restart|status]"
		rm $RUNNING
        exit 1
}

regenerateConfig () {
	if [ -z $MULTIWAN ] ; then
        uvm /usr/uo/pap-secrets.uo > /etc/ppp/pap-secrets 
		uvm /usr/uo/chap-secrets.uo > /etc/ppp/chap-secrets            
		uvm /usr/uo/pppoe.conf.uo > /etc/ppp/pppoe.conf
	else
        uvm /usr/uo/pap-secrets.uo 1 $MULTIWAN $MULTIWAN > /etc/ppp/pap-secrets 
		uvm /usr/uo/chap-secrets.uo 1 $MULTIWAN $MULTIWAN > /etc/ppp/chap-secrets            
		uvm /usr/uo/pppoe.conf.uo 1 $MULTIWAN $MULTIWAN > /etc/ppp/pppoe.conf
	fi
	chmod 600 /etc/ppp/*-secrets
}


# main ##########################################################

[ -z "$1" ] && usage 

case "$1" in
    start)
		regenerateConfig

		if [ -z $MULTIWAN ] ; then
			if [ $CNT_FORCE -eq 1 ] ; then
			#rewrite connect manual force enable
#				wrcsman "0x80040900 0x02 "
				pppoe-stop
			fi
		 	echo -n "Bringing up PPPoE link"
  
		USERNAME=`rdcsman 0x00040100 str`
		PASSWD=`rdcsman 0x00040200 str`
		echo "\"$USERNAME\" * \"$PASSWD\" *" > /etc/ppp/pap-secrets
		echo "\""$USERNAME"\" * \""$PASSWD"\" *" > /etc/ppp/chap-secrets

			if [ $CNT_FORCE -eq 1 ] ; then
			#rewrite connect manual force disable
				wrcsman "0x8001000E 0x00"
#						 0x80040900 $ORG_CNT_TYPE"
			fi

			pppoe-start
			if [ $? = 0 ] ; then
				rm $RUNNING
				exit 0
			else
				wrcsman "0x80040700 0x03 && \
						 0x80010007 0x03"			
				rm $RUNNING
				exit 1
			fi
		else
			if [ $CNT_FORCE -eq 1 ] ; then
			#rewrite connect manual force enable
#				wrcsman "0x80040900 0x02 "
				pppoe-stop $MULTIWAN
			fi
		 	echo -n "Bringing up PPPoE link"
  
			if [ $CNT_FORCE -eq 1 ] ; then
			#rewrite connect manual force disable
				wrcsman "0x800101C$MULTIWAN 0x00"
#						 0x80040900 $ORG_CNT_TYPE"
			fi

			pppoe-start $MULTIWAN
			if [ $? = 0 ] ; then
				rm $RUNNING
				exit 0
			else
				wrcsman "0x8004070$TMP1 0x03 && \
						 0x8001016$MULTIWAN 0x03"			
				rm $RUNNING
				exit 1
			fi
		fi
    	;;

    stop)
		regenerateConfig

	    echo -n "Shutting down PPPoE link"
		if [ -z $MULTIWAN ] ; then
			pppoe-stop > /dev/null 2>&1
			RET=$?
		
			LOADSHARING=`rdcsman 0x004301c0 u32`
			if [ $LOADSHARING -eq 0 ] ; then
				wrcsman "0x80040200 0x00 && \
						 0x80040300 0x00 && \
						 0x80040400 0x00 && \
						 0x80040500 0x00 && \
						 0x80040600 0x00 && \
						 0x80040700 0x00 && \
						 0x80010002 0x00 && \
						 0x80010003 0x00 && \
						 0x80010004 0x00 && \
						 0x80010005 0x00 && \
						 0x80010006 0x00 && \
						 0x80010007 0x00"
			else
				LOADSHARING_WANTYPE=`rdcsman 0x0043000b u32`
				if [ $LOADSHARING_WANTYPE -eq 6 ] ; then
					wrcsman "0x80040200 0x00 && \
							 0x80040300 0x00 && \
							 0x80040400 0x00 && \
							 0x80040500 0x00 && \
							 0x80040600 0x00 && \
							 0x80040700 0x00"
				fi
			fi
		else
			pppoe-stop $MULTIWAN > /dev/null 2>&1
			RET=$?
		
			wrcsman "0x8004020$TMP1 0x00 && \
					 0x8004030$TMP1 0x00 && \
					 0x8004040$TMP1 0x00 && \
					 0x8004050$TMP1 0x00 && \
					 0x8004060$TMP1 0x00 && \
					 0x8004070$TMP1 0x00 && \
					 0x8001011$MULTIWAN 0x00 && \
					 0x8001012$MULTIWAN 0x00 && \
					 0x8001013$MULTIWAN 0x00 && \
					 0x8001014$MULTIWAN 0x00 && \
					 0x8001015$MULTIWAN 0x00 && \
					 0x8001016$MULTIWAN 0x00"
		fi
		
		ifconfig $ETH 0.0.0.0
		rm $RUNNING
		exit $RET
			
        ;;

    restart)
		rm $RUNNING
		if [ -z $MULTIWAN ] ; then
			$0 stop
			$0 start
		else
			$0 stop $MULTIWAN
			$0 start $MULTIWAN
		fi
		;;

    status)
		regenerateConfig

		if [ -z $MULTIWAN ] ; then
			pppoe-status
		else
			pppoe-status $MULTIWAN
		fi
		;;

    *)
        usage
        ;;
esac

rm $RUNNING
exit 0
