#!/bin/sh
#  _    _          ___           __ _     (R)
# | |  (_)_ _____ / __|___ _ _  / _(_)__ _
# | |__| \ V / -_) (__/ _ \ ' \|  _| / _` |
# |____|_|\_/\___|\___\___/_||_|_| |_\__, |
#                                    |___/
# Copyright (c) 2009-2022 LiveConfig GmbH.
# ----------------------------------------------------------------------------
# /etc/init.d/liveconfig
# Init script for LiveConfig Server
# ----------------------------------------------------------------------------

### BEGIN INIT INFO
# Provides:          liveconfig
# Required-Start:    $network $syslog $local_fs
# Required-Stop:     $network $syslog $local_fs
# Should-Start:      $time $named apache2 mysql
# Should-Stop:       $time $named apache2 mysql
# Default-Start:     2 3 4 5
# Default-Stop:      0 1 6
# Short-Description: Start and stop the LiveConfig server control panel
# Description:       This script controls the LiveConfig server control
#                    panel daemon "liveconfig"
### END INIT INFO

DESC="LiveConfig Server"
NAME=liveconfig
DAEMON=/usr/sbin/$NAME
PIDFILE=/run/liveconfig/liveconfig.pid
LC_CONFIG=/etc/liveconfig/liveconfig.conf
SCRIPTNAME=/etc/init.d/$NAME

# This init script is basically LSB compatible. Because various distributions
# support different levels of LSB compatibility, some few (missing) functions
# are substituted on demand (eg. log_daemon_msg())

# Check for missing binaries (stale symlinks should not happen)
test -x $DAEMON || { echo "$DAEMON not installed"; 
    if [ "$1" = "stop" ]; then exit 0;
    else exit 5; fi; }

# Check /etc/liveconfig/liveconfig.conf
test -r $LC_CONFIG || { echo "$LC_CONFIG not existing";
    if [ "$1" = "stop" ]; then exit 0;
    else exit 6; fi; }

# Use LSB if available
if [ -f /lib/lsb/init-functions ]; then
    . /lib/lsb/init-functions
elif [ -f /etc/rc.d/init.d/functions ]; then
    # use CentOS/RedHat functions
    . /etc/rc.d/init.d/functions
fi

fn_exists() {
    type "$1" 1>/dev/null 2>/dev/null
}

if ! fn_exists "log_daemon_msg"; then
    # Define "log_daemon_msg"
    log_daemon_msg() {
        if [ -z "${1:-}" ]; then
            return 1
        fi
        if [ -z "${2:-}" ]; then
            echo -n "$1:"
            return
        fi
        echo -n "$1: $2"
    }
fi

if ! fn_exists "log_end_msg"; then
    # Define "log_end_msg"
    log_end_msg() {
        # If no arguments were passed, return
        [ -z "${1:-}" ] && return 1

        if [ $1 -eq 0 ]; then
            echo "."
	elif [ $1 -eq 255 ]; then
	    echo "(warning)."
        else
            echo "failed!"
        fi
        return $1
    }
fi


# ----------------------------------------------------------------------------
# Start the LiveConfig Server daemon
# ----------------------------------------------------------------------------
do_start()
{
    # Return
    #   0 if daemon has been started
    #   1 if daemon was already running
    #   2 if daemon could not be started
    if [ -x /sbin/start-stop-daemon ]; then
        # use start-stop-daemon
        /sbin/start-stop-daemon --start --quiet --pidfile "$PIDFILE" --exec "$DAEMON" 1>/dev/null
    elif [ -r "$PIDFILE" ]; then
        if read pid < "$PIDFILE" && ps -p "$pid" > /dev/null 2>&1;
        then 
            return 1
        else
            rm "$PIDFILE"
        fi
    else
        $DAEMON 1>/dev/null
    fi
}

# ----------------------------------------------------------------------------
# Stop the LiveConfig Server daemon
# ----------------------------------------------------------------------------
do_stop()
{
    if [ -x /sbin/start-stop-daemon ] && [ -r "$PIDFILE" ]; then
        # use start-stop-daemon
        /sbin/start-stop-daemon --stop --quiet --pidfile "$PIDFILE" --exec "$DAEMON" --oknodo --retry 30
	RETVAL="$?"
	[ "$RETVAL" = 2 ] && return 2
	# wait for child processes
	/sbin/start-stop-daemon --stop --quiet --exec $DAEMON --oknodo --retry=0/30/KILL/5
	return "$?"
    else
        $DAEMON -k stop 1>/dev/null
        RETVAL=$?
        [ $RETVAL -eq 0 ] && return 0
        if [ -r "$PIDFILE" ]; then
            rm $PIDFILE
        fi
        return $RETVAL
    fi
}

# ----------------------------------------------------------------------------
# Reload the LiveConfig configuration
# ----------------------------------------------------------------------------
do_reload()
{
    $DAEMON -k reload
    case "$?" in
        0) echo " successful reloaded" ;;
        *) echo " failed reloading" ;;
    esac
}


# ----------------------------------------------------------------------------
# "Main" function
# ----------------------------------------------------------------------------
case "$1" in
    start)
        log_daemon_msg "Starting $DESC" $NAME
        do_start
        log_end_msg "$?"
        ;;
    stop)
        log_daemon_msg "Stopping $DESC" $NAME
        do_stop
        log_end_msg "$?"
        ;;
    reload|force-reload)
        # log_daemon_msg "Reloading $DESC configuration" $NAME
        # do_reload
        # log_end_msg "$?"
        echo "reload/force-reload not yet supported. Please use 'restart'." >&2
        exit 3
        ;;
    restart)
        $0 stop
        $0 start
        exit 0
        ;;
    status)
        if fn_exists "status_of_proc"; then
            # use LSB function status_of_proc()
            status_of_proc -p "$PIDFILE" "$DAEMON" $NAME && exit 0 || exit $?
	elif fn_exists "status"; then
	    status -p "$PIDFILE" "$DAEMON" && exit 0 || exit $?
        else
            # IMPORTANT: pidofproc() only checks default location (/run/liveconfig/liveconfig.pid)
            pidofproc $DAEMON >/dev/null
            RETVAL=$?
            case $RETVAL in
                0)  echo "$NAME is running"
                    ;;
                4)  echo "could not access PID file for $NAME"
                    ;;
                *)  echo "$NAME is not running"
                    ;;
            esac
            exit $RETVAL
        fi
        ;;
    *)
        echo "Usage: $SCRIPTNAME {start|stop|restart|reload|force-reload|status}" >&2
        # "3" means "not implemented/supported"
        exit 3
        ;;
esac

exit 0
