#!/bin/bash
#
# ozeking       This shell script takes care of starting and stopping
#               ozeking (Ozeki NG SMS Gateway daemon).
#
# chkconfig: - 58 74
# description: ozeking is the Ozeki NG SMS Gateway daemon. \
# Ozeki NG SMS Gateway is used to send and receive SMS and MMS messages \
# using a GSM/GPRS modem or through IP SMS connections, such as SMPP, \
# UCP or CIMD2

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

prog=ozeking
lockfile=/var/lock/subsys/$prog
progdir=/var/lib/ozeking

start() {
        echo $"starting $prog: "
        nohup /usr/bin/Xvfb :9 -screen 0 1152x900x8 >> $progdir/outputxvfb.txt 2>> $progdir/extraxvfb.txt &
        nohup /usr/bin/twm -display :9 >> $progdir/outputtwm.txt 2>> $progdir/extratwm.txt &
        (export DISPLAY=":9"; cd $progdir; /usr/bin/mono ./OzekiNG.exe /run >> $progdir/output.txt 2>> $progdir/extra.txt &)
        RETVAL=$?
        echo
        [ $RETVAL -eq 0 ] && touch $lockfile
        return $RETVAL
}

stop() {
        echo $"Shutting down $prog: "

	# Find the PID of OzekiNG
	ng_pid=`ps -ef | grep OzekiNG | grep -v grep | awk '{ print $2 }'`
	# If OzekiNG is running, stop it.
	if [ $ng_pid -gt 0 ]
	then
		kill -9 $ng_pid
	fi

	# Find the PID of Xvfb
	Xvfb_pid=`ps -ef | grep Xvfb | grep -v grep | awk '{ print $2 }'`
	if [ $Xvfb_pid -gt 0 ]
	then
		kill -9 $Xvfb_pid
	fi

	# Find the PID of twm
	twm_pid=`ps -ef | grep twm | grep -v grep | awk '{ print $2 }'`
	# If twm is running, stop it.
	if [ $twm_pid -gt 0 ]
	then
		kill -9 $twm_pid
	fi


        RETVAL=$?
        echo
        [ $RETVAL -eq 0 ] && rm -f $lockfile
        return $RETVAL
}

# See how we were called.
case "$1" in
  start)
        start
        ;;
  stop)
        stop
        ;;
  status)
        status $prog
        ;;
  restart|force-reload)
        stop
        start
        ;;
  try-restart|condrestart)
        stop
        start
        ;;
  reload)
        exit 3
        ;;
  *)
        echo $"Usage: $0 {start|stop|status|restart|try-restart|force-reload}"
        exit 2
esac

