#!/bin/bash
#
# License: Copyright 2020 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=spxucd
DESC="SPX uC services"

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

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

# 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

# Only Fukiran (i.e. HMP400) boards DV5+ (rev Bxxx, rev Cxxx) and DV5
# engineering samples (rev 5001) have the uController, if this chages we
# also need to adapt the watchdog configurations
[ -f /sys/class/spx-board-info/id/model ] && \
    BOARD_MODEL="$(< /sys/class/spx-board-info/id/model)" || BOARD_MODEL=
[ -f /sys/class/spx-board-info/id/revision ] && \
    BOARD_REV="$(< /sys/class/spx-board-info/id/revision)" || BOARD_REV=
if [ "$BOARD_MODEL" = "HMP400" ]; then
    case "$BOARD_REV" in
    B*|C*|5001)
        HAS_UC=1
        ;;
    *)
        HAS_UC=
        ;;
    esac
fi

start() {
    local RET ERROR=

    if [ -z "$HAS_UC" ]; then
        echo "Skipping $DESC, this board is not equipped with a uC"
        return 0
    fi

    echo -n "Starting $DESC: "

    echo -n "$NAME "

    : > /run/spxucd/present

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

    return 0
}

stop () {
    local RET ERROR=

    [ -n "$HAS_UC" ] || return 0

    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

    [ -n "$HAS_UC" ] || return 0

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

    return $RET
}

condrestart() {
    local RET

    [ -n "$HAS_UC" ] || return 0

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

    return $RET
}

reload() {
    local RET pid

    [ -n "$HAS_UC" ] || return 0

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

forcereload() {
    local RET

    [ -n "$HAS_UC" ] || return 0

    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 $@
