#!/bin/sh

### BEGIN INIT INFO
# Provides:          storlever
# Required-Start:    $network $syslog $local_fs
# Required-Stop:     $network $syslog $local_fs
# Should-Start:      $local_fs
# Should-Stop:       $local_fs
# Default-Start:     2 3 4 5
# Default-Stop:      0 1 6
# Short-Description: The management system of Linux storage and network resource with RESTful API
# Description:       The web management system of Linux , which foucus on storage and network resource, \
#                    and provides RESTful API to central management software as well as the web page for \
#                    system admin.
### END INIT INFO

if [ -e /lib/lsb/init-functions ]; then
    . /lib/lsb/init-functions
    
    log_success() {
        log_success_msg "$1"
    }

    log_failure() {
        log_failure_msg "$1"
    }
else
    . /etc/rc.d/init.d/functions

    log_success() {
        echo_success
        echo "$1"
    }

    log_failure() {
        echo_failure
        echo "$1"
    }
fi

RETVAL=0
NAME=storlever
DAEMON=/usr/bin/pserve
PIDFILE=/var/run/storlever.pid
CFGFILE=/etc/storlever.ini
LOGFILE=/dev/null

case "$1" in
    start)
        echo "Starting $NAME:"
        export LC_CTYPE=en_US.UTF8

        if pidofproc -p $PIDFILE $DAEMON > /dev/null; then
            log_failure "already running"
            exit 1
        fi
        if $DAEMON --daemon --log-file=$LOGFILE --pid-file=$PIDFILE $CFGFILE; then
            log_success "started"
            RETVAL=0
        else
            RETVAL=$?
            log_failure "failed"						
        fi
        ;;
    stop)
        echo "Stopping $NAME:"
        if pidofproc -p $PIDFILE $DAEMON > /dev/null; then
            $DAEMON --stop-daemon --pid-file=$PIDFILE;
            /bin/rm -rf $PIDFILE
            log_success "stopped"
        else
            log_failure "not running"		   
        fi
        ;;
    restart)
        $0 stop && sleep 2 && $0 start
        ;;
    status)
        if pidofproc -p $PIDFILE $DAEMON > /dev/null; then
            log_success "$NAME is running"
        else
            log_success "$NAME is not running"
            RETVAL=1
        fi
        ;;
    *)
        echo "Usage: $0 {start|stop|restart|status}"
        exit 1 
esac

exit $RETVAL
