#!/bin/sh
#
# lento		Starts the InterMezzo replication daemon
#
#
# chkconfig: 2345 99 1
# description: startup/shutdown script for the InterMezzo replication daemon.
# 'start' will start Lento and 'stop' will stop Lento.  startdebug will start
# Lento with debugging at 10000 and presto kernel debugging.
#

. /etc/rc.d/init.d/functions

if [ -f /etc/sysconfig/intermezzo ]; then
	. /etc/sysconfig/intermezzo
fi

EXCLUDE_GID=`sed -n -e "s/^InterMezzo:x:\([0-9]*\):/\1/gp" /etc/group`
if [ -z $EXCLUDE_GID ]; then
    echo "Group InterMezzo do not exist,please create it in /etc/group" >> /var/log/lento
    exit 1
fi

# EXCLUDED_GID=${EXCLUDE_GID:=4711}

# See how we were called.
case "$1" in
  start)
  	# configure the InterMezzo "exclude group"
	echo $EXCLUDE_GID > /proc/sys/intermezzo/presto_excluded_gid
	# exclude lost+found from replication
	sed -e 's/#.*//g' -e '/^$/d' /etc/fstab |
	    while read dev mntpnt type options; do
		if [ "$type" = "InterMezzo" ]; then
			chgrp InterMezzo $mntpnt/lost+found
		fi
	done
  	# start the SSL tunnels
	if [ -n "$SSL_SERVER" ]; then
		if [ -n "$SSL_CLIENT" ]; then
			stunnel -c -d rpc2portmap -r $SSL_SERVER:rpc2portmap
		else
			stunnel -d $SSL_SERVER:rpc2portmap -p $SSLPEMFILE -r rpc2portmap
		fi
	fi

	if [ -x /usr/bin/runlento ]; then
		/usr/bin/runlento --mem_max=31000 --restart_max=4 --restart_range=1800 >>/var/log/lento 2>&1 &
	else
		lento >>/var/log/lento 2>&1 &
	fi
	;;
  stop)
	killproc runlento
        killproc lento
	if [ -n "$SSL_SERVER" ]; then
		killproc stunnel
	fi
	echo 0 > /proc/sys/intermezzo/debug
	echo 0 > /proc/sys/intermezzo/trace
    	;;
  restart | reload | force-reload)
	$0 stop
	$0 start
    	;;
  startdebug)
    	echo $EXCLUDE_GID > /proc/sys/intermezzo/presto_excluded_gid
	lento >>/tmp/lento.log --debuglevel=10000 2>&1 &
	echo 4095 > /proc/sys/intermezzo/debug
	echo 1 > /proc/sys/intermezzo/trace
	;;
  *)
    echo "Usage: lento {start|stop|startdebug|restart}"
    exit 1
esac

exit 0

