#!/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.	#
#								#
#################################################################
gtm_dist=/usr/gtm
echo="/bin/echo -e"
arch=linux
tmpfile="/tmp/tmp$$"

if [ "`$gtm_dist/geteuid`" != "root" ] ; then
$echo "If you want to perform a rundown on anyone"
$echo "else's MUMPS process, you must run this program as root."
$echo \\n
fi

$echo "Signalling all MUMPS processes to proceed with rundown."
$echo \\n

ps -ef | grep mumps | grep -v grep | awk '{ print "kill -15", $2, " ; sleep 1" }' | sh > /dev/null 2>&1

$echo "Waiting for MUMPS processes to rundown."
$echo \\n

sleep 10

$echo "Resignalling processes."
$echo \\n

ps -ef | grep mumps | grep -v grep | awk '{ print "kill -15", $2, " ; sleep 1" }' | sh > /dev/null 2>&1

ps -ef | fgrep mumps | fgrep -v grep > $tmpfile
if [ $? -eq 0 ]
then
	$echo "The following mumps process(es) are still running:"
	cat $tmpfile
fi
rm -f $tmpfile

if [ "`$gtm_dist/geteuid`" != "root" ] ; then
	$echo "You are not root. Unable to terminate some GT.M processes"
else
	$echo "Do you want to stop GT.M server(s) if present? (y or n)\c"
	read resp
	if [ "$resp" = "Y" -o "$resp" = "y" ] ; then
		$echo \\n

		if [ -f $gtm_dist/gtcm_server ]; then
			$echo "Stopping GT.CM server(s) ...."
			ps -ef | grep gtcm_run | grep -v grep | awk '{ print "kill -15", $2 }' | sh > /dev/null 2>&1
			ps -ef | grep gtcm_s | grep -v grep |  awk '{ print "kill -15", $2, " ; sleep 1" }' | sh > /dev/null 2>&1
			sleep 10
			ps -ef | grep gtcm_s | grep -v grep > $tmpfile

			if [ $? -eq 0 ]
			then
			    $echo "server(s) did not respond to kill -15."
			    $echo "sending TRAP signal to the gtcm_server(s)."
			    ps -ef | grep gtcm_s | grep -v grep | awk '{ print "kill -5", $2, " ; sleep 2" }' | sh > /dev/null 2>&1
			    sleep 4
			fi
		fi

		if [ -f $gtm_dist/shmclean ]; then
			$echo "Cleaning up shared memory segments and semaphores..."
#
# Greystone suggests adding mupip rundown -region <name> for each region
# in your active databases.  This ensures flushing and deletion
# of the appropriate shared memory segments and semaphores.
#
# example:	        $gtm_dist/mupip rundown -region DEFAULT
#
			$gtm_dist/shmclean -q -d
			$gtm_dist/shmclean -q -s
		fi
		$echo "Shutdown complete."
	fi
fi
