#!/bin/sh

# Bring up/down ipsec

PIDFILE="/var/run/l2tpd.pid"
SERVICE=rp-l2tpd

. /etc/sysconfig/rc.conf
. /bin/network-functions
. /etc/image_features

BINDIR="/bin"
CONFDIR="/etc/ppp-l2tp"
MYLOG="/tmp/logs/l2tp.log"


startActiveCons () {

    for a in `cat /etc/ppp-l2tp/connections | grep ActiveEnabled4Shell`; do
	IFS="="
	set -- $a
	IPADDR=$2
	LINKNAME=$3
	echo "state=2" > /var/run/l2tp/$3.state
	/bin/l2tp-control "start-session $2"
	unset IFS
    done
}

do_start ()
{
    /etc/init.d/softdogd wait

    ## open firewall
#if [ "$FEATURE_FIREWALL" = "1" ] ; then
#    /bin/firewall system add l2tpd > /dev/null 2>&1
#fi

    ## and start daemon
    /bin/setsid "$BINDIR"/rp-l2tpd -d 65535 -f &> /tmp/logs/l2tpd.log &

    ## store pid
    /bin/pidof rp-l2tpd > /var/run/l2tpd.pid

    ## wait
    /bin/usleep 500
    startActiveCons

    ServiceCheckRunning2
    return $?
}

do_stop ()
{
    /etc/init.d/softdogd wait

    /bin/killall ppp-l2tp > /dev/null 2>&1
    ServiceStop
    ret="$?"

    ## erase logfile
    echo "<--erase fiadlog-->" > $MYLOG

#if [ "$FEATURE_FIREWALL" = "1" ] ; then
#    ## and close the firewall
#    /bin/firewall system del l2tpd > /dev/null 2>&1
#fi

    return "$ret"
}


do_status ()
{
    ServiceStatus
    return $?
}

case "$1" in
	start)
		do_start
		;;
	stop)
		do_stop
		;;
	status)
		do_status
		;;
	restart)
		do_stop
		/bin/usleep 300000
		do_start
		;;
	*)
		echo "Usage: $0 {start|stop|restart|status}"
		exit 1
esac

exit $?
