#!/bin/sh

USAGE="Usage: $BBSDIR/bin/rocat_daemons {start | stop }"

# debugging overrides the system limits
# enabling core dumps when daemons are sent SIGUSR1
DEBUG=1

if [ $DEBUG -eq 1 ]; then
	ulimit -c unlimited
fi

if [ ! -d $BBSDIR ]
then			# /usr not mounted ??
	echo "$BBSDIR not set"
	echo "set $BBSDIR and try again."
	exit 
fi
case "$1" in
'start')

	if [ -x $BBSDIR/bin/errlogd ]
	then
		$BBSDIR/bin/errlogd &	
	fi
	sleep 5;	# give errlogd time to start
	if [ -x $BBSDIR/bin/rochatd ]
	then
		$BBSDIR/bin/rochatd &	
	fi
	if [ -x $BBSDIR/bin/ropchatd ]
	then
		$BBSDIR/bin/ropchatd &	
	fi
	if [ -x $BBSDIR/bin/robcastd ]
	then
		$BBSDIR/bin/robcastd &	
	fi
	if [ -x $BBSDIR/msql/bin/msqld ]
	then
		$BBSDIR/msql/bin/msqld &
	fi
	;;


'stop')
 #	kill the daemons if still running
# errlogd
 	pid=`/bin/ps -axgu 2>/dev/null | /usr/bin/grep errlogd | /usr/bin/grep -v grep \
 			| /usr/bin/sed -e 's/[ ][ ]*/ /' | /bin/cut -f2 -d' '`
 	if [ "${pid}" != "" ]
 	then
 		/bin/kill ${pid}
 	fi

# rochatd
 	pid=`/bin/ps -axgu 2>/dev/null | /usr/bin/grep rochatd | /usr/bin/grep -v grep \
 			| /usr/bin/sed -e 's/[ ][ ]*/ /' | /bin/cut -f2 -d" "`
 	if [ "${pid}" != "" ]
 	then
 		/bin/kill ${pid}
 	fi

# ropchatd
 	pid=`/bin/ps -axgu 2>/dev/null | /usr/bin/grep ropchatd | /usr/bin/grep -v grep \
 			| /usr/bin/sed -e 's/[ ][ ]*/ /' | /bin/cut -f2 -d" "`
 	if [ "${pid}" != "" ]
 	then
 		/bin/kill ${pid}
 	fi

# robcastd
 	pid=`/bin/ps -axgu 2>/dev/null | /usr/bin/grep robcastd | /usr/bin/grep -v grep \
 			| /usr/bin/sed -e 's/[ ][ ]*/ /' | /bin/cut -f2 -d" "`
 	if [ "${pid}" != "" ]
 	then
 		/bin/kill ${pid}
 	fi

# msqld
 	pid=`/bin/ps -axgu 2>/dev/null | /usr/bin/grep msqld | /usr/bin/grep -v grep \
 			| /usr/bin/sed -e 's/[ ][ ]*/ /' | /bin/cut -f2 -d" "`
 	if [ "${pid}" != "" ]
 	then
 		/bin/kill ${pid}
 	fi


	;;
*)
	echo ${USAGE}
	exit 1
	;;
esac
