#!/bin/sh
# Starts the abrt daemon
#
# chkconfig: 35 82 16
# description: Daemon to detect crashing apps
# processname: abrtd
### BEGIN INIT INFO
# Provides: abrt
# Required-Start: $syslog $local_fs
# Required-Stop: $syslog $local_fs
# Default-Stop: 0 1 2 6
# Default-Start: 3 5
# Short-Description: start and stop abrt daemon
# Description: Listen and dispatch crash events
### END INIT INFO

# Source function library.
. /etc/rc.d/init.d/functions

# Get network config
. /etc/sysconfig/network

# Get service config - may override defaults
[ -f /etc/sysconfig/abrtd ] && . /etc/sysconfig/abrtd

# Check that networking is up.
if is_yes "${NETWORKING}"; then
	if [ ! -f /var/lock/subsys/network -a "$1" != stop -a "$1" != status ]; then
		msg_network_down "abrt daemon"
		exit 1
	fi
else
	exit 0
fi

# configtest itself
# must return non-zero if check failed
# output is discarded if checkconfig is ran without details
configtest() {
	# Check if abrt is executable
	test -x /usr/sbin/abrtd || return 5
}

# wrapper for configtest
checkconfig() {
	local details=${1:-0}

	if [ $details = 1 ]; then
		# run config test and display report (status action)
		show "Checking %s configuration" "abrt daemon"; busy
		local out
		out=$(configtest 2>&1)
		RETVAL=$?
		if [ $RETVAL = 0 ]; then
			ok
		else
			fail
		fi
		[ "$out" ] && echo >&2 "$out"
	else
		# run config test and abort with nice message if failed
		# (for actions checking status before action).
		configtest >/dev/null 2>&1
		RETVAL=$?
		if [ $RETVAL != 0 ]; then
			show "Checking %s configuration" "abrt daemon"; fail
			nls 'Configuration test failed. See details with %s "checkconfig"' $0
			exit $RETVAL
		fi
	fi
}

start() {
	# Check if the service is already running?
	if [ -f /var/lock/subsys/abrtd ]; then
		msg_already_running "abrt daemon"
		return
	fi

	checkconfig
	msg_starting "abrt daemon"
	daemon /usr/sbin/abrtd
	RETVAL=$?
	[ $RETVAL -eq 0 ] && touch /var/lock/subsys/abrtd
}

stop() {
	if [ ! -f /var/lock/subsys/abrtd ]; then
		msg_not_running "abrt daemon"
		return
	fi

	# Stop daemons.
	msg_stopping "abrt daemon"
	killproc abrtd
	rm -f /var/lock/subsys/abrtd
}

condrestart() {
	if [ ! -f /var/lock/subsys/abrtd ]; then
		msg_not_running "abrt daemon"
		RETVAL=$1
		return
	fi

	checkconfig
	stop
	start
}

RETVAL=0
# See how we were called.
case "$1" in
  start)
  	start
	;;
  stop)
  	stop
	;;
  restart)
	checkconfig
	stop
	start
	;;
  try-restart)
	condrestart 0
	;;
  force-reload)
	condrestart 7
	;;
  checkconfig|configtest)
	checkconfig 1
	;;
  status)
	status abrtd
	RETVAL=$?
	;;
  *)
	msg_usage "$0 {start|stop|restart|try-restart|force-reload|checkconfig|status}"
	exit 3
esac
