#!/bin/sh
#################################################################
#								#
#	Copyright 2001, 2012 Fidelity Information Services, Inc	#
#								#
#	This source code contains the intellectual property	#
#	of its copyright holder(s), and is made available	#
#	under a license.  If you do not know the terms of	#
#	the license, please stop and do not read further.	#
#								#
#################################################################
arch=linux
gtm_dist=/usr/gtm
echo="/bin/echo -e"
logdir=$gtm_dist/log
multiservers=0
server_list=

export gtm_dist

while [ $# -ne 0 ]
do
        case $1 in
                -autorestart)
                        GTCM_RESTART=1
			export GTCM_RESTART
			;;
                *)
			multiservers=1
			server_list="$server_list $1" ;;
        esac
        shift
done

if [ ! -d $logdir ]
then
	$echo "GT.M/GT.CM logging directory ($logdir) does not exist, creating it..."
	mkdir $logdir
fi

if [ ! -f $gtm_dist/gtcm_server ]
then
	exit 0
fi

if [ $arch = "sun" ]; then
ps -ax | fgrep gtcm_s | fgrep -v grep > /usr/tmp/tmp$$
else
ps -ea | fgrep gtcm_s > /usr/tmp/tmp$$
fi
if [ $? -eq 0 ]
then
	$echo "The following server(s) are running:"
	cat /usr/tmp/tmp$$
fi
rm /usr/tmp/tmp$$

$echo "Do you want to start GT.CM? (Y or N)\c"

read resp
$echo
if [ "$resp" = "Y" -o "$resp" = "y" ] ; then
        if [ $gtmgbldir ] ; then
                $echo > /dev/null
        else
                $echo "The environment variable gtmgbldir is not defined. gtcm_server will"
                $echo "not run correctly without this variable. Please do the following"
                $echo "to start GT.M up correctly: "
                $echo "    1) Define gtmgbldir. (Please see the manual for instructions.)"
                $echo "    2) Rerun this script."
                exit
        fi

	if [ "$GTCM_RESTART" -eq 1 ]
	then
		$echo "Starting the GT.CM server(s) in auto-restart mode."
	else
		$echo "Starting the GT.CM server(s)."
	fi

	if [ $multiservers -eq 1 ]; then
		for i in $server_list
		do
			while read service id options
			do
				if [ "$service" != "$i" ] ; then
					continue
				fi

				echo "Starting GT.CM (${service}, ${id})..."
				if [ ! -d $logdir/$service ]
				then
					$echo "logging directory (${logdir}/${service}) does not exist, creating it..."
					mkdir $logdir/$service
				fi
			        nohup $gtm_dist/gtcm_run -service $service -id ${id} -log - $options \
					>> $logdir/${service}/session.log 2>&1 < /dev/null &

        			if [ $? != 0 ]; then
		                	$echo "The GT.CM server (${service}) failed to start."
			        fi
				sleep 1
			done < $gtm_dist/gtcm_slist
		done
	else
		while read service id options
		do
			if [ "$service" = "#" -o -z "$id" ] ; then
				continue
			fi

			echo "Starting GT.CM (${service}, ${id})..."
			if [ ! -d $logdir/$service ]
			then
				$echo "logging directory (${logdir}/${service}) does not exist, creating it..."
				mkdir $logdir/$service
			fi
		        nohup $gtm_dist/gtcm_run -service $service -id ${id} -log - $options \
				>> $logdir/${service}/session.log 2>&1 < /dev/null &

        		if [ $? != 0 ]; then
                		$echo "The GT.CM server (${service}) failed to start."
		        fi
			sleep 1
		done < $gtm_dist/gtcm_slist
	fi

else
        exit
fi

