#!/bin/sh
#
# prerootpatch.sh
#
# Copyright (c) 2004, 2009, Oracle and/or its affiliates. All rights reserved. 
#
#    NAME
#      prerootpatch.sh - Detects CRS stack status and prompts the user
#                        and unlocks CRS home
#
#    DESCRIPTION
#    This is run once per node when patching CRS homes. 
#    This script will detect if CRS stack is running on the local node
#    The script will also change ownership of CRS home and root owned files 
#    to Oracle CRS user.
#
#    If the CRS Home is shared among nodes, then the CRS stack must be stopped
#    on all nodes before running this script.
#
#    If each node has its own copy of the CRS Home, then the CRS stack needs
#    to be stopped on the local node prior to running this script.
#
#    NOTES
#
#    MODIFIED   (MM/DD/YY)
#       gnagiah  08/27/09 - 8531031 some files not instantiated properly
#       gnagiah  08/20/09 - 8781354 fix chmod shell buffer limits
#       jcreight 03/18/09 - Fix bug 8337092
#       smatpadi 03/31/08 - Bug 6831670
#       jcreight 03/18/08 - Backport jcreight_bug-6878939 from main
#       jcreight 11/07/07 - XbranchMerge jcreight_bug-6075415 from st_has_10.2
#       jcreight 10/24/07 - Fix bug 6075415: check crs home
#       jleys    10/15/07 - Add upgrade warning if using UDLM clusterware
#       vrai     06/22/07 - XbranchMerge vrai_bug-5547807 from main
#       jtellez  03/14/07 - Modified history tag added
#

ECHO=/bin/echo
CHOWN=/bin/chown
CHMOD=/bin/chmod
AWK=/bin/awk
GREP=/bin/grep
RMF="/bin/rm -f"
CAT=/bin/cat
ID=/usr/bin/id
FIND=/usr/bin/find
XARGS=/usr/bin/xargs

# Needs to print out (at least) the basename of all processes on the box
PSE="/bin/ps -e"

usage()
{
   printf "Usage:"
   printf "$0 -crshome <Oracle CRS home location> -crsuser <CRS user name>"
}

