#! /bin/sh
#
# $Id: //NW/dev/nw_8_1_1/nsr/scripts/nsrps_shutdown.sh#1 $ Copyright (c) 2003-2008, EMC Corporation.
#
# All rights reserved.
#

# This script is an adapted version of nsr_shutdown for PBAPI agent processes 
# Kill off all the processes associated with PBAPI agent (nsrpsd)
#

RELEASE=8.1.1.8.Build.349


# see nsr_shutdown script for details on PATH setting below 

PATH=/usr/ucb:/usr/bsd:/bin:/usr/bin:/etc:/usr/etc:/usr/sbin:$PATH
mypath="`expr X\"${0}\" : X'\(.*\)/.*' \| X\"${0}\" : X'\(/\)[^/]*$' \| '.'`"
PATH=$mypath:$PATH
export PATH
myname=`basename $0`

# default values
productname="EMC Snapshot Management service"
nsrpb_daemon="nsrpsd"

if [ -f /bin/hostname ]; then
	host=`/bin/hostname`
elif [ -f /usr/ucb/hostname ]; then
	host=`/usr/ucb/hostname`
elif [ -f /usr/bsd/hostname ]; then
	host=`/usr/bsd/hostname`
elif [ -f /usr/local/bin/hostname ]; then
	host=`/usr/local/bin/hostname`
else
	host=
fi
if [ X${host} = X ]; then
	host=`uname -n`
fi

nflag=n
qflag=n
debug=n

bsd_kill=kill
bsd_echo=true
bsd_ps=true
id=id


killnsrpb=y
kill_sig=

# save the args
ARGS="$*"

# find the binary type of /bin/sh
set X `file /bin/sh`
shift
# for some shells, the above 'set' results in 'X' being $0 and '/bin/sh:'
# in $1 so check for that and shift if it is true
if [ X"$1" = X/bin/sh: ]; then
	shift
fi
if [ X"$1" = Xsymbolic ]; then
	set X `file -L /bin/sh`
	shift
fi
bin_type="$1"
set X $ARGS
shift

#
# OS specific information gathering
# see nsr_shutdown for comments  
#
uname=`uname`
if [ -r /usr/bin/arch ]; then
	sun=y
	id=whoami
elif [ "X${uname}" = "XAIX" ]; then
	bsd_echo=false
	bsd_ps=false
elif [ "X${uname}" = "XDYNIX/ptx" ]; then
	bsd_echo=false
	bsd_ps=false
elif [ "X${uname}" = "XOSF1" ]; then
	bsd_echo=false
	bsd_ps=false
elif [ -r /usr/bin/sh5 -o X"${bin_type}" = "Xmipsel" ]; then
	if [ "X${SHUTDOWN_SHELL}" != "X/usr/bin/sh5" ]; then
		SHUTDOWN_SHELL=/usr/bin/sh5
		export SHUTDOWN_SHELL
		exec ${SHUTDOWN_SHELL} $0 $*
	fi
	bsd_echo=false
elif [ "X${uname}" = "XNetBSD" ]; then
	bsd_ps=true
	bsd_echo=true
elif [ "X${uname}" = "XFreeBSD" ]; then
	bsd_ps=true
	bsd_echo=true
elif [ "X${uname}" = "XBSD/OS" ]; then
	bsd_ps=true
	bsd_echo=true
elif [ "X${uname}" = "XLinux" ]; then
	bsd_ps=true
	bsd_echo=true
elif [ "X${uname}" = "XHP-UX" ]; then
	bsd_echo=false
	bsd_ps=false
elif [ "X${uname}" = "XIRIX" ]; then
        bsd_ps=false
else
	# Generic System V
	if [ -f /usr/ucb/echo ]; then
		bsd_echo=true
	else
		bsd_echo=false
	fi
	if [ -f /usr/ucb/ps ]; then
		bsd_ps=true
	else
		bsd_ps=false
	fi

fi

umask 22


#
# qecho
#	quite echo:  if the qflag is no, then echo all arguments,
#	otherwise don't.
#
qecho()
{
	if [ ${qflag} = n ]; then
		echo "$*"
	fi
}

if [ $bsd_echo = true ]; then
	echo_n()
	{
		echo -n "$*"
	}
else
	echo_n()
	{
		echo "$*\c"
	}
fi

ps_all()
{
	if [ $bsd_ps = true ]; then
		ps_output="`ps ax | egrep 'nsrps'`"
	else
		ps_output="`ps -ef | egrep 'nsrps'`"
	fi

	# determine relative positions of needed fields which would be
	# derived from either bsd or non-bsd style ps ( ps -ax or ps -ef )
	# nothing else

	if [ $bsd_ps = true ]; then
	        #
	        # bsd style output is the easiest
	        #
	        ps_name_nsrpb='$5'
	        ps_pid='$1'
		ps_opt='$6'
	else
	        ps_pid='$2'

	        # stime can be hh:mm:ss or mm dd - either one field or two 
		# The need for the following is not clear ???
	        ps_stime_nsrpb="`ps -ef  | egrep 'nsrps' | awk '{ print $8 }' | egrep 'nsrps'`"
		
		    if [ ! -z "$ps_stime_nsrpb" ]; then
	                # stime for nsrd is less than 24 hours
	                ps_name_nsrpb='$8'
			ps_opt='$9'
	        else
	                ps_name_nsrpb='$9'
			ps_opt='$10'
	        fi
	fi
}

