#!/bin/sh
# $Id: binfmt-detector.init,v 1.7 2007/03/29 15:59:59 arekm Exp $
#
# binfmt-detector:	Microsoft PE executable type detector. User can run
#			programs using Wine or Mono simply by clicking on them
#			or typing ./file.exe
#
# chkconfig:	2345 95 10
#
# description:	Microsoft PE executable type detector. User can run
#		programs using Wine or Mono simply by clicking on them
#		or typing ./file.exe
#

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

BINFMT_DIR='/proc/sys/fs/binfmt_misc'

[ ! -d "$BINFMT_DIR" ] && exit 0

start() {
	if [ ! -f /var/lock/subsys/binfmt-detector ]; then
		# check if binfmt_misc is not already mounted
		if ! grep -q "${BINFMT_DIR}" /proc/mounts; then
			/bin/mount none -t binfmt_misc ${BINFMT_DIR}
		fi
		msg_starting 'binfmt-detector'
		if [ -e "$BINFMT_DIR/register" ]; then
			echo ':windows:M::MZ::/usr/bin/binfmt-detector.sh:' > ${BINFMT_DIR}/register
			echo ':windowsPE:M::PE::/usr/bin/binfmt-detector.sh:' > ${BINFMT_DIR}/register
			ok
			touch /var/lock/subsys/binfmt-detector
		else
			fail
		fi
	else
		msg_already_running 'binfmt-detector'
	fi
}

stop() {
	if [ -f /var/lock/subsys/binfmt-detector ]; then
		msg_stopping 'binfmt-detector'
		echo '-1' > ${BINFMT_DIR}/windows
		echo '-1' > ${BINFMT_DIR}/windowsPE
		rm -f /var/lock/subsys/binfmt-detector
		ok
	else
		msg_not_running 'binfmt-detector'
	fi
}

RETVAL=0
case "$1" in
  start|reload|force-reload)
  	start
	;;
  stop)
  	stop
	;;
  restart)
    stop
    start
	;;
  status)
	;;
  *)
	msg_usage "$0 {start|stop|restart|reload|force-reload|status}"
	exit 3
esac

exit $RETVAL
