#!/bin/bash
#
# chkconfig: - 92 34
# description: Starts and stops mas90 host.exe. \
#	       used to provide host side support for Mas90 Client Server. \
#              wine must be loaded, with 90WRES32.DLL copied into the \
#              drive_c/windows/system32 directory. vncserver must be setup. \
#              service vncserver start must work. Mas90 home directory \
#              is expected in drive_c/mas90 directory.

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

# Source networking configuration.
. /etc/sysconfig/network

user=root

wine=/usr/bin/wine

mas90path="c:/mas90/home/"
mas90="host.exe"
mas90port="10000"

# Check that networking is up.
[ ${NETWORKING} = "no" ] && exit 0

prog=$"Mas90 Host"

vncchannel=`ps --no-heading -fC Xvnc|awk '{print $9}'|awk -F : '{print $2}'`
if [ "$vncchannel" = "" ];
then
	/sbin/service vncserver start
fi
vncchannel=`ps --no-heading -fC Xvnc|awk '{print $9}'|awk -F : '{print $2}'`
if [ "$vncchannel" = "" ];
then
	failure $"$prog Failure: vnc server not running"
	exit 1
fi

DISPLAY=:$vncchannel.0
export DISPLAY
procs=`ps --no-header -fC"$mas90"|grep $mas90port|head -1`

start() {
    echo -n $"Starting $prog: "
    if [ "$procs" != "" ];
    then
       echo $"$prog Already running"
       success $"$prog Already running"
    else
    	RETVAL=0
	    sleep 10
	    runuser -l $user -c "$wine $mas90path$mas90 $mas90port &" >/dev/null 2>/dev/null
	    RETVAL=$?
	    [ "$RETVAL" -eq 0 ] && success $"$prog startup" || \
	        failure $"$prog start"
    fi
    echo
}

stop() {
    echo -n $"Shutting down $prog: "
    if [ "$procs" != "" ] ; then
    	kill `ps --noheader -fC"$mas90"|grep $mas90port|awk '{print $2}'`
        success $"$prog shutdown" 
    else
        failure $"$prog shutdown"
    fi
    echo
}

# See how we were called.
case "$1" in
  start)
	start
	;;
  stop)
	stop
	;;
  restart|reload)
	stop
	sleep 3
	start
	;;
  status)
	status host.exe
	;;
  *)
	echo $"Usage: $0 {start|stop|restart|status}"
	exit 1
esac

