#!/bin/bash
#
# Universal RAID Utility raidsrv service
#
# chkconfig: 2345 90 10
# description: Manages the information of RAID System and monitors the failure of it.
# processname: raidsrv
# config: /etc/opt/nec/raidsrv/raidsrv.conf
#

### BEGIN INIT INFO
# Provides: raidsrv
# Required-Start: $network $syslog
# Should-Start:
# Required-Stop:
# Default-Start: 2 3 4 5
# Default-Stop: 0 1 6
# Short-Description: Manages the information of RAID System and monitors the failure of it.
# Description: Manages the information of RAID System and monitors the failure of it.
### 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
lockfile=/var/lock/subsys/raidsrv

RETVAL=0

# Load Promise environment variable 
if [ -e /etc/profile.d/Promise.sh ]; then 
    source /etc/profile.d/Promise.sh 
fi 

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

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


#start() {
#       echo -n $"Starting raidsrv service: "
#       touch "$lockfile" && success || failure
#       daemon $prog
#       RETVAL=$?
#       echo
#}

#stop() {
#       echo -n $"Stoping raidsrv service: "
#       rm -f "$lockfile" && success || failure
#       killproc $prog
#       RETVAL=$?
#       echo
#}
restart() {
        stop
#       status raidsrv
#       sleep 1
        start
#        $RC_STATUSV
}

case "$1" in
        start)
                start
                ;;
        stop)
                stop
                ;;
        restart)
                restart
                ;;
        status)
                $STATUS raidsrv
                $RC_STATUSV
#               if [ -f $lockfile ]; then
#                       echo $"The raidsrv service is running."
##                      RETVAL=0
#               else
#                       echo $"The raidsrv service is not running."
#                       RETVAL=3
#               fi
                ;;
        *)
                echo $"Usage: $0 {start|stop|restart|status}"
                exit 1
esac

exit $RETVAL

