#!/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"

. /lib/lsb/init-functions

uid=$(/usr/bin/id -u) && [ "$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
	log_daemon_msg "Starting $DESC" $NAME
	start-stop-daemon --start --oknodo --quiet --pidfile $PID_FILE 	\
	    --exec $MONITOR --						\
	    --pidfile="$PID_FILE"					\
	    --watchdog-interval="$WATCHDOG_INTERVAL"			\
	    --watchdog-start-interval="$WATCHDOG_START_INTERVAL"	\
	    $CORONERD_PATH $OPTIONS
	log_end_msg $?
	;;
stop)
	log_daemon_msg "Stopping $DESC" $NAME
	start-stop-daemon --stop --retry 30 --oknodo --quiet \
	    --pidfile $PID_FILE --exec $MONITOR
	if test "$?"; then
		rm -f $PID_FILE
	fi
	log_end_msg $?
	;;
restart)
	$0 stop
	$0 start
	;;
status)
	status_of_proc -p "$PID_FILE" "$MONITOR" $NAME && \
	exit $?
	;;
*)
	echo "Usage: $SCRIPTNAME {start|stop|restart|status}" >&2
	exit $EXIT_FAILURE
	;;
esac

exit $EXIT_SUCCESS
