#! /bin/ksh
# IBM_PROLOG_BEGIN_TAG 
# This is an automatically generated prolog. 
#  
#  
#  
# Licensed Materials - Property of IBM 
#  
# (C) COPYRIGHT International Business Machines Corp. 1998,2008 
# 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.8   src/rsct/rm/RMstop, RMframework, rsct_rfos, rfos0838a 2/18/08 17:41:46"


# 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

# ensure LC__FASTMSG is true; needed for correct output in C locale
export LC__FASTMSG=true

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

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

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

# mainline code
DATE=`date`
print "RMstop invoked on $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 [[ $OPTIND -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...

LSSRC_OUT=`LC_ALL=C /usr/bin/lssrc -s "$SUBSYS" | LC_ALL=C $SED -e"1d"`;
state=`echo $LSSRC_OUT | LC_ALL=C $SED -e"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

westop=0;
pid=`echo $LSSRC_OUT | LC_ALL=C $SED -e"s/[^0-9]*//g"`;
# If subsystem is active, issue a stopsrc with cancel
if [[ $state = "active" ]]
then
	stopsrc -c -s "$SUBSYS" >> $LOGFILE
    westop=1;
fi

# Now wait for sub-system to become inoperative but wait for a max of 30 second, reporting 
# a error.
waitcount=0;
while [[ $waitcount -lt 30 ]]
do
    NEW_LSSRC_OUT=`LC_ALL=C /usr/bin/lssrc -s "$SUBSYS" | LC_ALL=C $SED -e"1d"`;
    state=`echo $NEW_LSSRC_OUT | LC_ALL=C $SED -e"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;
    elif  [[ $state = "inoperative" ]] 
    then
       exit 0;
    else 
   	    newpid=`echo $NEW_LSSRC_OUT | LC_ALL=C $SED -e"s/[^0-9]*//g"`;
	    if [[ -z "$newpid" ]]; then
                # Subsystem stopped now.
       		exit 0;
	    elif [[ $pid -ne $newpid ]] #the old pid is gone
	    then
               # Subsystem has been stopped and restarted.
		    exit 0;
	    elif [[ $waitcount -ne 0 ]] # no report need in first iteration
        then
	        print "RMstop - Subsystem $SUBSYS is still stopping ($NEW_LSSRC_OUT) in $waitcount secs\n" >> $LOGFILE;
        fi
    fi 

    sleep 1;

    ((waitcount = waitcount + 1));

done;
 
# If we got to here, then the subsystem exists and is not inoperative so log error
if [[ $state != "inoperative" ]] 
then

    if [[ $westop -eq 1 ]]
    then
	    print "RMstop - Could not stop subsystem $SUBSYS by stopsrc.\n" >> $LOGFILE;
	    print "RMstop - Could not stop subsystem $SUBSYS by stopsrc.\n" ;
	    exit 1;
    else
	    print "RMstop - Could not stop subsystem $SUBSYS.\n" >> $LOGFILE;
	    print "RMstop - Could not stop subsystem $SUBSYS.\n"; 
	    exit 2;
    fi
fi
exit 0;




