#!/bin/sh
#added by installer
Installpath=/usr/local/ExceedConnectionServer2008

# ###############################################################
# Exceed Connection Server 2008
# Copyright (c) 1997-2008 Hummingbird Ltd. All Rights Reserved.
# ###############################################################
# ecsstop
# $Revision: 10408 $
# ###############################################################

# 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
Banner="ecsstop - Exceed Connection Server 2008" 
Usage1="Usage: ecsstop [-q]" 
Usage2="     -q - Quiet, print no messages"

ClusterManager="Exceed Connection Server"
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}

