#!/bin/sh
# Paths to programs

ETH=`rdcsman 0x8000F231 str`
CNT_FORCE=`rdcsman 0x8001000E u16`
#ORG_CNT_TYPE=`rdcsman 0x80040900 u16`
usage () {
        echo "$0 [start|stop|restart|status]"
        exit 1
}


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

[ -z "$1" ] && usage 

case "$1" in
    start)
		if [ $CNT_FORCE -eq 1 ] ; then
		#rewrite connect manual force enable
#			wrcsman "0x80040900 0x02 "
			pppoe-stop
		fi
     	echo -n "Bringing up PPPoE link"
  
        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
		chmod 600 /etc/ppp/*-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
			exit 0
		else
			wrcsman "0x80040700 0x03 && \
					 0x80010007 0x03"			
			exit 1
		fi
    	;;

    stop)
        echo -n "Shutting down PPPoE link"
		pppoe-stop > /dev/null 2>&1
		RET=$?
		
		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"

		
		ifconfig $ETH 0.0.0.0
		
		exit $RET
			
        ;;

    restart)
		$0 stop
		$0 start
		;;

    status)
		pppoe-status
		;;

    *)
        usage
        ;;
esac

exit 0
