#!/bin/sh
#
# rozofs-manager-agent This shell script takes care of starting and stopping
#                      the manager agents.
#
#chkconfig: 35 20 80
#description: rozofs manager agent
#processname: rozofs-manager-agent

### BEGIN INIT INFO
# Provides:          rozofs-manager-agent
# Required-Start:    $network $local_fs
# Required-Stop:     
# Default-Start:     2 3 4 5
# Default-Stop:      0 1 6
# Short-Description: RozoFS manager agent
# Description:       RozoFS is a scale-out NAS file system. This service
#                    provides the rozofs-manager-agent functionality.
### END INIT INFO


DAEMON=/usr/bin/rozo
NAME=rozo
PIDFILE=/var/run/rozo-agent.pid

# Read configuration variable file if it is present
[ -r /etc/default/rozofs-manager-agent ] && . /etc/default/rozofs-manager-agent

# Source function library.
if [ -f /lib/lsb/init-functions ]; then
    . /lib/lsb/init-functions
    alias START_DAEMON=start_daemon
    alias LOG_SUCCESS=log_success_msg
    alias LOG_FAILURE=log_failure_msg
    alias LOG_WARNING=log_warning_msg
elif [ -f /etc/init.d/functions ]; then
    . /etc/init.d/functions
    alias START_DAEMON=daemon
    alias LOG_SUCCESS=success
    alias LOG_FAILURE=failure
    alias LOG_WARNING=passed
else
    echo "Error: your platform is not supported by $0" > /dev/stderr
    exit 1
fi

do_status () {
    # Return
    #   0 if daemon is running
    #   1 if daemon is not running
	local status 

    status="0"
    $DAEMON agent-status >/dev/null || status="$?"
    if [ "$status" = 0 ]; then
        log_success_msg "$NAME is running"
        return $status
    else
        log_success_msg "$NAME is not running"
        return $status
    fi
}

do_start()
{
    # Return
    #   0 if daemon has been started
    #   1 if daemon could not be started

    if $PACEMAKER; then
    	$DAEMON agent-start -p $AGENTS
    else
        $DAEMON agent-start $AGENTS
    fi
    
    return "$?"
}

# stop the daemon/service

do_stop()
{
    # Return
    #   0 if daemon has been stopped
    #   1 if daemon could not be stopped
    #   other if a failure occurred
    
	$DAEMON agent-stop
	return "$?"
}

case "$1" in
    start)
        echo -n "Starting $NAME"
        
        do_start

        case "$?" in
            0) LOG_SUCCESS ;;
            *) LOG_FAILURE ;;
        esac
        ;;
    stop)
        echo -n "Stopping $NAME"
        
        do_stop

        case "$?" in
            0) LOG_SUCCESS ;;
            *) LOG_FAILURE ;;
        esac
        ;;
    status)
        do_status
        ;;
    restart|force-reload)
        echo -n "Restarting $NAME"

        do_stop
        case "$?" in
            0|1)
                sleep 1
                do_start

                case "$?" in
                    0) LOG_SUCESS ;;
                    *) LOG_FAILURE ;; # Failed to start
                esac
                ;;
            *)
                # Failed to stop
                LOG_FAILURE
            ;;
        esac
        ;;
    *)

    LOG_WARNING "Usage: $0 {start|stop|restart|force-reload}" >&2
    exit 3
    ;;
esac

:

