#!/bin/bash
#
# Universal RAID Utility raidsrv_agent service
#
# chkconfig: 2345 91 9
# description: Sends information of RAID Sytem to ESMPRO Manager in order to manage RAID System from remote system.
# processname: raidsrv_agent
# config: /etc/opt/nec/raidsrv/raidsrv_agent.conf
#

### BEGIN INIT INFO
# Provides: raidsrv_agent
# Required-Start: $network
# Should-Start: raidsrv
# Required-Stop:
# Default-Start: 2 3 4 5
# Default-Stop: 0 1 6
# Short-Description: Sends information of RAID Sytem to ESMPRO Manager in order to manage RAID System from remote system.
# Description: Sends information of RAID Sytem to ESMPRO Manager in order to manage RAID System from remote system.
### END INIT INFO

# source function library
if [ -f /etc/init.d/functions ]; then
    # for Redhat/MIRACLE/Asianux
    . /etc/init.d/functions
    START_DAEMON=daemon
    STATUS=status
    RC_STATUSV=
elif [ -f /lib/lsb/init-functions ]; then
    # for SUSE
    . /lib/lsb/init-functions
    START_DAEMON=start_daemon
    STATUS=/sbin/checkproc
    RC_STATUSV="rc_status -v"
    # execute reset
    rc_reset
else
    echo "Error: your platform is not supported by $0" > /dev/stderr
    exit 1
fi

# check networking status
if [ -f /etc/redhat-release ]; then
    if [ ! -f /etc/sysconfig/network ]; then
        exit 0
    fi

    . /etc/sysconfig/network
    [ "${NETWORKING}" = "no" ] && exit 0
fi
prog="/opt/nec/raidsrv/raidsrv_agent"
lockfile="/var/lock/subsys/raidsrv_agent"

RETVAL=0

start() {
        # chech network interfaces
        /sbin/ifconfig lo | grep 'UP[, ]LOOPBACK[, ]RUNNING' >& /dev/null
        if [ $? -ne 0 ]; then
            exit 0
        fi

        echo -n $"Starting raidsrv_agent services: "
        if [ -f $lockfile ]; then
            echo "The raidsrv_agent service is already running";
            return $RETVAL;
        fi
        export AXIS2C_HOME=/opt/nec/axis2c
        export LD_LIBRARY_PATH=$AXIS2C_HOME/lib:$LD_LIBRARY_PATH
        $START_DAEMON $prog
        RETVAL=$?
        $RC_STATUSV
        echo
        [ $RETVAL -eq 0 ] && touch $lockfile
        return $RETVAL
}
stop() {
        echo -n $"Stopping raidsrv_agent services: "
        killproc $prog #-15
        RETVAL=$?
        $RC_STATUSV
        echo
        [ $RETVAL -eq 0 ] && rm -f $lockfile
        return $RETVAL
}

restart() {
        stop
        start
}

case "$1" in
        start)
                start
                ;;
        stop)
                stop
                ;;
        restart)
                restart
                ;;
        status)
                $STATUS raidsrv_agent
                $RC_STATUSV
                ;;
        *)
                echo $"Usage: $0 {start|stop|restart|status}"
                exit 1
esac

exit $RETVAL

