#!/bin/bash
#
#	/etc/rc.d/init.d/diaspora-ws
#
# Starts the diaspora websocket server
#
# chkconfig: - 80 80
# description: Diaspora websocket server

### BEGIN INIT INFO
# Provides: diaspora-ws
# Required-Start: $local_fs $network
# Required-Stop: $local_fs $network
# Should-Start: $remote_fs
# Should-Stop: $remote_fs
# Default-Start: 
# Default-Stop: 0 1 2 3 4 5 6
# Short-Description: start and stop Diaspora websocket server
# Description: The websocket server provides websocket services for
#              diaspora. 
### END INIT INFO

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

if [ -f /etc/sysconfig/diaspora-ws -a $UID -eq 0 ]; then
    . /etc/sysconfig/diaspora-ws
fi

# Note: this line is patched by installation scripts.
cd  /usr/share/diaspora

RETVAL=0
prog="Diaspora websocket server"
exec="script/websocket_server.rb"
pidfile="/var/run/diaspora-ws"
lockfile="/var/lock/subsys/diaspora-ws"
logfile=/var/log/diaspora-ws.log

[ -n "$OPTIONS" ] && OPTIONS=" $OPTIONS"
ruby_cmd="ruby -C $PWD $exec$OPTIONS"

start() {
	[ $UID -eq 0 ] || exit 4
	[ -f $exec ]   || exit 5

        echo -n $"Starting $prog: "
	daemon --pidfile $pidfile "$ruby_cmd >>$logfile 2>&1 &"
	RETVAL=$?
        echo
	if test $RETVAL = 0; then
            touch  $lockfile
            pgrep -f "$ruby_cmd" > $pidfile || {
                echo "Warning: cannot find running diaspora-webserver"
                exit 7
            }
        fi
}

stop() {
	[ $UID -eq 0 ] || exit 4
	echo -n $"Stopping $prog: "
	killproc -p $pidfile $exec
	RETVAL=$?
	[ $RETVAL -eq 0 ] && rm -f $lockfile
	echo
}

#
#	See how we were called.
#
case "$1" in
  start)
	start
	;;
  stop)
	stop
	;;
  force-reload|restart)
	stop
	sleep 1
	start
	RETVAL=$?
	;;
  condrestart|try-restart)
	if [ -f $lockfile ]; then
	    stop
	    sleep 3
	    start
	fi
	;;
  status)
	status -p $pidfile $exec
	RETVAL=$?
	;;
  *)
	echo $"Usage: $0 {condrestart|try-restart|start|stop|restart|force-reload|status}"
	RETVAL=2
	[ "$1" = 'usage' ] && RETVAL=0
esac

exit $RETVAL

