#! /bin/ksh
# IBM_PROLOG_BEGIN_TAG 
# This is an automatically generated prolog. 
#  
#  
#  
# Licensed Materials - Property of IBM 
#  
# (C) COPYRIGHT International Business Machines Corp. 1998,2007 
# All Rights Reserved 
#  
# US Government Users Restricted Rights - Use, duplication or 
# disclosure restricted by GSA ADP Schedule Contract with IBM Corp. 
#  
# IBM_PROLOG_END_TAG 
#*===========================================================================*/
#*                                                                           */
#* Module Name:  RMstop                                                      */
#*                                                                           */
#* Description:                                                              */
#*      Frontend script for synchronously stopping any RM that is under src  */
#*      control.                                                             */
#*                                                                           */
#*  sccsid = "@(#)16   1.4   src/rsct/rm/RMstop, RMframework, rsct_relgh, relghs001a 2/11/00 17:39:15"                                    */

# Setup common variables.

DEFPATH='/usr/sbin/rsct/bin/';

# Set path to known value
PATH=/usr/sbin/rsct/bin:/usr/bin:/usr/sbin:/bin
export PATH

# Locations of commands used
SED="/bin/sed";

# Initialize values
SUBSYS=
MULTIPLE='-Q';
RESTART='-R';

# Initialize values
SUBSYS=
LOGFILE='/var/ct/RMstop.log';

# mainline code
print -n "RMstop invoked on " >> $LOGFILE;
date >> $LOGFILE;

# Parse command line
while getopts ":s:h" opt
do
	case $opt in
		s )	SUBSYS=$OPTARG; print "Subsys = $SUBSYS" >> $LOGFILE;;
		h ) print "RMstop <SRC options>" >> $LOGFILE;
				 exit 0;;
        : ) print "RMstop - Missing option for" $OPTARG >> $LOGFILE;
                 exit 1;;
		? ) print "RMstop - Stop a resource manager running under SRC control.\n\n" >> $LOGFILE;
				 exit 1;;
	esac
done

# Output warning message if extra parameters are encountered.

if [[ -n $OPTARG ]]
then
	if [[ $OPTARG -le $# ]]
	then
		print "RMstop - Extra arguments ignored.\n" >> $LOGFILE;
	fi
fi

if [[ ! -n $SUBSYS ]]
then
    print "RMstop - Subsystem name missing.\n" >> $LOGFILE;
    exit 1;
fi

# As soon as we have a way, get the Cluster ID to default the standard out/err to 
# a cluster specific directory.  

# Determine if subsystem is already defined and if it is running...

state=`LC_ALL=C /usr/bin/lssrc -s "$SUBSYS" | $SED -e"1d;s/.* \([a-zA-Z0-9]*\)$/\1/"`;
if [[ -z "$state" ]]
then
    # Subsystem is not defined so generate error and exit. 
    print "RMstop - subsystem $SUBSYS is not defined.\n" >> $LOGFILE;
    exit 0;
fi

# If subsystem is inactive, return 
if [[ $state = "inoperative" ]]
then
	exit 0;
fi

# If subsystem is active, issue a stopsrc with cancel
if [[ $state = "active" ]]
then
	stopsrc -c -s "$SUBSYS" >> $LOGFILE
fi

# Now wait for sub-system to become inoperative but wait for a max of 60 second, issuing
# a stopsrc -f after 35 seconds.
waitcount=0;
while [[ $waitcount -lt 30 ]]
do
    state=`LC_ALL=C /usr/bin/lssrc -s "$SUBSYS" | $SED -e"1d;s/.* \([a-zA-Z0-9]*\)$/\1/"`;
    if [[ -z $"state" ]] 
    then
        print "RMstop - Subsystem $SUBSYS disappeared while waiting for it to stop.\n" >> $LOGFILE;
        exit 1;
    else
	if [[ $state = inoperative ]] 
	then
	   exit 0;
        fi
    fi 

    sleep 2;

    ((waitcount = waitcount + 1));

    if [[ $waitcount -eq 16 ]] 
    then
		stopsrc -f -s "$SUBSYS"
    fi
done;
 
# If we got to here, then the subsystem exists and is not inoperative so log error
if [[ $state != inoperative ]] 
then
	print "RMstop - Could not stop subsystem $SUBSYS.\n" >> $LOGFILE;
fi

exit 0;




