#!/bin/sh
### BEGIN INIT INFO
# Provides:          rozofs-storaged
# Required-Start:    $network $local_fs $remote_fs $portmap
# Required-Stop:     $remote_fs
# Default-Start:     2 3 4 5
# Default-Stop:      0 1 6
# Short-Description: RozoFS storaged server
# Description:       RozoFS is a scale-out NAS file system. This service
#                    provides the rozofs-storaged server functionality.
### END INIT INFO

# Author: Fizians S.A.S. <devel@rozofs.com>

# PATH should only include /usr/* if it runs after the mountnfs.sh script
PATH=/sbin:/usr/sbin:/bin:/usr/bin
DESC=storaged            # Introduce a short description here
NAME=storaged     # Introduce the short server's name here
DAEMON=/usr/bin/storaged # Introduce the server's location here
DAEMON_ARGS=""           # Arguments to run the daemon with
PIDFILE=/var/run/launcher_${NAME}.pid
SCRIPTNAME=$0
LAUNCHER=/usr/bin/rozolauncher

# Exit if the package is not installed
[ -x $DAEMON ] || exit 0
[ -x $LAUNCHER ] || exit 0

# Read configuration variable file if it is present
[ -r /etc/default/$NAME ] && . /etc/default/$NAME

# Load the VERBOSE setting and other rcS variables
. /lib/init/vars.sh

# Define LSB log_* functions.
# Depend on lsb-base (>= 3.0-6) to ensure that this file is present.
. /lib/lsb/init-functions

# Enable core files
ulimit -c unlimited

status () {

    if [ -f $PIDFILE ];
    then 
      exit 0
    fi   
    
    exit 3
}
#
# Function that starts the daemon/service
#
do_start()
{
    # 1rst check the configuration
    $DAEMON $DAEMON_ARGS -C
    case "$?" in
    
      # Configuration is valid. 
      # Let's run through rozolauncher.
      "0"){
         $LAUNCHER start $PIDFILE $DAEMON $DAEMON_ARGS &
         return 0
      };;
      
      # Configuration is invalid. 
      # Return an error.
      *) {
	 echo "Inconsistant configuration !!!"
	 echo "Check log files."
	 return 3      
      };;
      
    esac    
}

#
# Function that stops the daemon/service
#
do_stop()
{
    $LAUNCHER stop $PIDFILE
    return 0
}


case "$1" in
  start|reload|restart|force-reload)
    [ "$VERBOSE" != no ] && log_daemon_msg "$1 $DESC " "$NAME"
    do_start
    res=$?
    case "$res" in
        0) [ "$VERBOSE" != no ] && log_end_msg 0 ;;
        *) [ "$VERBOSE" != no ] && log_end_msg 1 ;;
    esac
    exit $res
    ;;
  stop)
    [ "$VERBOSE" != no ] && log_daemon_msg "Stopping $DESC" "$NAME"
    do_stop
    res=$?
    case "$res" in
        0) [ "$VERBOSE" != no ] && log_end_msg 0 ;;
        *) [ "$VERBOSE" != no ] && log_end_msg 1 ;;
    esac
    exit $res
    ;;
  status) status;;

  *)
    echo "Usage: $SCRIPTNAME {start|stop|status|restart}" >&2
    exit 3
    ;;
esac

:
