#!/bin/sh
#
# fastladder:   Fastladder RSS Reader
#
# chkconfig: - 06 99
# description: Fastladder was very fast Web Bases RSS Reader
#
# processname: script/server
# pidfile: /var/www/fastladder/tmp/pids/mongrel.pid
#
#

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

# so we can rearrange this easily
processname="pushd /var/www/fastladder 2>&1 > /dev/null ; script/server -d -e production 2>&1 > /dev/null"
servicename=fastladder 
pidfile="/var/www/fastladder/tmp/pids/mongrel.pid"
crawlername="pushd /var/www/fastladder 2>&1 > /dev/null ; RAILS_ENV=production script/crawler > /dev/null &"

RETVAL=0

start() {

    status -p $pidfile 2>&1 > /dev/null
    RETVAL=$?
    [ $RETVAL -eq 0 ] && status -p $pidfile && exit 1
    
    echo -n $"Starting FastLadder: "
    daemon --check fastladder-clowler $crawlername
    RETVAL=$?
    daemon --check $servicename $processname
    RETVAL=$?
    echo
    [ $RETVAL -eq 0 ] && touch /var/lock/subsys/$servicename
}

stop() {
    echo -n $"Stopping FastLadder: "

    pkill -KILL -f crawler
    killproc  -p $pidfile
    rm -f $pidfile
    RETVAL=$?
    echo
    if [ $RETVAL -eq 0 ]; then
        rm -f /var/lock/subsys/$servicename
    fi
}

# See how we were called.
case "$1" in
    start)
        start
        ;;
    stop)
        stop
        ;;
    status)
        status -p $pidfile fastladder
        RETVAL=$?
        ;;
    restart)
        stop
	sleep 3
        start
        ;;
    condrestart)
        if [ -f /var/lock/subsys/$servicename ]; then
            stop
	    sleep 3
            start
        fi
        ;;
    *)
        echo $"Usage: $0 {start|stop|status|restart|condrestart}"
        ;;
esac
exit $RETVAL
