#!/bin/sh
#
# License: Copyright 2015 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: 4
# Default-Stop:
# Short-Description: Starting/stopping SpinetiX's dummy testing watchdog
# Description: Starting/stopping SpinetiX's dummy testing watchdog
### END INIT INFO
#

# Init script information
NAME=spxtest-watchdog
DESC="spxtest watchdog"

# watchdog daemon
DAEMON=/usr/sbin/watchdog
ARGS="-c /etc/spxtest/watchdog.conf"
BASENAME=watchdog
PIDFILE=/var/run/$BASENAME.pid

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

# 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

start() {
    local RET
    local f

    # Stop any potentially running watchdog daemon first
    if start-stop-daemon -K --quiet --pidfile $PIDFILE; then
        echo -n "Waiting for existing watchdog daemon to exit..."
        sleep 6
        echo
    fi

    echo -n "Starting $DESC: "
    echo -n "$BASENAME "
    start-stop-daemon -S --quiet --exec $DAEMON -- $ARGS
    RET=$?
    if [ $RET -eq 0 ]; then
	echo -n ", "
    else
	echo -n " "; failure; echo
	return 1
    fi
	
    echo -n "led "
    for f in /sys/class/leds/system:green:status /sys/class/leds/*:green:status1; do
	[ -d "$f" ] || continue
	echo heartbeat > "$f"/trigger
    done

    success; echo

    return 0
}

stop () {
    local RET
    local f

    echo -n "Stopping $DESC: "
    echo -n "led, "
    for f in /sys/class/leds/system:green:status /sys/class/leds/*:green:status1; do
	[ -d "$f" ] || continue
        if egrep -q '(^|\s|\[)fixed-timer(\]|\s|$)' "$f"/trigger; then
            echo fixed-timer > "$f"/trigger
        else
            echo timer > "$f"/trigger
            echo 62 > "$f"/delay_on
            echo 63 > "$f"/delay_off
        fi
    done

    echo -n "sync"
    sync
    RET=$?
    if [ $RET -eq 0 ]; then
	echo -n ", "
    else
	echo -n " failed, "
    fi
    echo -n "$BASENAME"
    start-stop-daemon -K --quiet --pidfile $PIDFILE
    RET=$?
    if [ $RET -eq 0 ]; then
	echo -n ". The device will hard reboot if no watchdog daemon is restarted within 1 min. "
	success; echo
    else
	echo -n " "
	failure; echo
	return 1
    fi

    return 0
}

restart() {
    local RET

    echo "Restarting $DESC..."
    stop
    sleep 6
    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

    echo -n "Reloading $DESC configuration not supported "
    failure

    return 1
}

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 $?
	    ;;
	condrestart|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 $@

