#!/bin/sh
# Oracle Cluster Ready Services 10.2 Patching script
#
# Requirements
# - Must be invoked as root
# - Should be invoked once per node
# - Should not be invoked while any CRS daemons in the home are up
#
# Shared CRS Homes
# - Shut down all CRS daemons using the CRS home before running
#   the script on any node. Otherwise active binaries may get overwritten,
#   and daemons could unexpectedly crash.
#
# If the script fails, it should be rerun until it succeeds.
#
# Overall Patch flow information:
# - Invoke install action, which will copy all patched files
#   into $ORA_CRS_HOME/install/patch%CRS_VERSION/
# - On each node, stop the CRS daemons, and then run this script.
#   It is acceptable to do each node in series to achieve a rolling 
#   upgrade, but it is not recommended to patch multiple homes 
#   simultaneously.

usage()
{
   printf "Usage:"
   printf "$0 [-paramfile <input file for CRS parameter values>]"
}

ORA_CRS_HOME=%s_OracleHome%
CRS_ORACLE_OWNER=%OwnerId%
CRS_DBA_GROUP=%s_usergroup%
PATCH_VERSION=%s_version%
if [ -z "$MAKE" ]; then MAKE="%s_OSDunixMakePath%"; fi

if [ -z "$CP" ]; then CP=/bin/cp; fi
if [ -z "$CPP" ]; then CPP="/bin/cp -p"; fi
if [ -z "$CPRF" ]; then CPRF="/bin/cp -r -f"; fi
if [ -z "$CPP" ]; then CPP="/bin/cp -p"; fi
if [ -z "$LS" ]; then LS=/bin/ls; fi
if [ -z "$LN" ]; then LN=/bin/ln; fi # Perform a hard link if OS can do it
if [ -z "$LNS" ]; then LNS="$LN -s"; fi # Perform soft link
if [ -z "$MV" ]; then MV=/bin/mv; fi
if [ -z "$MVF" ]; then MVF="$MV -f"; fi
if [ -z "$CAT" ]; then CAT=/bin/cat; fi
if [ -z "$CUT" ]; then CUT=/usr/bin/cut; fi
if [ -z "$RMF" ]; then RMF="/bin/rm -f"; fi
if [ -z "$ECHO" ]; then ECHO=/bin/echo; fi
if [ -z "$FIND" ]; then FIND=/usr/bin/find; fi
if [ -z "$CHOWN" ]; then CHOWN=/bin/chown; fi
if [ -z "$CHGRP" ]; then CHGRP=/bin/chgrp; fi
if [ -z "$CHOWNR" ]; then CHOWNR="/bin/chown -R"; fi
if [ -z "$CHGRPR" ]; then CHGRPR="/bin/chgrp -R"; fi
if [ -z "$CHMOD" ]; then CHMOD=/bin/chmod; fi
if [ -z "$MKDIR" ]; then MKDIR=/bin/mkdir; fi
if [ -z "$MKDIRP" ]; then MKDIRP="/bin/mkdir -p"; fi
if [ -z "$RMDIR" ]; then RMDIR=/bin/rmdir; fi
if [ -z "$SYNC" ]; then SYNC=/bin/sync; fi
if [ ! -f "$SYNC" ]; then SYNC=/usr/bin/sync; fi
if [ -z "$SLEEP" ]; then SLEEP=/bin/sleep; fi
if [ -z "$GREP" ]; then GREP=/usr/bin/grep; fi
if [ -z "$DIFF" ]; then DIFF=/bin/diff; fi
if [ -z "$AWK" ]; then AWK=/bin/awk; fi
if [ -z "$IDCMD" ]; then IDCMD=/usr/bin/id; fi

# Needs to print out (at least) the basename of all processes on the box
if [ -z "$PSE" ]; then PSE="/bin/ps -e"; fi

SYNCALL="$SYNC; $SYNC; $SYNC"

