#! /bin/sh
#
### BEGIN INIT INFO
# Provides:          mcelog
# Required-Start:    $syslog $local_fs
# Required-Stop:     
# Should-Start:      
# Should-Stop:       
# Default-Start:     2 3 4 5
# Default-Stop:      
# Short-Description: Machine Check Exceptions (MCE) collector & decoder
# Description:       Machine Check Exceptions are raised by the CPU whenever
#                    it encounters an hardware error. mcelog collects and
#                    decodes Machine Check Exceptions as they happen. 
### END INIT INFO

PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin
DAEMON=/usr/sbin/mcelog
NAME=mcelog
DESC="Machine Check Exceptions decoder"
TRIGGER=/sys/devices/system/machinecheck/machinecheck0/trigger

test -x $DAEMON || exit 0

set -e

case "$1" in
  start | restart)
	echo -n "Setting up Machine Check Exceptions trigger..."
	if [ -e $TRIGGER ]; then
	    echo $DAEMON > $TRIGGER
	else
	    echo " not supported on this machine."
	    exit 0
	fi
	echo " done."

	echo -n "Running $DESC..."
	$DAEMON
	echo " done."
	;;
  stop)
	echo -n "Clearing Machine Check Exceptions trigger..."
	echo "" > $TRIGGER
	echo " done."
	;;
  force-reload)
	[ "$(cat $TRIGGER)" = $DAEMON ] && $0 restart || exit 0
	;;
  status)
	if [ "$(cat $TRIGGER)" = $DAEMON ]; then
	    echo "Machine Check Exceptions collected by mcelog."
	    exit 0
	else
	    echo "Machine Check Exceptions not collected by mcelog."
	    exit 1
	fi
	;;
  *)
	N=/etc/init.d/$NAME
	echo "Usage: $N {start|stop|restart|force-reload}" >&2
	exit 1
	;;
esac

exit 0
