#!/bin/bash
#
# wine -- Script to registering and unregistering binary handler
#         for Windows applications
#
# chkconfig: 35 98 2
# description: Allow users to run Windows(tm) applications by just clicking \
#              on them (or typing ./file.exe)
#
# Copyright (c) 2003-2007 by Davide Madrisan <davide.madrisan@qilinux.it>

. /etc/sysconfig/rc
. $rc_functions

NAME=wine
LOCKFILE=/var/lock/subsys/$NAME

rc=0

case "$1" in
   start)
      echo -n $"Registering binary handler for Windows applications: "
      if [ -e /proc/sys/fs/binfmt_misc/windows ]; then
         echo_success
         echo
         exit 0
      fi

      ( /sbin/modprobe binfmt_misc &&
        mount -t binfmt_misc none /proc/sys/fs/binfmt_misc ) &>/dev/null

      if [ "$(mount 2>/dev/null | grep binfmt_misc)" ]; then
         #:name:type:offset:magic:mask:interpreter:flags
         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
         touch $LOCKFILE
      else
         echo_failure
         echo
         rc=1
      fi
   ;;
   stop)
      echo -n $"Unregistering binary handler for Windows applications: "
      if [ -e /proc/sys/fs/binfmt_misc/windows ]; then
         [ -f /proc/sys/fs/binfmt_misc/windows ] &&
            echo "-1" > /proc/sys/fs/binfmt_misc/windows
         [ -f /proc/sys/fs/binfmt_misc/windowsPE ] &&
            echo "-1" >/proc/sys/fs/binfmt_misc/windowsPE
      fi
      rm -f $LOCKFILE
      echo_success
      echo
   ;;
   status)
      if [ -e /proc/sys/fs/binfmt_misc/windows ]; then
         echo $"Wine binary format handlers are registered."
      else
         echo $"Wine binary format handlers are not registered."
         rc=3
      fi
   ;;
   restart)
      $0 stop
      sleep 1
      $0 start
   ;;
   condrestart)
      [ -e /proc/sys/fs/binfmt_misc/windows ] && $0 restart || :
   ;;
   *)
      echo $"Usage: ""$prog {start|stop|status|restart|condrestart}"
      exit 1
   ;;
esac

exit $rc
