corrade
#!/bin/bash
#
# corrade       This shell script takes care of starting and stopping
#               Corrade (the Second Life / OpenSim bot).
#
# chkconfig: - 58 74
# description: Corrade is a Second Life / OpenSim bot. \
# Corrade is a Second Life / OpenSim bot that stays connected to a grid \
# and processes commands sent to it via in-world LSL scripts or HTTP.
 
### BEGIN INIT INFO
# Provides: corrade
# Required-Start: $network $local_fs $remote_fs
# Required-Stop: $network $local_fs $remote_fs
# Should-Start: $syslog $named ntpdate
# Should-Stop: $syslog $named
# Short-Description: start and stop corrade
# Description: Corrade is a Second Life / OpenSim bot that stays
#              connected to a grid and processes commands sent to it
#              via in-world LSL script or HTTP.
### END INIT INFO
 
# Source function library.
. /etc/init.d/functions
 
###########################################################################
##                             CONFIGURATION                             ##
###########################################################################
CORRADE_USER="corrade" 
CORRADE_FOLDER="Corrade"
DAEMON="Corrade.exe"
LOCK="Corrade.exe.lock"
 
###########################################################################
##                              INTERNALS                                ##
###########################################################################
# Determine the path to mono and mono-service.
MONO=$(which mono)
MONOSERVER=$(which mono-service)
 
start() {
	echo -n "Starting Corrade: "
        HOMEDIR=`getent passwd $CORRADE_USER | cut -d: -f6`
        if [ -f $HOMEDIR/$CORRADE_FOLDER/$DAEMON ]; then
            if [ -f $HOMEDIR/$CORRADE_FOLDER/$LOCK ]; then
                rm -rf $HOMEDIR/$CORRADE_FOLDER/$LOCK
            fi
            su $CORRADE_USER -c "$MONOSERVER -d:$HOMEDIR/$CORRADE_FOLDER -l:$HOMEDIR/$CORRADE_FOLDER/$LOCK $HOMEDIR/$CORRADE_FOLDER/$DAEMON"
            echo "OK"
        fi
}	
 
status() {
	dbpid=`pgrep -u $CORRADE_USER mono`
	if [ -z $dbpid ] ; then
	    echo "corrade for USER $CORRADE_USER: not running."
	else
	    echo "corrade for USER $CORRADE_USER: running (pid $dbpid)"
	fi
}
 
stop() {
	echo -n "Shutting down Corrade: "
	HOMEDIR=`getent passwd $CORRADE_USER | cut -d: -f6`
	if [ -f $HOMEDIR/$CORRADE_FOLDER/$DAEMON ]; then
	    dbpid=`pgrep -u $CORRADE_USER mono`
	    if [ ! -z $dbpid ]; then
	        kill -s TERM $dbpid
	    fi
	    if [ -f $HOMEDIR/$CORRADE_FOLDER/$LOCK ]; then
	        rm -rf $HOMEDIR/$CORRADE_FOLDER/$LOCK
	    fi
	    echo "OK"
	fi
}
 
case "$1" in
    start)
	start
	;;
    stop)
	stop
	;;
    status)
        status
	;;
    restart)
    	stop
	start
	;;
    *)
	echo "Usage: corrade {start|stop|status|restart}"
	exit 1
	;;
esac
exit $?