#!/bin/bash
#
#	/etc/rc.d/init.d/elfspe
#
#	registers elfspe handler
#
# chkconfig: 345 1 1 
# description: executes elfspe-register

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


start() {
	echo -n $"Starting elfspe: "
	if [ ! -e /proc/sys/fs/binfmt_misc/register ]; then
		initlog $INITLOG_ARGS -c "/sbin/insmod binfmt_misc"
		initlog $INITLOG_ARGS -c "mount -t binfmt_misc none /proc/sys/fs/binfmt_misc"
		if [ ! -e /proc/sys/fs/binfmt_misc/register ]; then
			failure $"binfmt_misc could not be loaded"
			echo
			exit 1
		fi
	fi

	[ ! -f /proc/sys/fs/binfmt_misc/spe ] && sh /usr/bin/elfspe-register

	if [ -f /proc/sys/fs/binfmt_misc/spe ]; then
		success
		echo
		return 0
	else
		failure $"could not start elfspe"
		echo
		exit 1
	fi
}

stop() {
	echo -n "Shutting down elfspe: "
	if [ ! -f /proc/sys/fs/binfmt_misc/spe ]; then
	    success
	    echo
	    return 0
	fi
	if sh /usr/bin/elfspe-unregister; then
	    success
	    echo
	    return 0
	else
	    failure $"could not stop elfspe"
	    echo
	    exit 1
	fi
}

restart() {
	stop
	start
}

case "$1" in
    start)
	start
	;;
    stop)
	stop
	;;
    status)
	[ -f /proc/sys/fs/binfmt_misc/spe ] && echo "elfspe started" || echo "elfspe not started"
	;;
    restart)
	restart
	;;
    reload)
	;;
    condrestart)
	[ -f /proc/sys/fs/binfmt_misc/spe ] && restart || :
	;;
    *)
	echo "Usage: elfspe {start|stop|status|reload|restart}"
	exit 1
	;;
esac
exit $?
