#!/bin/sh
#
# winedummy    Starts, Stops and Reloads WINE for Windows Services autostart support.
#
# chkconfig: 2345 90 60
# description: WINE DUMMY daemon.
# processname: wineserver
# pidfile: /var/run/winedummy.pid
#

# Installation directory
WINE_BIN=/usr/local/bin
#SSA_BIN=/usr/local/sbin/SSA
WINE_PID=/var/run/winedummy.pid
WINE_LOCK=/var/lock/subsys/winedummy

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

if ! [ -x $WINE_BIN/wine ] ; then
	echo "ERROR: $WINE_BIN/wine not found"
	exit 0
fi

# Full path to wine binary
DAEMON=$WINE_BIN/wine

RETVAL=0

start() {
	echo -n $"Starting winedummy: "
	#$SSA_BIN/SSA&
	LANG=ru_RU.UTF-8 $WINE_BIN/wineserver -p -d0
	$WINE_BIN/wineboot
	#$DAEMON net start Asterisk_Segment_Service_0 > /dev/null 2> /dev/null
	#$DAEMON net start Kern_Service > /dev/null 2> /dev/null
	#$DAEMON net start RINOAutomaticService > /dev/null 2> /dev/null
	RETVAL=$?
	#RETVAL=0
	[ $RETVAL -eq 0 ] && echo $(pidof -s wineserver)>$WINE_PID
	[ $RETVAL -eq 0 ] && touch $WINE_LOCK
	echo
	return $RETVAL
}

stop() {
	echo -n $"Stopping winedummy: "
	$DAEMON net stop RINOAutomaticService > /dev/null 2> /dev/null
	$DAEMON net stop Kern_Service > /dev/null 2> /dev/null
	$DAEMON net stop Asterisk_Segment_Service_0 > /dev/null 2> /dev/null
	#killall -g services.exe > /dev/null 2> /dev/null
	$WINE_BIN/wineserver -k
	sleep 10s
	RETVAL=$?
	[ $RETVAL -ne 0 ] && killproc wineserver
	[ $RETVAL -ne 0 ] && RETVAL=$?
	#killproc SSA
	[ $RETVAL -eq 0 ] && rm -f $WINE_LOCK
	[ $RETVAL -eq 0 ] && rm -f $WINE_PID
	echo
	return $RETVAL
}

restart() {
	stop
	start
}

winedummy_status() {
	if [ -f /var/lock/subsys/winedummy ]; then
	    echo $"winedummy is running"
	    return 0
	else
	    echo $"winedummy is stopped"
	    return 3
	fi
}

case "$1" in
  start)
	[ -f $WINE_LOCK ] && stop || :
	start
	#status winedummy
	winedummy_status
	RETVAL=$?
	;;
  stop)
	stop
	#status winedummy
	winedummy_status
	RETVAL=$?
	;;
  restart)
	restart
	;;
  condrestart)
	[ -f $WINE_LOCK ] && restart || :
	;;
  status)
	#status winedummy
	winedummy_status
	RETVAL=$?
	;;
  *)
	echo "Usage: winedummy {start|stop|restart|condrestart|status}"
	exit 1
esac

exit $?
