#!/bin/sh
#
# chkconfig: 345 95 05
# description: Starts and stops MBSE BBS.
#
# For RedHat, Fedora Core, E-Smith and Mandrake SYSV init style.
# $Id$
#
# Source function library.
. /etc/rc.d/init.d/functions

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

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

# Find the MBSE_ROOT from the /etc/passwd file.
MBSE_ROOT=`cat /etc/passwd | grep ^mbse: | awk -F ':' '{ print $6}'`

if [ "${MBSE_ROOT}" = "" ]
then
    echo "MBSE BBS: No 'mbse' user in the password file."
    exit 1
fi

if [ ! -d ${MBSE_ROOT} ]
then
    echo "MBSE BBS: Home directory '${MBSE_ROOT}' not found."
    exit 1
fi

export MBSE_ROOT


# See how we were called.
#
case "$1" in
    start)
	echo -n "Starting MBSE BBS: "
	rm -f ${MBSE_ROOT}/var/run/*
	rm -f ${MBSE_ROOT}/var/sema/*
	rm -f ${MBSE_ROOT}/var/*.LCK
	rm -f ${MBSE_ROOT}/tmp/mb*
	su - mbse -c '${MBSE_ROOT}/bin/mbtask' >/dev/null
	echo -n "mbtask "
	sleep 2
	if [ -f ${MBSE_ROOT}/etc/config.data ]; then
	    su - mbse -c '${MBSE_ROOT}/bin/mbstat open -quiet'
	    echo "opened"
	fi
	touch /var/lock/subsys/mbsed
	;;
    stop)
	echo -n "Shutting down MBSE BBS: "
	if [ -f ${MBSE_ROOT}/var/run/mbtask ]; then
	    echo -n "logoff users "
	    su - mbse -c '${MBSE_ROOT}/bin/mbstat close wait -quiet' >/dev/null
	    echo -n "done, "
	    echo -n "stop mbtask: "
	    killproc mbtask -15
	    rm -f /var/lock/subsys/mbsed
	    sleep 3
	fi
	echo ""
	;;
  status)
	status mbtask
	;;
  restart|reload)
	$0 stop
	$0 start
	;;
  *)
	echo "Usage: $0 {start|stop|restart|reload|status}"
	exit 1
esac
exit 0