#!/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: 2 3 5
# Default-Stop:
# Short-Description: Starting/stopping SpinetiX's content uploader/updater
# Description: Starting/stopping SpinetiX's content uploader/updater
### END INIT INFO

# Init script information
NAME=uploader
DESC="SPX content uploader"

# Individual Deamon information
DAEMON=/usr/sbin/uploader
CONFFILE=/etc/raperca/uploaderconf.xml
LOGCONF=/etc/raperca/uploaderlog.xml
PASSFILE=/etc/raperca/spxpasswd.xml
ARGS="-daemon -config $CONFFILE -logconf $LOGCONF -pass $PASSFILE -nopreview -pause publish"
BASENAME=${DAEMON##*/}

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

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

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

# 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 BOOTCOUNT
    local BCNTLIM=4

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

    echo -n "Starting $DESC: "

    BOOTCOUNT="$(bootcount read)"

    if [ "${BOOTCOUNT:-10}" -ge $BCNTLIM ]; then
	echo -n "starting in safe mode (bootcount >= $BCNTLIM)"
	ARGS="$ARGS -safemode"
	echo -n ", "
    fi

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

stop () {
    local RET

    echo -n "Stopping $DESC: $NAME "
    killproc $BASENAME
    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

    echo -n "Reloading $DESC configuration "
    pid=`pidofproc $BASENAME` && kill -HUP $pid && success || failure 1
    RET=$?
    echo

    return $RET
}

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



