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

# specify installation directory
INSTALL_DIR=/opt/JSCAPE_MFT_Gateway_Agent

# specify user agent 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/agent ]
then
	echo "Unable to find startup script agent."
	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 Agent: "
	case $init_style in 
		suse)
			startproc -u $USER $INSTALL_DIR/agent start
			;;
		redhat)			
			daemon --user $USER $INSTALL_DIR/agent start
			RETVAL=$?
			;;
		ubuntu)
			start-stop-daemon --start --background \
			--name jscape-agent \
			--chuid $USER \
			 --exec $INSTALL_DIR/agent -- start
			RETVAL=$?
			;;
	esac
	echo
	return $RETVAL
}

stop() {
	echo -n $"Stopping JSCAPE MFT Gateway Agent: "
	case $init_style in
		suse)
			$INSTALL_DIR/agent stop
		        RETVAL=$?	
			;;
		redhat)
			daemon --user $USER $INSTALL_DIR/agent stop
			RETVAL=$?
			;;
		ubuntu)
			$INSTALL_DIR/agent 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 $?
