#!/bin/sh
#
# Copyright (c) 2011 by Axway Software
# All rights reserved.
# This document and the software described in this document are the confidential and proprietary
# intellectual property of Axway Software and are protected as Axway Software trade secrets.
# No part of this software or this document may be reproduced or disseminated in any form or
# by any means without the prior written permission of Axway Software.
#

# Default
SHUTDOWN_MODE=
TIMEOUT=120
BACKGROUND=YES

# Component name to be displayed
COMPONENT_NAME=Integrator

#
# =======================================
# Check if a process is running
#
running() {
	if test -f $1 ; then
		kill -0 `cat $1` 2> /dev/null
		if test $? -eq 0 ; then
			# Process is running
			return 0
		fi
	fi
	# PID file does not exist or process is not running
	return 1
}

#
# =======================================
# Change upper character to lower character
#
tolower()
{
	target=`echo $1 | tr "[A-Z]" "[a-z]"`
	echo $target
}
#
# =======================================
# Start Integrator
#
start() {
	CMD_SURVIVE="survive -sterm -d5 -skill -p $CORE_DATA/pid/$CORE_HOST -P $CORE_DATA/pid/$CORE_HOST.grp"
	if running $CORE_DATA/pid/$CORE_HOST ; then
		echo "The servers are already running!"
	else
		$CMD_ST_SETMODE $SHUTDOWN_MODE $CMD_ST_SETMODE_OPTIONS
		if test "$BACKGROUND" = "YES" ; then
			echo "Starting $COMPONENT_NAME..."
			$CMD_SURVIVE "$CMD_STARTER $CMD_STARTER_OPTIONS" 2>>$STARTER_LOG 1>&2 &
		else
			echo "Starting $COMPONENT_NAME..."
			$CMD_SURVIVE "$CMD_STARTER $CMD_STARTER_OPTIONS" 2>>$STARTER_LOG 1>&2
		fi
	fi
}

#
# =======================================
# Start Integrator and wait for it to stop
#
startForeground() {
	BACKGROUND=NO
	start
}

#
# =======================================
# Stop Integrator 
#
normalStop() {
	echo "Stopping $COMPONENT_NAME..."
	r4edi starterstop.x4 \
		-p ${CORE_SYSSTARTER_PORT} \
		-a $CORE_LOCAL/config/passwd \
		-t $TIMEOUT
	if test "$?" = 0; then
		if running $CORE_DATA/pid/$CORE_HOST ; then
			kill -15 `cat $CORE_DATA/pid/$CORE_HOST`
		fi
	fi
}

#
# =======================================
# Kill Integrator processes
#
forceStop() {
	echo "Stopping $COMPONENT_NAME with force mode..."
	if running $CORE_DATA/pid/$CORE_HOST ; then
		kill -9 `cat $CORE_DATA/pid/$CORE_HOST`
	fi
	if running $CORE_DATA/pid/$CORE_HOST.grp ; then
		kill -9 -`cat $CORE_DATA/pid/$CORE_HOST.grp`
	fi
}

#
# =======================================
# Get Integrator status
#
status() {
	if running $CORE_DATA/pid/$CORE_HOST ; then
		echo "$COMPONENT_NAME status is: running"
		exit 0;
	else
		echo "$COMPONENT_NAME status is: stopped"
		exit 1;
	fi
}

#
# =======================================
# Display script help
#
display_help() {
	echo "Usage: $COMPONENT_NAME [commands]"
	echo "Controls the $COMPONENT_NAME Server."
	echo ""
	echo "Commands include:"
	echo "    start [-S]           Starts $COMPONENT_NAME in the background."
	echo "                         Option:"
	echo "                             -S Does not start Tasks."
	echo "    stop [-t <timeout>]  Stops $COMPONENT_NAME."
	echo "                         Option:"
	echo "                             -t Specifies timeout in seconds."
	echo "    status               Displays whether $COMPONENT_NAME is running or stopped."
	echo "    force-stop           Kills $COMPONENT_NAME processes."
	echo "    start-and-wait [-S]  Starts $COMPONENT_NAME then waits for it to stop."
	echo "                         Option:"
	echo "                              -S Does not start Tasks."
	echo "    -h                   Displays usage."
	echo ""
	echo "Other commands:"
	echo "To see the options and arguments for these commands, type: $COMPONENT_NAME <command> -help"
	echo ""
	$CALL_BOOTSTRAP --help
}

