#!/bin/sh
#
# Backtrace I/O start-up script for coronerd crash reporting.
#
# Copyright (C) 2014 Backtrace I/O.
# All Rights Reserved.
#
# All information contained herein is, and remains the property of Backtrace
# I/O and its suppliers, if any.  The intellectual and technical concepts
# contained herein are proprietary to Backtrace I/O and its suppliers and may
# be covered by U.S. and Foreign Patents, patents in process, and are protected
# by trade secret or copyright law.  Dissemination of this information or
# reproduction of this material is strictly forbidden unless prior written
# permission is obtained from Backtrace I/O Incorporated.
#
### BEGIN INIT INFO
# Provides:		coronerd-crash-report
# Required-Start:	$syslog $local_fs $remote_fs $network
# Default-Start:	2 3 4 5
# Default-Stop:		0 1 6
# Short-Description:	Backtrace I/O database error reporting
# Description:		Reports database errors to Backtrace I/O
### END INIT INFO

EXIT_SUCCESS=0
EXIT_FAILURE=1

PATH="/sbin:/bin:/usr/sbin:/usr/bin"
export PATH

DESC="Backtrace I/O database error reporting"
NAME=coroner
CORONERD_PATH="/opt/backtrace/bin/coroner"
PID_DIR=/var/run/coronerd
PID_FILE=${PID_DIR}/${NAME}.pid
SCRIPTNAME=/etc/init.d/${NAME}
CF="/etc/coronerd/coroner.cf"
OPTIONS="-c ${CF} daemon -f"
MONITOR="/opt/backtrace/bin/monitor"
WATCHDOG_INTERVAL="120"
WATCHDOG_START_INTERVAL="300"

. /etc/init.d/functions

test $UID = 0 || exit 4

# Verify that all relevant binaries are present.
if test ! -x $CORONERD_PATH; then
	log_failure_msg "$CORONERD_PATH not found"
	exit $EXIT_FAILURE
fi

mkdir -m0755 -p ${PID_DIR}

case "$1" in
start)
	test -f $CF || exit 6
	echo -n "Starting $DESC ($NAME): "
	$MONITOR --pidfile="$PID_FILE"					\
	    --watchdog-interval="$WATCHDOG_INTERVAL"			\
	    --watchdog-start-interval="$WATCHDOG_START_INTERVAL"	\
	    $CORONERD_PATH $OPTIONS
	echo
	exit $?
	;;
stop)
	echo -n "Stopping $DESC ($NAME): "
	killproc $NAME
	RT=$?
	[ $RT = 0 ] && rm -f $PIDFILE
	echo
	exit $RT
	;;
restart)
	$0 stop
	$0 start
	;;
status)
	status -p $PIDFILE $NAME
	exit $?
	;;
*)
	echo "Usage: $SCRIPTNAME {start|stop|restart|status}" >&2
	exit $EXIT_FAILURE
	;;
esac

exit $EXIT_SUCCESS
