#!/bin/bash
#
# License: Copyright 2018 SpinetiX. This file is licensed
#          under the terms of the GNU General Public License version 2.
#          This program is licensed "as is" without any warranty of any
#          kind, whether express or implied.
#
# Copyright 1999-2003 MontaVista Software, Inc.
# Copyright 2002, 2003, 2004 Sony Corporation
# Copyright 2002, 2003, 2004 Matsushita Electric Industrial Co., Ltd.
#
### BEGIN INIT INFO
# Required-Start:
# Required-Stop:
# Should-Start:
# Should-Stop:
# Default-Start: 2 3 5
# Default-Stop:
# Short-Description: Starting/stopping SpinetiX's IoT services daemon
# Description: Starting/stopping SpinetiX's IoT services daemon
### END INIT INFO

# Init script information
NAME=spxiot
DESC="SPX IoT services"

# Individual Daemon information
DAEMON=/usr/sbin/spxiotd
ARGS=""
BASENAME=spxiotd

# Load init script configuration
[ -f /etc/default/$NAME ] && . /etc/default/$NAME

# Source the init script functions
. /etc/init.d/functions

UPDATER_STATUS_FILE=/var/lib/updater/status
SAFEINDICATOR=/var/run/raperca/safe-mode
CONFIGURED_FILE=/etc/spxmanage/configured
ARYA_SETTINGS_FILE=/etc/spxmanage/arya-settings

# Verify daemons are installed
if [ ! -x $DAEMON -a "$1" != "stop" ]; then
    echo -n "Not starting $DESC $NAME, $DAEMON not installed"
    warning
    echo
    exit 0
fi

start() {
    local RET ERROR=
    local UPDATER_STATUS
    local ARYA_ENABLED

    echo -n "Starting $DESC: "

    echo -n "$NAME "

    UPDATER_STATUS=
    if [ -f "$UPDATER_STATUS_FILE" ]; then
	UPDATER_STATUS="$(< "$UPDATER_STATUS_FILE")"
    fi
    if [ "$UPDATER_STATUS" = "CORRUPTED" ]; then
	ARGS="$ARGS --fw-status=corrupted"
    fi
    if [ -f $SAFEINDICATOR ]; then
	ARGS="$ARGS --mode=safe"
    fi
    # configuration status (default is normal)
    if [ -f $CONFIGURED_FILE ]; then
	if grep -q '^[[:space:]]*WIZARD=1' $CONFIGURED_FILE; then
	    ARGS="$ARGS --cfg-status=wizard"
        fi
    else
	ARGS="$ARGS --cfg-status=unconfigured"
    fi
    if [ -f $ARYA_SETTINGS_FILE ]; then
	ARYA_ENABLED="$(sed -n -e '/^\s*ENABLED=/{s/.*=//;s/\s*//g;p}' $ARYA_SETTINGS_FILE)"
	ARGS="$ARGS --arya-enabled=$ARYA_ENABLED"
    fi
    if [ -n "$ENABLE_DEBUG" ]; then
        ARGS="$ARGS --debug"
    fi

    $DAEMON $ARGS
    RET=$?
    if [ $RET -eq 0 ]; then
	success; echo
    else
	failure; echo
	return 1
    fi
	
    return 0
}

stop () {
    local RET ERROR=

    echo -n "Stopping $DESC: $NAME "
    $DAEMON -k
    RET=$?
    if [ $RET -eq 0 ]; then
	success; echo
    else
	failure; echo
	return 1
    fi

    return 0
}

restart() {
    local RET

    echo "Restarting $DESC..."
    stop
    start
    RET=$?

    return $RET
}

condrestart() {
    local RET

    pidofproc $BASENAME >/dev/null
    RET=$?
    if [ $RET -eq 0 ]; then
	restart
	RET=$?
    else
	RET=1
    fi

    return $RET
}

reload() {
    local RET pid

    # spxiot has no support for HUP, so just restart
    condrestart
}

forcereload() {
    local RET

    reload
    RET=$?
    if [ $RET -ne 0 ]; then
	restart
	RET=$?
    fi

    return $RET
}

parse() {
    case "$1" in
	start)
	    start
	    return $?
	    ;;
	stop)
	    stop
	    return $?
	    ;;
	restart)
	    restart
	    return $?
	    ;;
	cnodrestart|try-restart)
	    condrestart
	    return $?
	    ;;
	reload)
	    reload
	    return $?
	    ;;
	force-reload)
	    forcereload
	    return $?
	    ;;
	status)
	    status $BASENAME
	    return $?
	    ;;
	*)
	    echo "Usage: $NAME " \
		"{start|stop|restart|condrestart|reload|force-reload|status}" >&2
	    ;;
    esac

    return 1
}

parse $@
