#!/bin/bash
#
# java_binfmt   Allow execution of Java applications and applets just like any other program
#
# chkconfig: 2345 99 10
# description: Allow execution of Java applications and applets just like any other program
#
### BEGIN INIT INFO
# Provides: java_binfmt
# Default-Start: 2 3 4 5
# Short-Description: Allow execution of Java applications and applets
# Description: Allow execution of Java applications and applets just like any other program
### END INIT INFO

SERVICE=java_binfmt
NAME=Java

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

RETVAL=0

case "$1" in
	start)
	gprintf "Starting %s: " "$SERVICE"
	if [ -e /proc/sys/fs/binfmt_misc/Java -a -e /proc/sys/fs/binfmt_misc/Applet -a -e /proc/sys/fs/binfmt_misc/ExecutableJAR ]; then
		failure
		echo
		gprintf "$NAME registration already enabled!\n"
		exit 1
	fi
	/sbin/modprobe binfmt_misc &>/dev/null
	RETVAL=$?
	echo ':Java:M::\xca\xfe\xba\xbe::/usr/bin/javawrapper:' >/proc/sys/fs/binfmt_misc/register
	echo ':Applet:E::html::/usr/bin/appletviewerwrapper:' >/proc/sys/fs/binfmt_misc/register
	echo ':ExecutableJAR:E::jar::/usr/bin/jarwrapper:' >/proc/sys/fs/binfmt_misc/register
	success
	echo
	[ $RETVAL -eq 0 ] && touch /var/lock/subsys/$SERVICE
	;;
    	stop)
	gprintf "Stopping %s: " "$SERVICE"
	if ! [ -e /proc/sys/fs/binfmt_misc/Java -a -e /proc/sys/fs/binfmt_misc/Applet -a -e /proc/sys/fs/binfmt_misc/ExecutableJAR ]; then
		failure
		echo
		gprintf "$NAME registration already disabled!\n"
		exit 1
	fi
	echo "-1" >/proc/sys/fs/binfmt_misc/Java
	echo "-1" >/proc/sys/fs/binfmt_misc/Applet
	echo "-1" >/proc/sys/fs/binfmt_misc/ExecutableJAR
	RETVAL=$?
	success
	echo
	[ $RETVAL -eq 0 ] && rm -f /var/lock/subsys/$SERVICE
	;;
	status)
	if [ -e /proc/sys/fs/binfmt_misc/Java -a -e /proc/sys/fs/binfmt_misc/Applet -a -e /proc/sys/fs/binfmt_misc/ExecutableJAR ]; then
		gprintf "$NAME registration enabled\n"
	else
		gprintf "$NAME registration disabled\n"
	fi
	;;
	restart|reload)
	$0 stop
	$0 start
	;;
	*)
	gprintf "Usage: %s {start|status|stop}\n" "$SERVICE"
	exit 1
esac

exit $RETVAL
