#!/bin/sh
### BEGIN INIT INFO
# Provides:          rozofs-manager-agent
# Required-Start:    $local_fs $remote_fs $network $syslog
# Required-Stop:     $remote_fs
# Default-Start:     2 3 4 5
# Default-Stop:      0 1 6
# Short-Description: RozoFS manager agent
# Description:       RozoFS is a scale-out NAS file system. This service
#                    provides the rozofs-manager-agent 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="rozo manager agent"
NAME=rozofs-manager-agent
DAEMON=/usr/bin/rozo
DAEMON_ARGS=""
PIDFILE=/var/run/rozo-agent.pid
SCRIPTNAME=$0

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

# Default values
AGENTS=auto
PACEMAKER=auto
EXPORTD_RESOURCE_NAME=auto

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

if echo "$AGENTS" | grep -q 'auto' ; then
    AGENTS_auto=''
    for agent in exportd storaged rozofsmount; do
        if [ -x "/usr/bin/$agent" ] ; then
            AGENTS_auto="$AGENTS_auto $agent"
        fi
    done
    AGENTS=$(echo $AGENTS | sed "s/auto/$AGENTS_auto/")
fi

if [ "$PACEMAKER" = 'auto' ] ; then
    if [ -x /usr/sbin/pacemakerd ] ; then
        PACEMAKER=true
    else
        PACEMAKER=false
    fi
fi

# 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

if [ -t 0 ] ; then
  # Be verbose when called from a terminal
  VERBOSE=yes
fi

#
# Function that starts the daemon/service
#
do_start()
{
  # Return
  #   0 if daemon has been started
  #   1 if daemon was already running
  #   2 if daemon could not be started

  $DAEMON agent status > /dev/null 2>&1

  if [ "$?" -eq 0 ] ; then
    return 1
  fi

  if [ "$PACEMAKER" = 'true' ] ; then

    if [ "$EXPORTD_RESOURCE_NAME" = 'auto' ] ; then
      $DAEMON agent start -p -l ${AGENTS} > /dev/null 2>&1
    else
      $DAEMON agent start -p ${EXPORTD_RESOURCE_NAME} -l ${AGENTS} > /dev/null 2>&1
    fi

  else
      $DAEMON agent start -l ${AGENTS} > /dev/null 2>&1
  fi

  case "$?" in
    0) return 0 ;;
    *) return 2 ;;
  esac
}

#
# Function that stops the daemon/service
#
do_stop()
{
  # Return
  #   0 if daemon has been stopped
  #   1 if daemon was already stopped
  #   2 if daemon could not be stopped

  $DAEMON agent status > /dev/null 2>&1

  if [ "$?" -eq 2 ] ; then
    return 1
  fi

  $DAEMON agent stop > /dev/null 2>&1
  case "$?" in
    0) return 0 ;;
    *) return 2 ;;
  esac
}

case "$1" in
  start)
    [ "$VERBOSE" != no ] && log_daemon_msg "Starting $DESC " "$NAME"
    do_start
    case "$?" in
      0) [ "$VERBOSE" != no ] && log_end_msg 0 ;;
      1) [ "$VERBOSE" != no ] && log_progress_msg "(already started)" && log_end_msg 0 ;;
      2) [ "$VERBOSE" != no ] && log_end_msg 1 ;;
    esac
  ;;
  stop)
    [ "$VERBOSE" != no ] && log_daemon_msg "Stopping $DESC" "$NAME"
    do_stop
    case "$?" in
      0) [ "$VERBOSE" != no ] && log_end_msg 0 ;;
      1) [ "$VERBOSE" != no ] && log_progress_msg "(already stopped)" && log_end_msg 0 ;;
      2) [ "$VERBOSE" != no ] && log_end_msg 1 ;;
    esac
  ;;
  status)
    status_of_proc -p "$PIDFILE" "$DAEMON" "$NAME" && return 0 || return $?
  ;;
  restart|force-reload)
    $0 stop
    $0 start
  ;;
  *)
    echo "Usage: $SCRIPTNAME {start|stop|status|restart|force-reload}" >&2
    exit 3
  ;;
esac

: