#!/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:  RMstart                                                     */
#*                                                                           */
#* Description:                                                              */
#*      Frontend script for start src to define subsystem if it is not       */
#*      already defined.                                                     */
#*                                                                           */
#*      This script can be used for all SRC controlled resource managers so  */
#*      that each one does not have to write one of these scripts.  The      */
#*      SRC parameters necessary to define the subsystem will be passed as   */
#*      parameters to this shell script.   There will be some defaults       */
#*      provided that are different that the SRC defaults.  For example,     */
#*      standard error and out will be directed to files in the log          */
#*      directory for the resource manager.                                  */
#*                                                                           */
#*      The subsystem name passed to this script should be the same as the   */
#*      resource manager name (e.g. IBM.LANRM).                              */
#*                                                                           */
#*      Errors detected by this script will be written to /var/ct/RMstart.log*/
#*                                                                           */
#*      Defaults                                                             */
#*      --------                                                             */
#*      a) If the path specified by the -p option does not start with '/'    */
#*         then the path will be expanded to be /usr/sbin/rsct/bin/<path>.   */
#*      b) User id if not specified defaults to 0 (root).                    */
#*      c) stderr will be put in                                             */
#*          /var/ct/<RM name>.stderr                                         */
#*      d) Restart on errors (-R)                                            */
#*      e) Run one instance (-Q) Can't be overridden (-q option is ignored.) */
#*                                                                           */
#*                                                                           */
#*                                                                           */
#*      Note: c) is temporary until we can get cluster id from a script.     */
#*                                                                           */
#*  Todo                                                                     */
#*  ----                                                                     */
#*  1. Add a message catalog for message text                                */
#*  2. Use cluster id to determine where to write log messages.              */
#*                                                                           */
#*===========================================================================*/

# sccsid = "@(#)24   1.16   src/rsct/rm/RMstart, RMframework, rsct_relgh, relghs001a 10/20/06 10:36:32" */

# Setup common variables.

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

REDHAT_RELFILE="/etc/redhat-release"
REDHAT_VERSION="^Red Hat Enterprise Linux .* release 3 (.*)$"
REDHAT_VERSION_4="^Red Hat Enterprise Linux .* release 4 (.*)$"
SUSE_RELFILE="/etc/SuSE-release"
SUSE_VERSION="^SUSE LINUX Enterprise Server 9 (.*)$"

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

# Location of tools used
SED="/bin/sed";

# Initialize values
SUBSYS=
MULTIPLE='-Q';
RESTART='-R';
CMDOPTS=
LOGFILE='/var/ct/RMstart.log';
CMDPATH=
STDERR=
USER='-u 0';
STDOUT=
STDIN=

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

# Parse command line
while getopts ":s:p:u:a:e:E:f:G:i:I:Km:n:o:ORdDqQSt:w:h" opt
do
	case $opt in
		s )	SUBSYS=$OPTARG;;
        p ) CMDPATH=$OPTARG;;
        u ) CMDOPTS="$CMDOPTS -u$OPTARG";;
        a ) CMDOPTS="$CMDOPTS -a$OPTARG";;
        e ) STDERR=" -e $OPTARG";;
        E ) CMDOPTS="$CMDOPTS -E$OPTARG";;
        f ) CMDOPTS="$CMDOPTS -f$OPTARG";;
        G ) CMDOPTS="$CMDOPTS -G$OPTARG";;
        i ) STDIN=" -i $OPTARG";;
        I ) CMDOPTS="$CMDOPTS -I$OPTARG";;
        K ) CMDOPTS="$CMDOPTS -K";;
        m ) CMDOPTS="$CMDOPTS -m$OPTARG";;
        n ) CMDOPTS="$CMDOPTS -n$OPTARG";;
        o ) STDOUT=" -o $OPTARG";;
        O ) RESTART="-O";;
        R ) RESTART="-R";;
        d ) CMDOPTS="$CMDOPTS -d";;
        D ) CMDOPTS="$CMDOPTS -D";;
        q ) print "Can't specify multiple copies... -q option";;
        Q ) ;;
        S ) CMDOPTS="$CMDOPTS -S";;
        t ) CMDOPTS="$CMDOPTS -t$OPTARG";;
        w ) CMDOPTS="$CMDOPTS -w$OPTARG";;
		h ) print "RMstart <SRC options>" >> $LOGFILE;
				 exit 0;;
        : ) print "RMstart - Missing option for" $OPTARG >> $LOGFILE;
                 exit 1;;
		? ) print "RMstart - Make and start a subsystem\n\n   RMstart <SRC options>" >> $LOGFILE;
				 exit 1;;
	esac