RUID=`$IDCMD|$AWK -F\( '{print $2}'|$AWK -F\) '{print $1}'`

if [ ${RUID} != "root" ];then
  $ECHO "You must be logged in as root to run $0."
  $ECHO "Log in as root and restart $0 execution."
  exit 1
fi


patchFiles()
{
  ##
  ## Apply all of the patched files to the CRS Home.
  ##
  ## The patched files only need to be dealt with on the first node
  ## if the CRS home is shared. Otherwise they are done on all node.
  ## A small number of patched files will be generated via make
  ## on the customer site.
   
  if [ -d $PATCHDIR ]; then

     # Converts the patched libskgxn2.so to a form appropriate to
     # the platform. This is expressly intended for all platforms.
     if [ -f $PATCHDIR/lib/$LIBSKGXN ]; then
       if [ -f $CH/lib/$LIBSKGXNS ]; then
         $MVF $PATCHDIR/lib/$LIBSKGXN $PATCHDIR/lib/$LIBSKGXNS
       elif [ ! -f $CH/lib/$LIBSKGXN ]; then
         $MVF $PATCHDIR/lib/$LIBSKGXN $PATCHDIR/lib/$LIBSKGXNS
       fi
     fi
     if [ -f $PATCHDIR/lib32/$LIBSKGXN ]; then
       if [ -f $CH/lib32/$LIBSKGXNS ]; then
         $MVF $PATCHDIR/lib32/$LIBSKGXN $PATCHDIR/lib32/$LIBSKGXNS
       elif [ ! -f $CH/lib32/$LIBSKGXN ]; then
         $MVF $PATCHDIR/lib32/$LIBSKGXN $PATCHDIR/lib32/$LIBSKGXNS
       fi
     fi
 
    if [ ! -d $PRE_PATCHDIR ]; then
       $ECHO "Creating pre-patch directory for saving pre-patch clusterware files"
       $MKDIRP $PRE_PATCHDIR
       $CHMOD 755 $PRE_PATCHDIR
    fi

    # Remove the skgxp files (fix for bug#4327694)
    $RMF $CH/lib/libskgxp*
     
    $RMF $CH/install/files$PATCH_VERSION.log
    cd $PATCHDIR

    for f in `$FINDPATCHFILES`
    do
      # There is a possibility that if a parent directory is created via a
      # previous iteration through this loop, that some of the subsequent
      # patched files will not be in the patchdir anymore. Skip them.
      if [ -r "$PATCHDIR/$f" ]; then

        # Check to see whether any of the parent directories of the
        # patched file need to be created. If so, then create it based
        # on the permissions in the patch directory.
        PARENTDIR=`$DIRNAME $f`
        if [ ! -d "$CH/$PARENTDIR" ]; then

          # Find the highest directory not exisitng in the CRS Home, and
          # move the copy in the patchdir into the CRS home. This will
          # move a number of files and directories wholesale.
          while [ ! -d "$CH/$PARENTDIR" ];
          do
            CHILDDIR=$PARENTDIR
            PARENTDIR=`$DIRNAME $PARENTDIR`
          done
          $MVF $PATCHDIR/$CHILDDIR $CH/$CHILDDIR
        else

          # Otherwise, the parent directory exists, and we should just
          # insert the new file.
          PARENTDIR=`$DIRNAME $f`
          if [ ! -d $PRE_PATCHDIR/$PARENTDIR ]; then
	    $MKDIRP $PRE_PATCHDIR/$PARENTDIR
	    $CHMOD 755 $PRE_PATCHDIR/$PARENTDIR
          fi
          if [ -f $CH/$f ]; then
            $CPP $CH/$f $PRE_PATCHDIR/$f
          fi
          if [ ! $? ]
          then
            $ECHO Unable to copy file $CH/$f to $PRE_PATCHDIR/$f.
            exit 1;
          fi

          $MVF $f $CH/$f
          if [ ! $? ]
          then
            $ECHO Unable to move file $f to $CH/$f.
            exit 1;
          fi
        fi
      fi
      $ECHO $f >> $CH/install/files$PATCH_VERSION.log
    done

  # In some configurations, we will have just an skgxn2 library. This
  # change converts them to have symlinks from skgxn2 to skgxns to simplify
  # later patch updates. (makes them consistent with other platforms)
  if [ ! -f $CH/lib/$LIBSKGXNS ]; then
    if [ -f $CH/lib/$LIBSKGXN ]; then
      $MVF $CH/lib/$LIBSKGXN $CH/lib/$LIBSKGXNS
      $LNS $CH/lib/$LIBSKGXNS $CH/lib/$LIBSKGXN
    fi
  fi
  if [ -d $CH/lib32 ]; then
    if [ ! -f $CH/lib32/$LIBSKGXNS ]; then
      if [ -f $CH/lib32/$LIBSKGXN ]; then
        $MVF $CH/lib32/$LIBSKGXN $CH/lib32/$LIBSKGXNS
        $LNS $CH/lib32/$LIBSKGXNS $CH/lib32/$LIBSKGXN
      fi
    fi
  fi

    $ECHO "Completed patching clusterware files to $CH"

    cd $CH

    # Now clear out all of the empty directories in $PATCHDIR.
    # (This could be done with an rm -rf, but the following is safer.)
    for f in `$FINDPATCHDIRS`
    do
      # Try to gracefully remove the directory. 
      $RMDIR $f
      if [ ! $? ]
      then
        $ECHO Unable to remove directory $f.
        exit 1;
      fi
    done

    # We need to do a small amount of relinking on customer site. Only
    # perform this on the first node, if the CRS home is shared.
    $ECHO Relinking some shared libraries.

    $MAKE -f $CH/network/lib/ins_net_client.mk ntcontab.o \
      ORACLE_HOME=$CH  >> $CH/install/make.log
    if [ ! $? ]; then
      $ECHO Relinking failed at ntcontab.o. See $CH/install/make.log.
      exit 1
    fi

    $MAKE -f $CH/network/lib/ins_net_client.mk nnfgt.o \
      ORACLE_HOME=$CH  >> $CH/install/make.log
    if [ ! $? ]; then
      $ECHO Relinking failed at nnfgt.o. See $CH/install/make.log.
      exit 1
    fi

    $MAKE -f $CH/network/lib/ins_net_client.mk client_sharedlib \
      ORACLE_HOME=$CH >> $CH/install/make.log
    if [ ! $? ]; then
      $ECHO Relinking failed at client_sharedlib. See $CH/install/make.log.
      exit 1
    fi

    $ECHO Relinking of patched files is complete.
  fi

  $CHOWNR $CRS_ORACLE_OWNER $PRE_PATCHDIR
  $CHGRPR $CRS_DBA_GROUP $PRE_PATCHDIR

  # fix for bug# 4736024 - fix templates in inventory
  $CPRF $CH/inventory/Templates/install/patch102/* $CH/inventory/Templates
}

restoreFiles()
{
  ##
  ## Apply all of the patched files to the CRS Home.
  ##
  ## The patched files only need to be dealt with on the first node
  ## if the CRS home is shared. Otherwise they are done on all node.
  ## A small number of patched files will be generated via make
  ## on the customer site.
   
  PATCHDIR=$PRE_PATCHDIR
  FINDPATCHDIRS="$FIND $PATCHDIR -type d -depth"
  FINDPATCHFILES="$FIND . -type f"

  if [ -d $PATCHDIR ]; then
     
    $RMF $CH/install/restorefiles$PATCH_VERSION.log
    cd $PATCHDIR

    for f in `$FINDPATCHFILES`
    do
      # There is a possibility that if a parent directory is created via a
      # previous iteration through this loop, that some of the subsequent
      # patched files will not be in the patchdir anymore. Skip them.
      if [ -r "$PATCHDIR/$f" ]; then

        # Check to see whether any of the parent directories of the
        # patched file need to be created. If so, then create it based
        # on the permissions in the patch directory.
        PARENTDIR=`$DIRNAME $f`
        if [ ! -d "$CH/$PARENTDIR" ]; then

          # Find the highest directory not exisitng in the CRS Home, and
          # move the copy in the patchdir into the CRS home. This will
          # move a number of files and directories wholesale.
          while [ ! -d "$CH/$PARENTDIR" ];
          do
            CHILDDIR=$PARENTDIR
            PARENTDIR=`$DIRNAME $PARENTDIR`
          done
          $MVF $PATCHDIR/$CHILDDIR $CH/$CHILDDIR
        else

          # Otherwise, the parent directory exists, and we should just
          # insert the new file.
          $MVF $f $CH/$f
          if [ ! $? ]
          then
            $ECHO Unable to move file $f to $CH/$f.
            exit 1;
          fi
        fi
      fi
      $ECHO $f >> $CH/install/rstorefiles$PATCH_VERSION.log
    done

    $ECHO "Completed restoring clusterware files to $CH"

    cd $CH

    # Now clear out all of the empty directories in $PATCHDIR.
    # (This could be done with an rm -rf, but the following is safer.)
    for f in `$FINDPATCHDIRS`
    do
      # Try to gracefully remove the directory. 
      $RMDIR $f
      if [ ! $? ]
      then
        $ECHO Unable to remove directory $f.
        exit 1;
      fi
    done

    # We need to do a small amount of relinking on customer site. Only
    # perform this on the first node, if the CRS home is shared.
    $ECHO Relinking some shared libraries.

    $MAKE -f $CH/network/lib/ins_net_client.mk ntcontab.o \
      ORACLE_HOME=$CH  >> $CH/install/make.log
    if [ ! $? ]; then
      $ECHO Relinking failed at ntcontab.o. See $CH/install/make.log.
      exit 1
    fi

    $MAKE -f $CH/network/lib/ins_net_client.mk nnfgt.o \
      ORACLE_HOME=$CH  >> $CH/install/make.log
    if [ ! $? ]; then
      $ECHO Relinking failed at nnfgt.o. See $CH/install/make.log.
      exit 1
    fi

    $MAKE -f $CH/network/lib/ins_net_client.mk client_sharedlib \
      ORACLE_HOME=$CH >> $CH/install/make.log
    if [ ! $? ]; then
      $ECHO Relinking failed at client_sharedlib. See $CH/install/make.log.
      exit 1
    fi

    $ECHO Relinking of patched files is complete.
  fi
}

##
## Step 1: Read in a config file if requested.
##
if [ $# -gt 1 ]; then
   usage;
   exit 2
fi

USE_FILE=false
RESTORE_PATCH=false
while [ $# -gt 0 ];
do
  if [ $1 = "-paramfile" ]; then
    USE_FILE=true
    if [ $# -ge 2 ]; then
      CRS_ENV_FILE=$2
      shift
    fi
#  elif [ $1 = "-restore" ]; then
#    RESTORE_PATCH=true
#     $ECHO
  else
    $ECHO "Invalid argument passed"
    usage;
    exit 1
  fi
  shift;
done

if $USE_FILE;
then
  if [ ! -f $CRS_ENV_FILE ]; then
     $ECHO "The input file($CRS_ENV_FILE) specifying the values for CRS variable is not found"
     exit 1
  fi

  ## Make sure that this input file has only "LHS=RHS" and empty lines
  ## The file should have only LHS=RHS and empty lines.
  ## Any other lines will be considered improper input for root script
  awkOutput=`$AWK 'BEGIN { Ni=0 } \
                     /^#/   { next } \
                     /[A-Z]\=\`*\`/ { printf("\"%s\"\n", $0); next } \
                     /[A-Z_]\=/ { next } \
                     /^$/   { next } \
                            { printf("\"%s\"\n", $0); next }' $CRS_ENV_FILE`

  if [ "x$awkOutput" != "x" ]; then
     $ECHO "The following unexpected CRS parameters found in input: $awkOutput"
     exit 1
  fi

  . $CRS_ENV_FILE

  $ECHO "File ($CRS_ENV_FILE) is used for setting CRS variables."
  $ECHO "CRS_ORACLE_OWNER=$CRS_ORACLE_OWNER"
  $ECHO "ORA_CRS_HOME=$ORA_CRS_HOME"
  $ECHO "MAKE=$MAKE"
fi

export ORA_CRS_HOME
ORACLE_OWNER=$CRS_ORACLE_OWNER
CH=$ORA_CRS_HOME

# PATCHDIR, and commands for finding things within it
PATCHDIR=$CH/install/patch102/
PRE_PATCHDIR=$CH/install/prepatch$PATCH_VERSION/
FINDPATCHDIRS="$FIND $PATCHDIR -type d -depth"
FINDPATCHFILES="$FIND . -type f"

# Some of the copies below could go horribly wrong if CH
# is not set, or is set to something nonexistant.
if [ ! -d "$CH" ]; then
  $ECHO Unable to find ORA_CRS_HOME=$CH.
  exit 1;
fi

if [ ! -x "$MAKE" ]; then
  $ECHO Make executable not found. MAKE=$MAKE
  exit 1;
fi

##
## Step 2: Get definitions for all platform-specific variables
##

# Definitions to Add/Remove inittab entries

ADDITAB="$CAT \$IT | $GREP -v init.evmd | $GREP -v init.cssd | $GREP -v init.crsd > \$IT.no_crs || { $ECHO $?; exit 1; } ; \
	$CAT \$IT.no_crs $CH/crs/admin/inittab > \$IT.crs || { $ECHO $?; exit 1; }; \
        $RM \$IT.no_crs 2>/dev/null; \
        $ECHO \"Adding daemons to inittab\"; \
        $CP \$IT.crs \$IT"
RMITAB="$CAT \$IT | $GREP -v init.evmd | $GREP -v init.cssd | $GREP -v init.crsd > \$IT.no_crs || { $ECHO $?; exit 1; } ; \
        $CP \$IT.no_crs \$IT || { $ECHO $?; exit 1; }"

UNAME='/bin/uname'
PLATFORM=`$UNAME`

case $PLATFORM in
SunOS)
  DIRNAME=/bin/dirname
  ID=/etc/init.d
  RCSDIR="/etc/rc3.d"
  RCKDIR="/etc/rc0.d /etc/rc1.d /etc/rc2.d /etc/rcS.d"
  RCALLDIR="/etc/rc0.d /etc/rc1.d /etc/rc2.d /etc/rc3.d /etc/rcS.d"
  RC_START=S96
  RC_KILL=K96
  IT=/etc/inittab
  INIT=/sbin/init

  LIBSKGXN=libskgxn2.so
  LIBSKGXNS=libskgxns.so
  ;;
Linux)
  DIRNAME=/usr/bin/dirname
  ID=/etc/init.d
  RCSDIR="/etc/rc.d/rc3.d /etc/rc.d/rc5.d"
  RCKDIR="/etc/rc.d/rc0.d /etc/rc.d/rc1.d /etc/rc.d/rc2.d /etc/rc.d/rc4.d /etc/rc.d/rc6.d"
  RCALLDIR="/etc/rc.d/rc0.d /etc/rc.d/rc1.d /etc/rc.d/rc2.d /etc/rc.d/rc3.d /etc/rc.d/rc4.d /etc/rc.d/rc5.d /etc/rc.d/rc6.d"

  RC_START=S96
  RC_KILL=K96
  IT=/etc/inittab
  INIT=/sbin/init
  GREP=/bin/grep

  LIBSKGXN=libskgxn2.so
  LIBSKGXNS=libskgxns.so
  ;;
HP-UX)
  MACH_HARDWARE=`/bin/uname -m`
  if [ "$MACH_HARDWARE" = "ia64" ]; then
    SO_EXT=so
  else
    SO_EXT=sl
  fi 

  DIRNAME=/bin/dirname
  ID=/sbin/init.d
  RCSDIR="/sbin/rc3.d"
  RCKDIR="/sbin/rc2.d"
  RCALLDIR="/sbin/rc2.d /sbin/rc3.d"
  RC_START=S960
  RC_KILL=K960
  IT=/etc/inittab
  INIT=/sbin/init

  LIBSKGXN=libskgxn2.$SO_EXT
  LIBSKGXNS=libskgxns.$SO_EXT
  ;;
AIX)
  DIRNAME=/bin/dirname
  ID=/etc
  RCSDIR="/etc/rc.d/rc2.d"
  RCKDIR="/etc/rc.d/rc2.d"
  RCALLDIR="/etc/rc.d/rc2.d"
  RC_START=S96
  RC_KILL=K96
  IT=/etc/inittab
  INIT=/usr/sbin/init
  ADDITAB="$ECHO \"Adding daemons to inittab\";
           while read line; \
           do \
           rflds=\`echo \$line | $CUT -d ':' -f 1-3\`; \
           lfld=\`echo \$line | $CUT -d ':' -f 4-\`; \
           mkitab \$rflds:\"\$lfld\"; \
           done < $CH/crs/admin/inittab"
  RMITAB="while read line; \
           do \
           identifier=\`echo \$line | $CUT -d ':' -f 1\`; \
           rmitab \$identifier 2>/dev/null; \
           done < $CH/crs/admin/inittab"

  LIBSKGXN=libskgxn2.a
  LIBSKGXNS=libskgxns.a
  ;;
OSF1)
  DIRNAME=/usr/bin/dirname
  ID="/sbin/init.d"
  RCSDIR="/sbin/rc3.d"
  RCKDIR="/sbin/rc3.d"
  RCALLDIR="/sbin/rc3.d"
  RC_START=S96
  RC_KILL=K96
  IT=/etc/inittab
  INIT=/sbin/init

  LIBSKGXN=libskgxn2.so
  LIBSKGXNS=libskgxns.so
  ;;
*) 
  /bin/echo "ERROR: Unknown Operating System"
  exit -1
  ;;
esac

##
## Step 2.5: verify the CRS home 
##
CLUSTERWARE_HOME=`$ID/init.crs home`
if [ "$ORA_CRS_HOME" != "$CLUSTERWARE_HOME" ];
then
    $ECHO "The clusterware daemons are running from $CLUSTERWARE_HOME. But you are patching $ORA_CRS_HOME"
    exit 1
fi

if [ ! -f $ORA_CRS_HOME/bin/crsd.bin ];
then
    $ECHO "The home $ORA_CRS_HOME is not an Oracle clusterware home"
    exit 1
fi

if [ ! -f $ORA_CRS_HOME/bin/srvctl ];
then
    $ECHO "The home $ORA_CRS_HOME is not a 10.2 Oracle clusterware home"
    exit 1
fi

##
## Step 3: Verify that the CRS daemons are down.
##

$PSE | $GREP -i 'o[c]ssd'    > /tmp/ocssd.ps
$PSE | $GREP -i 'c[r]sd.bin' > /tmp/crsd.ps
$PSE | $GREP -i 'e[v]md.bin' > /tmp/evmd.ps
PROCS=`$CAT /tmp/ocssd.ps /tmp/crsd.ps /tmp/evmd.ps`
if [ ! -z "$PROCS" ]; then
  $ECHO Please shut down the following processes before running this script.
  $CAT /tmp/ocssd.ps /tmp/crsd.ps /tmp/evmd.ps
  $RMF /tmp/ocssd.ps /tmp/crsd.ps /tmp/evmd.ps
  exit 1
fi

$RMF /tmp/ocssd.ps /tmp/crsd.ps /tmp/evmd.ps

##
## Step 4: Patch or restore Oracle clustereware files.
##
if $RESTORE_PATCH;
then
   restoreFiles || { $ECHO $?; exit 1; }
else
   patchFiles || { $ECHO $?; exit 1; }
fi

cd $CH

##
## Step 5: Reapply all permissions changes normally done in the CRS Home.
##

# Ensure that everything in the bin directory is executable
$CHMOD +x $CH/bin/*

# set ownership to root, but give execute permissions to all
$CHOWN root $CH/bin/crs*
$CHMOD 555  $CH/bin/crs*

# only owner(root) should ever invoke crsd
$CHMOD 744  $CH/bin/crsd
$CHMOD 744  $CH/bin/crsd.bin

# Set root ownership on the CRS Home
$CHMOD 755 $CH
$CHOWN root $CH

# Create $CH/log. A small number of things will use this directory
# for diagnostics.
if [ ! -d $CH/log ]; then
  $MKDIR $CH/log
fi
$CHOWN $ORACLE_OWNER $CH/log
$CHMOD 755 $CH/log

# Writable by the dba group but can only be deleted by the crshome owner
$CHMOD 1770 evm/log

# Everyone should be able to insert, but they should not be able to
# delete the directory.
$CHOWN $ORACLE_OWNER $CH/crs/public
$CHMOD 1777 $CH/crs/public

# Check that the parent directories of $CH are ownable by root.
# Any parent not owned by root may allow a non-root user to invoke
# actions as root.
d=$CH
while [ "$d" != "/" ]
do
    set -$- `$LS -ld $d`
    case $3 in
    root)   ;;      # ok
    *)      $ECHO "WARNING: directory '$d' is not owned by root";;
    esac
    d=`$DIRNAME $d`
done

##
## Step 6: Recopy things that live outside of the CRS Home
##
## NOTE: Step 6 and Step 7 are done with an enhanced level of paranoia
##       compared to previous incarnations in root.sh. The following
##       methods of modifying inittab are performed intentionally 
##       in hopes of avoiding situations where inittab gets truncated
##       to a 0 byte file, since this is especially hard to repair.
##       Some additional complexity is here because of similar reasons
##       to why we do not have or support a manual startup of CRS.
##

case $PLATFORM in
AIX)
  # Remove CRS entries from inittab
  $ECHO Preparing to recopy patched init and RC scripts.
  $CP $IT $IT.pre$PATCH_VERSION || { $ECHO $? ; exit 1; }
  eval $RMITAB
  $CP $IT $IT.nocrs$PATCH_VERSION || { $ECHO $? ; exit 1; }
  eval $SYNCALL
  $INIT q
  eval $SYNCALL
  ;;
*)
  # Remove CRS entries from inittab
  $ECHO Preparing to recopy patched init and RC scripts.
  $CP $IT $IT.pre$PATCH_VERSION || { $ECHO $? ; exit 1; }
  ($CAT $IT | $GREP -v -i "init.evmd" | $GREP -v -i "init.cssd" | \
   $GREP -v -i "init.crs"  | $GREP -v -i "init.crsd" > $IT.nocrs$PATCH_VERSION) \
        || { $ECHO $? ; exit 1; }
  $CP $IT.nocrs$PATCH_VERSION $IT.tmp || { $ECHO $? ; exit 1; }
  eval $SYNCALL
  $MVF $IT.tmp $IT || { $ECHO $? ; exit 1; }
  eval $SYNCALL
  $INIT q
  eval $SYNCALL
  ;;
esac

# Give init a brief amount of time to acknowledge the change.
$SLEEP 10

# Recopy the RC and init scripts.
$ECHO Recopying init and RC scripts.
$CP $CH/evm/admin/init.evmd $ID/init.evmd || { $ECHO $?; exit 1; }
$CP $CH/css/admin/init.cssd $ID/init.cssd || { $ECHO $?; exit 1; }
$CP $CH/crs/admin/init.crsd $ID/init.crsd || { $ECHO $?; exit 1; }
$CP $CH/crs/admin/init.crs  $ID/init.crs  || { $ECHO $?; exit 1; }

# Clean up any old init.crs scripts
for rc in $RCALLDIR
do
  $RMF $rc/"$RC_START"init.crs
  $RMF $rc/"$RC_KILL"init.crs
done

# Install new ones
for rc in $RCSDIR
do
  $LNS $ID/init.crs $rc/"$RC_START"init.crs  || { $ECHO $?; exit 1; }
done
for rc in $RCKDIR
do
  $LNS $ID/init.crs $rc/"$RC_KILL"init.crs || { $ECHO $?; exit 1; }
done


eval $SYNCALL

##
## step 6.1 perform ocrcheck before starting the CRS daemons
$CH/bin/ocrcheck > /dev/null 2>&1
if [ "$?" != "0" ]; then
  $ECHO "ocrcheck failed. Check $CH/srvm/log for more details"
  exit 1;
fi

##
## Step 7: Now try to start up the CRS daemons again.
##
## WARNING: Doing this manually is discouraged.
##

$ID/init.crs start
$ID/init.cssd startcheck css
if [ ! $? ]; then
  $ECHO Failed at startcheck. See the system message file for details.
  exit 1;
fi

case $PLATFORM in
AIX)
  $ECHO Starting up the CRS daemons.
  eval $ADDITAB  
  eval $SYNCALL
  $INIT q
  eval $SYNCALL
  ;;
*)
  $ECHO Starting up the CRS daemons.
  $CAT $IT.nocrs$PATCH_VERSION $CH/crs/admin/inittab > $IT.tmp || { $ECHO $?; exit 1; }
  eval $SYNCALL
  $MVF $IT.tmp $IT || { $ECHO $?; exit 1; }
  eval $SYNCALL
  $INIT q
  eval $SYNCALL
  ;;
esac

works=false
# Wait a few minutes to see if the CRS daemons come up. On some systems if
# the init daemon is relatively unresponsive this can take 5 or more minutes.
$ECHO "Waiting for the patched CRS daemons to start."
$ECHO "  This may take a while on some systems."
for i in 30 60 90 120 150 180 210 240 270 300 330 360 390 420 450 480 510 540 570 600
do 
  if $CH/bin/crs_stat > /dev/null 2>&1
  then
    works=true
    break
  fi 

  $ECHO "."
  $SLEEP 30
done

##
## Step 7: Verify Clusterware software version 
##

if $works
then
  $ECHO "$PATCH_VERSION patch successfully applied."
  # Startup was successful. Continue with the remainder.

  ##run clscfg upgrade
  if $CH/bin/clscfg -upgrade
  then
     $ECHO "clscfg -upgrade completed successfully"
  else
     $ECHO "clscfg -upgrade failed"
     exit 1
  fi

#   SOFTWAREVERSION=`$CRSCTL query crs softwareversion | $AWK '{ print $NF }' | $SED 's/\[//g' | $SED 's/\]//g'`

#   if [ "$SOFTWAREVERSION" != "$PATCH_VERSION" ];
#   then
#      $ECHO "CRS upgrade to $PATCH_VERSION failed on this node"
#      exit 1
#   else
#      $ECHO "CRS stack on this node, is successfully upgraded to $PATCH_VERSION"
#    fi
else
  $ECHO "Timed out waiting for the CRS daemons to start. Look at the"
  $ECHO "system message file and the CRS log files for diagnostics."
  exit 1
fi

