#!/bin/sh
#
# compassd          Compass daemon
##################################

# LSB header

### BEGIN INIT INFO
# Provides: compassd
# Required-Start: $network $httpd 
# Default-Start: 3 4 5
# Default-Stop: 0 1 2 6
# Short-Description: compassd
# Description: Compass daemon service
#              
### END INIT INFO

# chkconfig header

# chkconfig: 345 99 99
# description:  This is a daemon that provides Compass daemon service
#
# Checking Sanity
[ -x /opt/compass/bin/poll_switch.py ] || exit 0
[ -x /opt/compass/bin/progress_update.py ] || exit 0
UBUNTU=/etc/debian_version
SUSE=/etc/SuSE-release

if [ -f $UBUNTU ]; then
    . /lib/lsb/init_functions
elif [ -f $SUSE -a -r /etc/rc.status ]; then
    . /etc/rc.status
else
    . /etc/rc.d/init.d/functions
fi

SERVICE=compassd
PROCESS=compassd

RETVAL=0
start() {
    echo "Starting Compass: "
    if [ -f $SUSE ]; then
        echo -n "Start celeryd: "
        startproc -f -p /var/run/celeryd.pid -l /tmp/celeryd.log "C_FORCE_ROOT=1 CELERY_CONFIG_MODULE=compass.utils.celeryconfig_wrapper celeryd"
        rc_status -v
        echo
        echo -n "Start service progress_update: "
        startproc -f -p /var/run/progress_update.pid -l /tmp/progress_update.log /opt/compass/bin/progress_update.py
        rc_status -v
        echo
    elif [ -e $UBUNTU ]; then
        if [ -f /var/run/celeryd.pid ]; then
            echo "celeryd is already started"
            RETVAL=1
        elif C_FORCE_ROOT=1 CELERY_CONFIG_MODULE=compass.utils.celeryconfig_wrapper celeryd &> /tmp/celeryd.log; then
            echo "celeryd starts OK"
            RETVAL=0
        fi
        if [ -f /var/run/progress_update.pid ]; then
            echo "progress_update is already started"
            RETVAL=1
        elif /usr/bin/python /opt/compass/bin/progress_update.py &> /tmp/progress_update.log; then
            echo "progress_update starts OK"
            RETVAL=0
        fi
    else
        echo -n "Start celeryd: "
        daemon --pidfile /var/run/celeryd.pid "C_FORCE_ROOT=1 CELERY_CONFIG_MODULE=compass.utils.celeryconfig_wrapper celeryd &>/tmp/celeryd.log & echo \$! > /var/run/celeryd.pid"
        RETVAL=$?
        echo
        echo -n "Start service progress_update: "
        daemon --pidfile /var/run/progress_update.pid "/opt/compass/bin/progress_update.py &>/tmp/progress_update.log & echo \$! > /var/run/progress_update.pid"
        RETVAL=$?
        echo
    fi
    echo
    return $RETVAL
}

stop() {
    echo "Stopping Compass: "
    if [ -f $SUSE ]; then
        echo -n "Stop service celeryd: "
        killproc -t 10 -p /var/run/celeryd.pid celeryd
        rc_status -v
        echo
        echo -n "Stop service progress_update: "
        killproc -t 30 -p /var/run/progress_update.pid /opt/compass/bin/progress_update.py
        rc_status -v
        echo
    elif [ -f $UBUNTU ]; then
        echo "Unsupported"
        RETVAL=1
    else
        echo -n "Stop service celeryd: "
        killproc -p /var/run/celeryd.pid -d 30 celeryd
        RETVAL=$?
        echo
        echo -n "Stop service progress_update: "
        killproc -p /var/run/progress_update.pid -d 30 /opt/compass/bin/progress_update.py
        RETVAL=$?
        echo
    fi
}

restart() {
   stop
   start
}
case "$1" in
    start|stop|restart)
        $1
        ;;
    status)
        echo "Checking compass: "
        if [ -f $SUSE ]; then
            echo -n "Checking for service celeryd: "
            checkproc -v -p /var/run/celeryd.pid celeryd
            rc_status -v
            echo
            echo -n "Checking for service progress_update: "
            checkproc -v -p /var/run/progress_update.pid /opt/compass/bin/progress_update.py
            rc_status -v
            echo
        elif [ -f $UBUNTU ]; then
            echo -n "Checking for service celeryd"
            if [ -f /var/run/celeryd.pid ]; then
                RETVAL=0
                echo "celeryd is running."
            else
                RETVAL=1
                echo "celeryd is stopped."
            fi
            echo -n "Checking for service progress_update"
            if [ -f /var/run/progress_update.pid ]; then
                RETVAL=0
                echo "progress_update is running."
            else
                RETVAL=1
                echo "progress_update is stopped."
            fi
        else
            echo -n "checking for service celeryd: "
            status -p /var/run/celeryd.pid celeryd
            retval=$?
            echo
            echo -n "checking for service progress_update: "
            status -p /var/run/progress_update.pid /opt/compass/bin/progress_update.py
            retval=$?
            echo
        fi
        ;;
    *)
        echo "Usage: $0 {start|stop|status|restart}"
        exit 1
        ;;
esac
exit $RETVAL