#!/bin/sh
#
# winex		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)
# processname: wine
# Modified by Lav <lav@altlinux.ru>
# - print name of service based on the script name
# - review script

# Do not load RH compatibility interface.
WITHOUT_RC_COMPAT=1

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

RETVAL=0

PROCBIN=/proc/sys/fs/binfmt_misc

check()
{
	[ -e $PROCBIN/windows -o -e $PROCBIN/windowsPE ]
}

start()
{
	echo -n "Registering binary handler for Windows program (`basename $0 .init` 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:' >$PROCBIN/register &&
		echo ':windowsPE:M::PE::/usr/bin/wine:' >$PROCBIN/register &&
		echo_success || echo_failure
		RETVAL=$?
		echo -e '\r'
	fi
	return $RETVAL
}

stop()
{
		echo -n "Deregistering binary handler for Windows program (`basename $0 .init` services): "
        if ! check; then
        	echo_failure
        	RETVAL=$?
        	echo -e '\r'
	else
	        echo '-1' >$PROCBIN/windows &&
	        echo '-1' >$PROCBIN/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
		;;
	restart)
		restart
		;;
	condstop)
	        check && stop
	        ;;
	condrestart)
		;;
	status)
			echo "Wine binary format handlers information:"
	        for n in windows windowsPE; do
		        f="$PROCBIN/$n"
	        	if [ -e "$f" ]; then
				echo
				echo "Handler $n: `cat "$f"`"
			else
			        echo "$n: absent"
				RETVAL=1
			fi
		done
		;;
	*)
		msg_usage "${0##*/} {start|stop|restart|condstop|condrestart|status}"
		RETVAL=1
esac

exit $RETVAL
