#!/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
# 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
name=raidsrv
prog=/opt/nec/raidsrv/$name
if [ -d /var/lock/subsys ]; then
    lockfile=/var/lock/subsys/$name
else
    lockfile=/var/lock/$name
fi
if [ -f /etc/init.d/functions ]; then
    # for Redhat/MIRACLE/Asianux
    . /etc/init.d/functions
    START_DAEMON="daemon $prog"
    STOP_DAEMON="killproc $prog"
    STATUS="status $name"
    RC_STATUSV=
elif [ -f /lib/lsb/init-functions ]; then
    . /lib/lsb/init-functions
    if [ -f /etc/SuSE-release ]; then
        # for SUSE
        START_DAEMON="start_daemon $prog"
        STOP_DAEMON="killproc $prog"
        STATUS="/sbin/checkproc $name"
        RC_STATUSV="rc_status -v"
        # execute reset
        rc_reset
    elif [ -f /etc/os-release ]; then
        # for Ubuntu
        START_DAEMON="start-stop-daemon --start --exec $prog"
        STOP_DAEMON="start-stop-daemon --stop --exec $prog"
        STATUS="start-stop-daemon --start --test --exec $prog"
        RC_STATUSV=
    else
        echo "Error: your platform is not supported by $0" > /dev/stderr
        exit 1
    fi
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

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 lo | grep 'UP[, ]LOOPBACK[, ]RUNNING' >& /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
        RETVAL=$?
        $RC_STATUSV
        echo
        [ $RETVAL -eq 0 ] && touch $lockfile
        return $RETVAL
}
stop() {
        echo -n $"Stopping raidsrv services: "
        $STOP_DAEMON
        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
                $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

