#!/bin/sh
#
#   chkconfig: 345 93 7
#   description: Startup/shutdown script for Mirth.
#
# We can use mirth-daemon, but $JAVA_HOME/bin needs to be in 
# the path first.
#


JAVA_HOME=./usr/lib/jvm/java-1.5.0-sun-1.5.0.15/
export JAVA_HOME
PATH=${PATH}:$JAVA_HOME/bin
MIRTH_HOME="/opt/mirth"
MIRTH_USER=mirth


start() {
        su $MIRTH_USER -c "$MIRTH_HOME/mirth-daemon start"
	echo -n "Waiting for Mirth to start"
	count=1
	while true
	do
	    if [ $count -gt 60 ]; then
	        echo "mirth failed to start." 1>&2
		exit 1
	    fi
	    echo -n "."
	    count=`expr $count + 1`
	    netstat -an | grep 8443 | grep LISTEN > /dev/null
	    if [ $? -eq 0 ]; then
		break
	    fi
	    sleep 1
	done
	echo ""
}

stop() {
        su $MIRTH_USER -c "$MIRTH_HOME/mirth-daemon stop"
}

status() {
        su $MIRTH_USER -c "$MIRTH_HOME/mirth-daemon status"
}

### main logic ###
case "$1" in
  start)
        start
        ;;
  stop)
        stop
        ;;
  status)
        status mirth
        ;;
  restart|reload|condrestart)
        stop
        start
        ;;
  *)
        echo "Usage: $0 {start|stop|restart|reload|status}"
        exit 1
esac

exit 0