#
# See if nsrpbcsd master or agents are currently running
#
# INPUTS:
#	killnsrpb=	y if daemons should be killed
#
# Outputs:
#	The nsrpbcsd daemons are in ${nsrpbdaemons}
#
find_nsrpbdaemons()
{
	# build up an egrep style pattern we can feed to awk which will match
	# all the various daemons.
	nsr_pat='[0-9] [-a-zA-Z\+\/._0-9]*('"${nsrpb_daemon}"')'
	nsrpbdaemons="`echo "${ps_output}" | awk '/awk/	{ next }
		$0 ~ /'"$nsr_pat"'/ { print $0 }'`"
}


#
# Kill nsrpbcsd master and agents 
#
# INPUTS:
#	killnsrpb=	y if daemons should be killed
#	productname=	e.g. NetWorker
#	kill_sig=	signal to use (blank for default)
#
killnsrpbcsd()
{
	if [ ${killnsrpb} = y -a ! -z "${nsrpbdaemons}" ]; then
		qecho "	* * * Killing ${productname} daemons"
		if [ "X${kill_sig}" = "X-9" ]; then
			# Kill master and agents
			pid=`echo "${nsrpbdaemons}" | awk '{ print '"$ps_pid"' }'`
		else
			pid=`echo "${nsrpbdaemons}" |
				awk "$ps_opt"' !~ /-[aA]/ { print '"$ps_pid"' }'`
		fi

		if [ ! -z "${pid}" ]; then
			${bsd_kill} ${kill_sig} ${pid}
		fi
	fi
}


#
# yesno function
#
# Generic function to get yes/no answer
#
# inputs:
#	prompt=		string to prompt with (excluding default value)
#	default=	name of default value if no response
#
# outputs:
#	result=		'y' or 'n'
#
yesno()
{
	if [ $debug = y ]; then
		notreally="(not really) "
	else
		notreally=
	fi
	while true
	do
		if [ -z "${default}" ]; then
			echo_n "${prompt}${notreally}? "
			read ans
		elif [ ${qflag} = n ]; then
			echo_n "${prompt} ${notreally}[${default}]? "
			read ans
		else
			ans=
		fi
		if [ "X${ans}" = X ]; then
			result="${default}"
		else
			result="${ans}"
		fi

		if [ `expr "X${result}" : 'X[yY]'` -ne 0 ]; then
			result=y
			break
		elif [ `expr "X${result}" : 'X[nN]'` -ne 0 ]; then
			result=n
			break
		else
			echo "Please respond \`yes' or \`no'"
		fi
	done
}

#
# usage function - prints out a usage message and exits
#
usage()
{
	echo ""
	echo "usage: ${myname} [-d] [-n] [-q] [-v]"
	exit 1
}

#
# Process arguments.  Set tmp args here so we know if
# we have to reset defaults.
#
argnsrpb=n

while [ $# -gt 0 ]; do
	case "$1" in
	-q)
		qflag=y
		shift
		;;
	-a)
                shift
                ;;
        -A)
                shift
                ;;
	-d)	# default option
		argnsrpb=y
		shift
		;;
	-n)
		nflag=y
		shift
		;;
	-D)
		bsd_kill="echo kill"
		debug=y
		shift
		;;
        -c)
                shift
                ;;
        -s)
                shift
                ;;
	-v)
		set -x
		shift
		;;
	-??*)
		# rip apart the concatenated argument, reset 'em, and reloop.
		first_opt="`expr substr "$1" 2 1`"
		rest_opts="`expr substr "$1" 3 255`"
		shift
		set -- "-$first_opt" "-$rest_opts" $*
		;;
	*)
		echo $1
		usage
		;;
	esac
done

#
# If any kill args were set use the arg values
#
if [ $argnsrpb = y ]; then
	killnsrpb=$argnsrpb
fi

#
# If not running in "do nothing mode" (or not debugging),
# insist on being run by the Super user.
#
# This method has a hole.  If user or group name has substring of
# "root", they get access!
#
whoiam=`${id} | grep root`
if [ "$debug" = "n" -a -z "$whoiam" ]; then
	echo ""
	echo "${myname} must be run by the Super user!"
	exit 1
fi



ps_all
find_nsrpbdaemons

#
# If -n, just print daemons
#
if [ $nflag = y ]; then
	echo "${nsrpbdaemons}"
	exit 0
fi

if [ -z "${nsrpbdaemons}" ]; then
	qecho "${myname}: EMC Snapshot Management service is already down"
	exit 0
fi

qecho "${myname} will kill the following processes"
if [ ! -z "${nsrpbdaemons}" ]; then
	qecho "${nsrpbdaemons}"
fi


prompt="Do you want to continue?"
default="Yes"
yesno

if [ ${result} = n ]; then
	qecho "${myname} exiting"
	exit 1
fi

kill_sig=
killnsrpbcsd

sleep 5
ps_all
find_nsrpbdaemons

ntries=10
while [ ! -z "${nsrpbdaemons}" ];
do
	ntries=`expr ${ntries} - 1`
	qecho \
"	* * * daemons not dead yet (trying ${ntries} more times)."
	qecho "${nsrpbdaemons}"
	if [ $ntries -eq 0 ]; then
		# use a bigger stick
		kill_sig="-9"
	fi
	if [ $ntries -le 6 ]; then
		killnsrpbcsd
	fi
	if [ $ntries -eq 0 ]; then
		break
	fi

	sleep 10 
	ps_all
	find_nsrpbdaemons
done

if [ "X${uname}" = "XOSF1" -a "${worklist}" = "yes" ]; then
        exit 0
fi

exit 0
