#!/bin/bash
#
# This shell script takes care of starting and stopping the JSCAPE MFT Gateway. 
#
# chkconfig: 2345 66 09
# description: manages JSCAPE MFT Gateway services
#

# specify installation directory
INSTALL_DIR=/opt/JSCAPE_MFT_Gateway

# specify user server should run as
USER=root

# Source function library.
if [ -f /etc/init.d/functions ] ; then
  . /etc/init.d/functions
elif [ -f /etc/rc.d/init.d/functions ] ; then
  . /etc/rc.d/init.d/functions
fi

if [ ! -f $INSTALL_DIR/gateway ]
then
	echo "Unable to find startup script gateway."
	exit 1
fi 

# Check what OS is used
if [ -x /sbin/startproc ]; then
    init_style="suse"
else if [ -x /sbin/start-stop-daemon ]; then
	init_style="ubuntu"
    else
	init_style="redhat"
    fi
fi



RETVAL=0

umask 077

start() {
	echo -n $"Starting JSCAPE MFT Gateway: "
	case $init_style in 
		suse)
			startproc -u $USER $INSTALL_DIR/gateway start
			;;
		redhat)			
			daemon --user $USER $INSTALL_DIR/gateway start
			RETVAL=$?
			;;
		ubuntu)
			start-stop-daemon --start --background \
			--name jscape \
			--chuid $USER \
			 --exec $INSTALL_DIR/gateway -- start
			RETVAL=$?
			;;
	esac
	echo
	return $RETVAL
}

stop() {
	echo -n $"Stopping JSCAPE MFT Gateway: "
	case $init_style in
		suse)
			$INSTALL_DIR/gateway stop
		        RETVAL=$?	
			;;
		redhat)
			daemon --user $USER $INSTALL_DIR/gateway stop
			RETVAL=$?
			;;
		ubuntu)
			$INSTALL_DIR/gateway stop
			RETVAL=$?
			;;
	esac			
	echo
	return $RETVAL
}

restart() {
	stop
	start
}

case "$1" in 
	start)
	start
	;;
	stop)
	stop
	;;
	restart|reload)
	restart
	;;
	*)
	echo $"Usage: $0 {start|stop|restart}"
	exit 1
esac

exit $?
