#!/bin/sh
#
# wine		Allow users to run Windows(tm) applications by just clicking
#		on them (or typing ./file.exe)
#
# chkconfig: 2345 99 01
# description: Allow users to run Windows(tm) applications by just clicking \\
# on them (or typing ./file.exe)
#

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

RETVAL=0

check()
{
	[ -e /proc/sys/fs/binfmt_misc/windows -o -e /proc/sys/fs/binfmt_misc/windowsPE ]
}

start()
{
	echo -n "Registering wine services: "
	/sbin/modprobe binfmt_misc &>/dev/null
	if [ $? -ne 0 ]; then
		echo_failure
		RETVAL=$?
		echo -e '\r'
	else
		echo ':windows:M::MZ::/usr/bin/wine:' >/proc/sys/fs/binfmt_misc/register &&
		echo ':windowsPE:M::PE::/usr/bin/wine:' >/proc/sys/fs/binfmt_misc/register &&
		echo_success ||
		echo_failure
		RETVAL=$?
		echo -e '\r'
	fi
	return $RETVAL
}

stop()
{
	echo -n "Deregistering wine services: "
	if ! check; then
		echo_failure
		RETVAL=$?
		echo -e '\r'
	else
		echo '-1' >/proc/sys/fs/binfmt_misc/windows ||
		echo '-1' >/proc/sys/fs/binfmt_misc/windowsPE &&
		echo_success ||
		echo_failure
		RETVAL=$?
		echo -e '\r'
	fi
	return $RETVAL
}

restart()
{
	stop
	start
}

# See how we were called.
case "$1" in
	start)
		start
		;;
	stop)
		stop
		;;
	reload|restart)
		restart
		;;
	condstop)
		check && stop
		;;
	condrestart)
		check && restart
		;;
	status)
		for n in windows windowsPE; do
			f="/proc/sys/fs/binfmt_misc/$n"
			if [ -e "$f" ]; then
				echo "$n: `cat "$f"`"
			else
				echo "$n: absent"
			fi
		done
		;;
	*)
		echo "Usage: ${0##*/} {start|stop|status|restart|condstop|condrestart}"
		RETVAL=1
esac

exit $RETVAL
