#!/bin/sh
#
# Startup script for the CIPE VPN
#
# chkconfig: 345 11 89
# description: CIPE is used to create encrypted IP-IP.
# processname: ciped-cb
# pidfile: /var/run/ciped-cb.pid
# config: /etc/cipe/options


# Source function library.
. /etc/rc.d/init.d/functions

# Source networking configuration.
. /etc/sysconfig/network

# Source cipe configurtion.
if [ -f /etc/sysconfig/cipe ] ; then
	. /etc/sysconfig/cipe
else
	exit 1
fi


# Check that networking is up.
[ ${NETWORKING} = "no" ] && exit 0

RETVAL=0

ip=$(ifconfig $DEVICE | awk '/inet addr/ {sub ("addr:",""); print $2}')

start() {
	echo -n "Starting CIPE: "
	if [ -f /var/run/ciped-cb.pid ] ; then  
		echo "CIPE is already running"
		exit 1
	fi
	if [ -n "$DEVICE" ] ; then
		CIPE_OPTS="${CIPE_OPTS} me=${ip}:${PORT}"
	fi
	if [ -n "$PORT" -a -n "$PEER" ] ; then
		CIPE_OPTS="${CIPE_OPTS} peer=${PEER}:${PORT}"
	fi
	if [ -n "$IPADDR" ] ; then
		CIPE_OPTS="${CIPE_OPTS} ipaddr=${IPADDR}"
	fi
	if [ -n "$PTPADDR" ] ; then
		CIPE_OPTS="${CIPE_OPTS} ptpaddr=${PTPADDR}"
	fi
	daemon ciped-cb ${CIPE_OPTS}
        RETVAL=$? 
	echo
	[ $RETVAL -eq 0 ] && touch /var/lock/subsys/ciped
}

stop() {
	echo -n "Shutting down CIPED: "
        killproc ciped-cb
        RETVAL=$?
   	echo
        [ $RETVAL -eq 0 ] && rm -f /var/lock/subsys/ciped
}

# See how we were called.
case "$1" in
    start)
	start
	;;
    stop)
	stop
        ;;
    status)
	status ciped-cb
	RETVAL=$?
	;;
    restart)
	stop
	start
	RETVAL=$?
	;;
    condrestart)
	if [ -f /var/lock/subsys/ciped ] ; then
	    stop
	    start
	fi
	;;
    reload)
	stop
	start
	;;
    *)
	echo "Usage: $0 {start|stop|restart|reload|status}"
	RETVAL=1
esac

exit $RETVAL
