#!/bin/sh

# @file clearwater-infrastructure.init.d
#
# Copyright (C) Metaswitch Networks 2016
# If license terms are provided to you in a COPYING file in the root directory
# of the source code repository by which you are accessing this code, then
# the license outlined in that COPYING file applies to your use.
# Otherwise no rights are granted except for those provided to you by
# Metaswitch Networks in a separate written agreement.

# On systems running cloud-init, some of the configuration set by
# clearwater-infrastructure, e.g. hostname, is also managed by cloud-init.
# In order to avoid cloud-init overwriting these settings, we ask the init
# system to run clearwater-infrastructure after cloud-init has finished.

### BEGIN INIT INFO
# Provides:          clearwater-infrastructure
# Required-Start:    $network $local_fs
# Required-Stop:
# Should-Start:      cloud-init
# Default-Start:     2 3 4 5
# Default-Stop:      0 1 6
# Short-Description: Clearwater infrastructure
# Description:       Determines and applies local configuration
# X-Start-Before:    memcached bono sprout restund homer homestead homestead-prov ralf memento
### END INIT INFO

# PATH should only include /usr/* if it runs after the mountnfs.sh script
PATH=/sbin:/usr/sbin:/bin:/usr/bin
DESC=clearwater-infrastructure             # Introduce a short description here
NAME=clearwater-infrastructure             # Introduce the short server's name here
SCRIPTNAME=/etc/init.d/$NAME

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

# Load the VERBOSE setting and other rcS variables
[ -r /lib/init/vars.sh ] && . /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

# Include the clearwater init helpers.
. /usr/share/clearwater/utils/init-utils.bash

#
# Function that starts the daemon/service
#
do_start()
{
        # Fill in any files that depend on it
        for SCRIPT in $(ls -1 /usr/share/clearwater/infrastructure/scripts/* 2>/dev/null)
        do
          if [ -f "$SCRIPT" ]; then
            $SCRIPT
          fi
        done

        return 0
}

#
# Function that stops the daemon/service
#
do_stop()
{
    # Nothing to do, because no actual daemon.
    return 0;
}

#
# Function that sends a SIGHUP to the daemon/service
#
do_reload() {
        # Nothing to do
        return 0
}

case "$1" in
  start)
    [ "$VERBOSE" != no ] && log_daemon_msg "Starting $DESC " "$NAME"
    do_start
    case "$?" in
                0|1) [ "$VERBOSE" != no ] && 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|1) [ "$VERBOSE" != no ] && log_end_msg 0 ;;
                2) [ "$VERBOSE" != no ] && log_end_msg 1 ;;
        esac
        ;;
  status)
        echo "clearwater-infrastructure is not a long-running daemon so does not have a 'status' command"
        ;;
  reload|force-reload)
        log_daemon_msg "Reloading $DESC" "$NAME"
        do_reload
        log_end_msg $?
        ;;
  restart)
        log_daemon_msg "Restarting $DESC" "$NAME"
        do_stop
        case "$?" in
          0|1)
                do_start
                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

:
