#! /bin/sh
#
# Name: bootcount
# Author: SpinetiX
# Copyright: 2015 SpinetiX
# License: 2015 (c) 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:
# Default-Stop:
# Short-Description: resets the bootcount
# Description: resest the bootcount after waiting a preconfigured delay in the background.
### END INIT INFO

#
# To externally modify the bootcount call this script with the 'noreset'
# argument *before* modifying the bootcount.
#

# Init script information
NAME=bootcount
DESC="bootcount reset"

LOGGER="logger -p daemon.info -t $NAME"

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

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

# Source spxsysconf functions
. /etc/spinetix/init-functions

# Source main resources
[ -f /usr/share/resources/default/init/main ] && . /usr/share/resources/default/init/main

start() {
    local RET ERROR=
    local BCNTLIM=3 # the limit at which we trip to safe mode

    BOOTCOUNT="$(bootcount read)"

    $LOGGER "Bootcount = $BOOTCOUNT"
    echo "Bootcount = $BOOTCOUNT"

    [ -n "$rc_bootcount_lim_offset" ] && \
        [ "$rc_bootcount_lim_offset" -gt 0 ] && \
        BCNTLIM=$(( BCNTLIM + rc_bootcount_lim_offset ))

    [ -n "$DELAY" ] || DELAY="$rc_bootcount_delay"
    [ -n "$DELAY" ] || DELAY=1800 # a sane fallback
    if [ "${BOOTCOUNT:-10}" -ge $BCNTLIM ]; then
        [ "$DELAY" -ge 1800 ] || DELAY=1800 # minimum in safe mode
    fi

    echo -n "Starting $DESC: "
    echo -n "delay $DELAY secs "
    (sleep $DELAY && [ ! -f $BOOTCOUNT_NORESET ] && bootcount write 0) &
    RET=$?
    if [ $RET -eq 0 ]; then
        success; echo
    else
        failure; echo
        return 1
    fi

    return 0
}

stop() {
    local RET ERROR=

    echo -n "Stopping $DESC: "
    if [ ! -f $BOOTCOUNT_NORESET ]; then
        bootcount write 0
        RET=$?
        rm -f $BOOTCOUNT_NORESET
    else
        RET=0
    fi
    if [ $RET -eq 0 ]; then
        success; echo
    else
        failure; echo
        return 1
    fi

    return 0
}

status() {
    local RET

    COUNT=`bootcount read`
    echo -n "current boot count is $COUNT"
    if [ ! -f $BOOTCOUNT_NORESET ]; then
        echo " (will reset)"
    else
        echo " (will not reset)"
    fi
    RET=$?

    return $RET
}

parse() {
    case "$1" in
        start)
            start
            return $?
            ;;
        stop)
            stop
            return $?
            ;;
        noreset)
            touch $BOOTCOUNT_NORESET
            return $?
            ;;
        status)
            status
            return $?
            ;;
        *)
            echo "Usage: $NAME {start|stop|noreset|status}" >&2
            ;;
    esac

    return 1
}

parse $@
