#!/bin/sh
#
# apport    Script to control apport handling of core dumps
#
# chkconfig: 2345 90 10
# description:  Starts and stops apport crash handling

# $Id: apport.init,v 1.6 2008/08/24 18:28:42 patrys Exp $

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

# The location of the core pattern file
PATFILE=/proc/sys/kernel/core_pattern
# The location of the apport binary
APPORT='/usr/bin/apport %p %s %c'
# Location to save the old core_pattern
OLDPAT=/var/run/core_pattern

# Return success if apport is already enabled
apport_is_enabled() {
    # XXX check the lock here too?
    grep -q "^|.*apport" $PATFILE
}

enable_apport() {
    if ! apport_is_enabled; then 
        cat $PATFILE > $OLDPAT
        echo "|$APPORT" > $PATFILE
    fi
}

disable_apport() {
    if apport_is_enabled; then
        cat $OLDPAT > $PATFILE
        rm -f $OLDPAT
    fi
}

start() {
	if [ ! -f /var/lock/subsys/apport ]; then
		show "Enabling Apport crash handling"
		busy
		enable_apport
		touch /var/lock/subsys/apport
		ok
	else
		msg_already_running "Apport crash handling"
	fi
}

stop() {
	if [ -f /var/lock/subsys/apport ]; then
		show "Disabling Apport crash handling"
		busy
		disable_apport
		rm -f /var/lock/subsys/apport
		ok
	else
		msg_not_running "Apport crash handling"
	fi
}

RETVAL=0
# See how we were called.
case "$1" in
  start)
	start
	;;
  stop)
	stop
	;;
  status)
	# FIXME are these the right return values?
	if grep -q 'apport' $PATFILE; then
		echo "Apport is enabled."
		exit 0
	else
		echo "Apport is disabled."
		exit 1
	fi
	;;
  restart|reload)
	stop
	start
	;;
  *)
	msg_usage "$0 {start|stop|status|restart|reload}"
	exit 3
esac

exit $RETVAL