if [ ! -f /home/bucxib5/Axway/Integrator/profile ]; then
	echo "Cannot find profile in /home/bucxib5/Axway/Integrator"
	exit 128
fi
	. /home/bucxib5/Axway/Integrator/profile

if test "$CORE_ROOT" = "" ; then
	echo "CORE_ROOT environment variable must be set before running this script."
	exit 128
fi

CMD_ST_SETMODE="r4edi -i st_setmode.x4"
CMD_ST_SETMODE_OPTIONS="\
	-p ${CORE_SYSSTARTER_PORT}\
	-a $CORE_LOCAL/config/passwd\
	-h $CORE_PORTER_HOST\
	$CORE_DATA/starter/starter.cfg"

CMD_STARTER="r4edi -i starter.x4"

if test "$CORE_SECONDARY_NODE" = "YES" ; then
	CMD_STARTER_OPTIONS="\
		-p ${CORE_SYSSTARTER_PORT}\
		-a $CORE_LOCAL/config/passwd\
		-h $CORE_PORTER_HOST\
		-s $CORE_PORTER_PORT\
		-e $CORE_ROOT/config/environment.dat\
		-e $CORE_LOCAL/config/environment.dat"
	STARTER_LOG="$CORE_DATA/log/starter_$CORE_HOST.log"
else
	CMD_STARTER_OPTIONS="\
		-p ${CORE_SYSSTARTER_PORT}\
		-a $CORE_LOCAL/config/passwd\
		-h $CORE_PORTER_HOST\
		-e $CORE_ROOT/config/environment.dat\
		-e $CORE_LOCAL/config/environment.dat\
		$CORE_DATA/starter/starter.cfg"
	STARTER_LOG="$CORE_DATA/log/starter.log"
fi

JAVA_OPTION="$JAVA_OPTION -Djava.awt.headless=true"
BOOTSTRAP_VERSION=`cat $CORE_ROOT/version/bootstrap`
BOOTSTRAP_VERSION=`echo $BOOTSTRAP_VERSION | cut -d: -f 2`
CALL_BOOTSTRAP="java $JAVA_OPTION -jar $CORE_ROOT/java/lib/bootstrap-$BOOTSTRAP_VERSION.jar"

#
# =======================================
# Parse command line
#
fileowner=`ls -l $CORE_ROOT/bin/survive | awk '{ print $3 }'`
user=`whoami`
if [ "$fileowner" != "$user" ]; then
	echo "Please run Integrator script using <<$fileowner>> user."
	exit 1
fi

option=`tolower $1`
case "$option" in
	"" | "help" | "/?" | "-help" | "--help" | "-h" | "-?")
		display_help
		exit 0
	;;

	"start")
		if test "$2" = "-S" ; then
			SHUTDOWN_MODE=-S
		fi
		start
	;;

	"start-and-wait")
		if test "$2" = "-S" ; then
			SHUTDOWN_MODE=-S
		fi
		startForeground
	;;

	"stop")
		if test "$2" = "-t" ; then
			TIMEOUT=$3
		fi
		normalStop
	;;

	"force-stop")
		forceStop
	;;

	"restart")
		restart
	;;

	"status")
		status
	;;

	*)
		$CALL_BOOTSTRAP $*
		if test $? -ne 2 ; then
			exit $?
		fi

		display_help
		exit 1
	;;
esac

