#!/bin/sh
#
# GPVE		GPVE daemon
#
# chkconfig: 345 47 68
# description:  The GNU Virtual Private Ethernet suite implements a virtual 
#               (uses udp, tcp, rawip and other protocols for tunneling),
#               private (encrypted, authenticated) ethernet (mac-based,
#               broadcast-based network) that is shared among multiple nodes,
#               in effect implementing an ethernet bus over public networks.
# processname: gvpe
# config: /etc/gvpe/gvpe.conf
# pidfile: /var/run/gvpe.pid
#
### BEGIN INIT INFO
# Provides:          gvpe
# Required-Start:    $network
# Required-Stop:     $network
# Should-Start:      $time $syslog
# Should-Stop:       $syslog
# Default-Start:     345
# Default-Stop:      0 1 2 6
# Short-Description: Script to start and stop the GNU Virtual Private Ethernet daemon.
# Description:       The GNU Virtual Private Ethernet suite implements a virtual 
#                    (uses udp, tcp, rawip and other protocols for tunneling),
#                    private (encrypted, authenticated) ethernet (mac-based,
#                    broadcast-based network) that is shared among multiple nodes,
#                    in effect implementing an ethernet bus over public networks.
### END INIT INFO

# Do not load RH compatibility interface.
WITHOUT_RC_COMPAT=1

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

DAEMON=gvpe
PIDFILE=/var/run/$DAEMON.pid
LOCKFILE=/var/lock/subsys/$DAEMON

GVPEBIN="/usr/sbin/$DAEMON"
SYSCONFIGFILE="/etc/sysconfig/$DAEMON"

RETVAL=0

###################################################################
[ -x "$GVPEBIN" ] || exit

# Source networking configuration.
SourceIfNotEmpty /etc/sysconfig/network
is_yes "$NETWORKING" || return 0

# GVPE configuration
CONFIGDIR="/etc/$DAEMON"
LOGLEVEL=info
MLOCK=
NODE=

SourceIfNotEmpty $SYSCONFIGFILE

if is_yes "$MLOCK"; then 
    MLOCK='-mlock'
else
    MLOCK=''
fi

if [ ! -f "$CONFIGDIR/gvpe.conf" ]; then
    echo -n "Config file $CONFIGDIR/gvpe.conf not found!"
    failure "$DAEMON startup"
    echo
    exit 1
fi

#################################################################
start()
{
	if [ "x$NODE" == "x" ]; then
		echo -n "Node name is not found!"
		failure "$DAEMON startup"
		echo
		echo "You need to setup proper node name in $SYSCONFIGFILE"
		echo
		RETVAL=1
	fi
	if egrep -q "^node *= *$NODE *\$" "$CONFIGDIR/gvpe.conf"; then 
		start_daemon --pidfile "$PIDFILE" --lockfile "$LOCKFILE" \
			--expect-user root -- "$GVPEBIN" "$MLOCK -l$LOGLEVEL" -c "$CONFIGDIR" "$NODE"
		RETVAL=$?
	else
		echo -n "Node $NODE is not found in config file $CONFIGDIR/gvpe.conf!"
		failure "$DAEMON startup"
		echo
		echo "You need to setup proper node name in $SYSCONFIGFILE"
		echo
		RETVAL=1
	fi
	return $RETVAL
}

stop()
{
	stop_daemon --pidfile "$PIDFILE" --lockfile "$LOCKFILE" --expect-user root -- "$GVPEBIN"
	RETVAL=$?
	return $RETVAL
}

restart()
{
	stop
	start
}

# See how we were called.
case "$1" in
	start)
		start
		;;
	stop)
		stop
		;;
	reload|restart)
		restart
		;;
	condstop)
		if [ -e "$LOCKFILE" ]; then
			stop
		fi
		;;
	condrestart)
		if [ -e "$LOCKFILE" ]; then
			restart
		fi
		;;
	condreload)
		if [ -e "$LOCKFILE" ]; then
			restart
		fi
		;;
	status)
		status --pidfile "$PIDFILE" --expect-user root -- "$GVPEBIN"
		RETVAL=$?
		;;
	*)
		msg_usage "${0##*/} {start|stop|reload|restart|condstop|condrestart|condreload|status}"
		RETVAL=1
esac

exit $RETVAL