done

# Output warning message if extra parameters are encountered.

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

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

if [[ ! -n $CMDPATH ]]
then
    print "RMstart - Executable path missing for $SUBSYS definition.\n" >> $LOGFILE;
    exit 1;
fi

# Attempt to expand path if not absolute

if [[ ! ( $CMDPATH = ?('/*') ) ]]
then
    CMDPATH="$DEFPATH$CMDPATH";
fi

# Default stderr if not specified.
if [[ ! -n $STDERR ]]
then
    STDERR="-e /var/ct/${SUBSYS}.stderr";
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 [[ -n "$state"  ]] 
then
         
    # Subsystem is already defined.    
    
    # We may want to do a lssrc -s <subsys> -S to get the current SRC definition and update it
    # if it has changed.

    # For now, just start it if it is inoperative.
    if [[ $state = "active" ]] 
    then
        print "RMstart - Subsystem $SUBSYS is already active.\n" >> $LOGFILE;
        exit 0;
    else
        # if is is not active nor inoperative, it must be shutting down so wait a while.
	waitcount=0;
	while [[ ($waitcount -lt 12) && ($state != inoperative) ]]
        do
            sleep 5;
            state=`LC_ALL=C /usr/bin/lssrc -s "$SUBSYS" | $SED -e"1d;s/.* \([a-zA-Z0-9]*\)$/\1/"`;
            if [[ -n "$state" ]] 
            then
                print "RMstart - Subsystem $SUBSYS disappeared while waiting for it to stop.\n" >> $LOGFILE;
                state="inoperative";
            fi
            ((waitcount = waitcount + 1));
        done;
 
        #  If the above loop exited because it timed out, generate an error and exit. 
        if [[ $state != inoperative ]]
        then
            print "RMstart - Subsystem $SUBSYS never stopped so it could be restarted.\n" >> $LOGFILE;
            exit 1;
        fi
    fi

    # If we got to there then the sub-system is inoperative so we can undefine it
    print "RMstart - removing subsystem $SUBSYS\n" >> $LOGFILE
    CMDLINE="-s $SUBSYS"
    rmssys $CMDLINE >> $LOGFILE
fi


# Subsystem is not defined so we will now define it.
CMDLINE="-s $SUBSYS -p $CMDPATH -G rsct_rm $USER $RESTART $MULTIPLE $STDERR $STDOUT $STDIN $CMDOPTS"
print "RMstart - creating subsystem with the following parameters:\n" >> $LOGFILE;
print "    $CMDLINE\n" >> $LOGFILE;

# Create the ODM definitions.
mkssys $CMDLINE >> $LOGFILE;
if [[ $? -ne 0 ]]
then
	print "RMstart - Could not define subsystem for $SUBSYS.\n" >> $LOGFILE;
	exit 1
else
    print "RMstart - Subsystem $SUBSYS has been defined.\n" >> $LOGFILE;
fi

# If we got to here, then the subsystem exists and is inoperative so attempt to start it.

# special processing for RH EL release 3

EFLAG=
if [[ -f $REDHAT_RELFILE ]]
then
	grep "$REDHAT_VERSION" $REDHAT_RELFILE > /dev/null 2>&1
	if (($? == 0))
	then
		EFLAG="-e LD_ASSUME_KERNEL=2.4.19"
	fi
elif [[ -f $SUSE_RELFILE ]]
then
    grep "$SUSE_VERSION" $SUSE_RELFILE > /dev/null 2>&1
    if (($? == 0))
    then
		EFLAG="-e LD_ASSUME_KERNEL=2.4.19"
    fi
fi

if [[ $(uname -s) = "AIX" ]]
then
	EFLAG="-e AIXTHREAD_SCOPE=S"
fi

startsrc -s $SUBSYS $EFLAG -I >> $LOGFILE;
if [[ $? -ne 0 ]]
then
    print "RMstart - Error starting subsystem $SUBSYS, code = $?.\n" >> $LOGFILE;
	exit $?
fi

exit 0;




