#!/bin/sh
#added by installer
Installpath=/data/Exceed_Connection_Server_13.7

# ####################################################################
# Exceed Connection Server 13.7
# Copyright (c) 1997-2009 Open Text Corporation. All Rights Reserved.
# ####################################################################
# ecsstop
# $Revision: 14533 $
# ####################################################################

# ecsstop
# stop and then, if necessary, clean up after server
# arguments:
#	-q Quiet, ask no questions, produce no output

if [ ${OSTYPE:="UNKNOWN"} = "Linux" \
        -o $OSTYPE = "linux-gnu" \
        -o $OSTYPE = "linux" \
        ]; then
        ECHO="echo -e"
else
        ECHO="echo"
fi

#constants
CMLockfile=${Installpath}/esessionmgr.lock
SessionCleanFileRel="/bin/sys/sessclean"
SessionsDirRel="/sessions/"

#some strings
VERSION="13.7"
SRV_NAME="Exceed Connection Server"
PROD_NAME="${SRV_NAME} ${VERSION}"

Banner="ecsstop - ${PROD_NAME}" 
Usage1="Usage: ecsstop [-q]" 
Usage2="     -q - Quiet, print no messages"

ClusterManager="${SRV_NAME}"
ServerStopped=" stopped."
ServerNotRunning1=" is not running or you do not have"
ServerNotRunning2="permission to stop it."

# process command line
Quiet=0
for i
do
	case $i in
	-q) Quiet=1 ;;
	*) ${ECHO} "" ; ${ECHO} ${Banner}; ${ECHO} "" ; ${ECHO} "${Usage1}"; ${ECHO} "${Usage2}"; exit 1;;
	esac
done

if [ ${Quiet} != "1" ] ; then
	${ECHO} ""
	${ECHO} ${Banner}
	${ECHO} ""
fi

# kill the server - first send it a shutdown signal, then if it does not die, 
# terminate it
# kill the cm - first send it a shutdown signal, then if it does not die, 
# terminate it
if [ -r $CMLockfile ] ; then
	txpid="`cat $CMLockfile`"
	if  kill $txpid > /dev/null 2>&1; then
		countdown=9:8:7:6:5:4:3:2:1:0
		for i in ${countdown} ; do
			if [ -r ${CMLockfile} ] ; then sleep 2 ; fi
		done
	
	fi
	if [ -r $CMLockfile ] ; then
		rm -rf ${CMLockfile}
   		kill -9 $txpid > /dev/null 2>&1
	fi
    	if [ ${Quiet} != "1" ] ; then
        	${ECHO} "${ClusterManager}${ServerStopped}"
        	${ECHO}
    	fi

else
   	if [ ${Quiet} != "1" ] ; then
       		${ECHO} "${ClusterManager}${ServerNotRunning1}"
       		${ECHO} "${ServerNotRunning2}"
       		${ECHO}
    	fi
fi

# session dirs
SessionCmd=${Installpath}${SessionCleanFileRel}
SessionsDir=${Installpath}${SessionsDirRel}
if [ ${Quiet} = "1" ] ; then
	QuietArg="-q"
else
	QuietArg=""
fi

${SessionCmd} ${QuietArg} ${SessionsDir}

