#!/bin/sh
#
# Copyright (c) 2014-2016 Linagora
#
# This program/library is free software: you can redistribute it and/or modify
# it under the terms of the GNU Lesser General Public License as published by
# the Free Software Foundation, either version 2.1 of the License, or (at your
# option) any later version.
#
# This program/library is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
# FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
# for more details.
#
# You should have received a copy of the GNU Lesser General Public License
# along with this program/library; If not, see http://www.gnu.org/licenses/
# for the GNU Lesser General Public License version 2.1.
#
### BEGIN INIT INFO
# Provides:          petals-registry
# Required-Start:    $remote_fs $syslog
# Required-Stop:     $remote_fs $syslog
# Default-Start:     2 3 4 5
# Default-Stop:      0 1 6
# Short-Description: Petals Registry Overlay automatic startup script
# Description:       This file should be used to construct scripts to be
#                    placed in /etc/init.d.
### END INIT INFO

#
# Author: Christophe DENEUX <christophe.deneux@linagora.com>
#

# Do NOT "set -e"

# PATH should only include /usr/* if it runs after the mountnfs.sh script
PATH=/sbin:/usr/sbin:/bin:/usr/bin
DESC="Petals Registry Overlay"
NAME=petals-registry
DAEMON=/usr/bin/$NAME
SCRIPTNAME=/etc/init.d/$NAME
CONF_BASE="/etc/petals-registry/member-enable"
RUNDIR=/var/run/$NAME

# Exit if the package is not installed
[ -x "$DAEMON" ] || 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.2-14) to ensure that this file is present
# and status_of_proc is working.
. /lib/lsb/init-functions


#
# Function that starts all Petals Registry Member as daemons/services
#
do_start_all_members()
{
   MEMBERS=`ls -1 $CONF_BASE`
   log_daemon_msg "Starting Petals Registry Overlay:"
   if [ -z "$MEMBERS" ]; then
      log_progress_msg " No Petals Registry Overlay member to start."
      log_end_msg 0
   else
      mkdir -p $RUNDIR
      for MEMBER in $MEMBERS
      do
         DAEMON_ARGS="-c file://$CONF_BASE/$MEMBER/member.properties"
         PIDFILE=$RUNDIR/$MEMBER.pid
         log_progress_msg " Starting Petals Registry Overlay member: $MEMBER"
         WARNING="0"
         do_start
         case "$?" in
            0|1)
               # No operation 
               ;;
            2) 
               WARNING="1"
               ;;
         esac
      done
      log_end_msg $WARNING
   fi
   
}

#
# Function that starts one Petals Registry Overlay member as daemon/service
#
do_start()
{
	# Return
	#   0 if daemon has been started
	#   1 if daemon was already running
	#   2 if daemon could not be started
	start-stop-daemon --start --chuid petals --quiet --background --make-pidfile --pidfile $PIDFILE --startas $DAEMON --test > /dev/null \
		|| return 1
	start-stop-daemon --start --chuid petals --quiet --background --make-pidfile --pidfile $PIDFILE --startas $DAEMON -- \
		$DAEMON_ARGS \
		|| return 2
}

#
# Function that stops all Petals Registry Overlay member daemons/services
#
do_stop_all_members()
{
   MEMBERS=`ls -1 $CONF_BASE`
   log_daemon_msg "Stopping Petals Registry Overlay:"
   if [ -z "$MEMBERS" ]; then
      log_progress_msg " No Petals Regitry Overlay member to stop."
      log_end_msg 0
   else
      for MEMBER in $MEMBERS
      do
         PIDFILE=$RUNDIR/$MEMBER.pid
         log_progress_msg " Stopping Petals Registry Overlay member: $CONTAINER"
         WARNING="0"
         do_stop
         case "$?" in
            0|1)
               # No operation 
               ;;
            2) 
               WARNING="1"
               ;;
         esac
      done
      log_end_msg $WARNING
   fi
}

#
# 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
	#   other if a failure occurred
	start-stop-daemon --stop --chuid petals --quiet --retry=TERM/30/KILL/5 --pidfile $PIDFILE
	RETVAL="$?"
	[ "$RETVAL" = 2 ] && return 2
	rm -f $PIDFILE
	return "$RETVAL"
}

#
# Function that sends a SIGHUP to the daemon/service
#
do_reload() {
	#
	# If the daemon can reload its configuration without
	# restarting (for example, when it is sent a SIGHUP),
	# then implement that here.
	#
	start-stop-daemon --stop --signal 1 --quiet --pidfile $PIDFILE --name $NAME
	return 0
}

case "$1" in
  start)
	[ "$VERBOSE" != no ] && log_daemon_msg "Starting $DESC" "$NAME"
	do_start_all_members
	;;
  stop)
	[ "$VERBOSE" != no ] && log_daemon_msg "Stopping $DESC" "$NAME"
	do_stop_all_members
	;;
  status)
   status_of_proc "$DAEMON" "$NAME" && exit 0 || exit $?
   ;;
  #reload|force-reload)
	#
	# If do_reload() is not implemented then leave this commented out
	# and leave 'force-reload' as an alias for 'restart'.
	#
	#log_daemon_msg "Reloading $DESC" "$NAME"
	#do_reload
	#log_end_msg $?
	#;;
  restart|force-reload)
	#
	# If the "reload" option is implemented then remove the
	# 'force-reload' alias
	#
	log_daemon_msg "Restarting $DESC" "$NAME"
	do_stop_all_members
	case "$?" in
	  0|1)
		do_start_all_members
		case "$?" in
			0) log_end_msg 0 ;;
			1) log_end_msg 1 ;; # Old process is still running
			*) log_end_msg 1 ;; # Failed to start
		esac
		;;
	  *)
	  	# Failed to stop
		log_end_msg 1
		;;
	esac
	;;
  *)
	#echo "Usage: $SCRIPTNAME {start|stop|restart|reload|force-reload}" >&2
	echo "Usage: $SCRIPTNAME {start|stop|status|restart|force-reload}" >&2
	exit 3
	;;
esac