##Function for parsing the command line arguments
parseArgs()
{
   USER_ARGS=$*

   while [ $# -gt 0 ];
   do
    if [ $1 = "-crshome" ]; then
       if [ $# -ge 2 ]; then
	 ORA_CRS_HOME=$2;
         shift;
       else
	 usage;
	 exit 2
       fi
     elif [ $1 = "-crsuser" ]; then
       if [ $# -ge 2 ]; then
	 CRS_ORACLE_OWNER=$2;
         shift;
       else
	 usage;
	 exit 2
       fi
     else
       usage;
       exit 2
     fi
     shift;
   done
}

# This script is intended to be run by root.

RUID=`$ID|$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

parseArgs $*

if [ -z "$ORA_CRS_HOME" ];
then
   $ECHO "Oracle CRS home is not specified"
   usage;
   exit 1
fi

ORACLE_HOME=$ORA_CRS_HOME
export ORACLE_HOME
export ORA_CRS_HOME
CH=$ORA_CRS_HOME

if [ -z "$CRS_ORACLE_OWNER" ];
then
   $ECHO "Oracle CRS user is not specified"
   usage;
   exit 1
fi


if [ ! -f "$CH/bin/crs_stat" ];
then
  $ECHO "Cannot locate <CRS home>/bin/crs_stat"
  $ECHO "Plese check that a valid CRS home is being specified."
  exit 1
fi

crsparamsfile="$CH/install/params.crs"

if [ -f "$crsparamsfile" ];
then
   crs_home_chk="`$GREP ORA_CRS_HOME $crsparamsfile | $AWK -F= '{print $2}'`"
   if [ $crs_home_chk != $CH ];
   then
      $ECHO "This home does not match location as defined in params.crs"
      exit 1
   fi
fi 

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

case $PLATFORM in
Linux) 
    ID=/etc/init.d
       ;;
HP-UX) ID=/sbin/init.d
   HPUXCHOWN="$CHOWN $CRS_ORACLE_OWNER $CH/lib/libhasgen11.* $CH/lib/libttsh11.* $CH/lib32/libttsh11.* $CH/lib32/libdbcfg11.* $CH/lib32/libhasgen11.* $CH/lib32/libocr11.* $CH/lib32/libocrb11.* $CH/lib32/libocrutl11.* $CH/lib32/libsrvm11.* $CH/lib32/libsrvmhas11.* $CH/lib32/libsrvmocr11.* $CH/lib32/libuini11.* $CH/lib32/libemmas11.*"
   HPUXCHMOD="$CHMOD u+w  $CH/lib/libhasgen11.* $CH/lib/libttsh11.* $CH/lib32/libttsh11.* $CH/lib32/libdbcfg11.* $CH/lib32/libhasgen11.* $CH/lib32/libocr11.* $CH/lib32/libocrb11.* $CH/lib32/libocrutl11.* $CH/lib32/libsrvm11.* $CH/lib32/libsrvmhas11.* $CH/lib32/libsrvmocr11.* $CH/lib32/libuini11.* $CH/lib32/libemmas11.*"
       ;;
SunOS) 
    ID=/etc/init.d
       ;;
AIX)   
    ID=/etc
#
#   For the AIX platform we need to do the following, unlock this lib remove the read permission.
#   This is here as it is an AIX specific library, and not a platform generic one, so the actions
#   are defined here and not at the end like the others. Also we can slip in the slibclean here as
#   it needs to be run as root and this is the last action to be done when on AIX.
#
   AIXCHMOD="$CHMOD o-r $CH/lib/libttsh11.* $CH/lib32/libttsh11.* $CH/lib/libocr11.* $CH/lib/libocrb11.* $CH/lib/libhasgen11.* $CH/lib/libocrutl11.* $CH/lib/libdbcfg11.* $CH/lib/libttsh11.*"
   AIXCHOWN="$CHOWN $CRS_ORACLE_OWNER $CH/lib/libhasgen11.* $CH/lib/libttsh11.* $CH/lib32/libttsh11.*"
       ;;
OSF1)  
    ID=/sbin/init.d
       ;;
*)  $ECHO "ERROR: Unknown Operating System"
    exit -1
       ;;
esac

########################################################
#STEP 1: Check if Oracle CRS stack is down on local node
#######################################################
CRS_UP=false
$ECHO "Checking to see if Oracle CRS stack is down..."
if $CH/bin/crs_stat > /dev/null 2>&1
then
    CRS_UP=true
fi

$ID/init.cssd startcheck
if [ "$?" = "0" ]; then
    CRS_UP=true
fi

if [ $CRS_UP = "true" ];
then
    $ECHO "Oracle CRS stack is run status. Stop the CRS stack using the following command"
    $ECHO "$ID/init.crs stop"
    $ECHO "If the CRS Home is shared, also make sure that the Oracle CRS stack is stopped on other cluster nodes"
    exit 1
fi

##
## Step 2: Verify that the CRS daemons are down.
##
$PSE | $GREP -i 'oc[s]sd' > /tmp/ocssd.ps
$PSE | $GREP -i 'cr[s]d.bin' > /tmp/crsd.ps
$PSE | $GREP -i 'ev[m]d.bin' > /tmp/evmd.ps
PROCS=`$CAT /tmp/ocssd.ps /tmp/crsd.ps /tmp/evmd.ps`
if [ ! -z "$PROCS" ]; then
  $ECHO 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
else
  $ECHO "Oracle CRS stack is down now."
fi

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

######################################
#STEP 2: Unlock CRS Home for CRS user
######################################
# set ownership to CRS user, but give all permissons to CRS user

$CHOWN $CRS_ORACLE_OWNER $CH/bin/crs* $CH/bin/racg*
$CHMOD u+rwx $CH/bin/racg*
$FIND $CH/bin $CH/lib -print | $XARGS $CHMOD u+rwx

for d in . lib bin crs crs/init crs/profile crs/script crs/template crs/auth srvm/mesg crs/mesg css/mesg evm/mesg racg/mesg racg/admin
do
    $CHOWN $CRS_ORACLE_OWNER $CH/$d 
    $CHMOD u+rwx $CH/$d 
done

# These are writeable by all
for d in crs/public
do
    $CHOWN $CRS_ORACLE_OWNER $CH/$d 
    $CHMOD u+rwx $CH/$d 
done

for d in evm evm/init
do
    $CHMOD u+rwx $CH/$d 
done

for d in css css/init css/log css/auth
do
    $CHMOD u+rwx $CH/$d 
done

$CHOWN $CRS_ORACLE_OWNER $CH/install/rootconfig
$CHMOD u+rwx $CH/install/rootconfig

case $PLATFORM in
AIX)   $AIXCHOWN
       $AIXCHMOD
       slibclean
       ;;
HP-UX)  $HPUXCHOWN
        $HPUXCHMOD
       ;;
*)
       ;;
esac


