#!/bin/sh

# preroot.sbs for CRS
#
# This is run once per node when upgrading or patching CRS homes. 
# This script will stop the CRS stack running on the node
# The script will also change ownership of CRS home and root owned files 
# to Oracle CRS user.
#
# USAGE: preupdate.sh -crshome <Oracle CRS home location> -crsuser <CRS user name> [-noshutdown]

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

ORACLE_HOME=%s_OracleHome%
ORACLE_OWNER=%OwnerId%
export ORACLE_HOME
ORA_CRS_HOME=$ORACLE_HOME

if [ -z "$ECHO" ]; then ECHO=/bin/echo; fi
if [ -z "$CHOWN" ]; then CHOWN="/bin/chown -h"; fi
if [ -z "$CHMOD" ]; then CHMOD=/bin/chmod; fi
if [ -z "$AWK" ]; then AWK=/bin/awk; fi
if [ -z "$GREP" ]; then GREP=/bin/grep; fi
if [ -z "$RMF" ]; then RMF="/bin/rm -f"; fi
if [ -z "$CAT" ]; then CAT=/bin/cat; fi
if [ -z "$IDCMD" ]; then IDCMD=/usr/bin/id; fi
if [ -z "$SLEEP" ]; then SLEEP=/bin/sleep; fi
if [ -z "$CHOWNH" ]; then CHOWNH="/bin/chown -h"; fi

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

# This script is intended to be run by root.

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

USER_ARGS=$*
CRS_SHUTDOWN=true
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
       ORACLE_OWNER=$2;
       shift;
    else
       usage;
       exit 2
    fi
  elif [ $1 = "-noshutdown" ]; then
    CRS_SHUTDOWN=false
  fi
  shift;
done

export ORA_CRS_HOME
CH=$ORA_CRS_HOME

valid=`$ECHO $ORA_CRS_HOME | $AWK '/^%/ { print "false"; }'`
if [ "$valid" = "false" ];
then
   $ECHO "Specify Oracle CRS home path using the command line option";
   usage;
   exit 2
fi

valid=`$ECHO $ORACLE_OWNER | $AWK '/^%/ { print "false"; }'`
if [ "$valid" = "false" ];
then
   $ECHO "Specify Oracle CRS use name using the command line option";
   usage;
   exit 2
fi

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

case $PLATFORM in
Linux) ID=/etc/init.d
       ;;
HP-UX) ID=/sbin/init.d
       ;;
SunOS) ID=/etc/init.d
       ;;
AIX)   ID=/etc
       ;;
OSF1)  ID=/sbin/init.d
       ;;
*)     ID=/etc/init.d
       ;;
esac

# Changes for bug 7551160
if [ $PLATFORM = "SunOS" ]; then
     PS="/bin/ps"
     ZONENAME="/bin/zonename"
     MAJOROS=`$UNAME -r | $AWK -F. '{print $1}'`
     MINOROS=`$UNAME -r | $AWK -F. '{print $2}'`
fi

if $CRS_SHUTDOWN;
then
   ###############################
   #STEP 1: stop CRS stack
   ###############################
   $ID/init.crs stop

   for i in 5 10 15 20 25 30 35 40 45 50 55 60
   do
       $ECHO "Checking to see if Oracle CRS stack is down..."
       if $CH/bin/crs_stat > /dev/null 2>&1
       then
	   $ECHO ""
       else
           break;
       fi
       $SLEEP 5
   done

   if [ $PLATFORM = "AIX" ];
   then
       /usr/sbin/slibclean;
   fi
   ##
   ## Step 2: Verify that the CRS daemons are down.
   ##
   for j in 5 10 15 20 25 30 35 40 45 50 55 60
   do
       GREPCMD=`basename $GREP`
       # Changes for bug 7551160
       if [ $PLATFORM = "SunOS" -a $MAJOROS -ge 5 -a $MINOROS -ge 10 -a -f $ZONENAME ]; then
              $PS -z `$ZONENAME` | $GREP -i 'oc[s]sd'  > /tmp/ocssd.ps
              $PS -z `$ZONENAME` | $GREP -i 'cr[s]d.bin'  > /tmp/crsd.ps
              $PS -z `$ZONENAME` | $GREP -i 'ev[m]d.bin'  > /tmp/evmd.ps
       else
              $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
       fi
       PROCS=`$CAT /tmp/ocssd.ps /tmp/crsd.ps /tmp/evmd.ps`
       if [ ! -z "$PROCS" ]; then
           $ECHO Waiting for the following processes to shut down:
           $CAT /tmp/ocssd.ps /tmp/crsd.ps /tmp/evmd.ps
           $SLEEP 5
       else
           $RMF /tmp/ocssd.ps /tmp/crsd.ps /tmp/evmd.ps
           $ECHO "Oracle CRS stack is down now."
           break;
       fi
       
       #bail after a min of waiting
       if [ $j -gt 55 ]; then
           $ECHO Unable to shut down the following processes. Please
           $ECHO make sure they are stopped and re-run the script
           $CAT /tmp/ocssd.ps /tmp/crsd.ps /tmp/evmd.ps
           exit 1
       fi
       $RMF /tmp/ocssd.ps /tmp/crsd.ps /tmp/evmd.ps
   done
fi

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

$CHOWNH $ORACLE_OWNER $CH/bin/crs* 
$CHMOD u+rwx  $CH/bin/crs* 

$CHOWNH $ORACLE_OWNER $CH/bin/racg* 
$CHMOD u+rwx  $CH/bin/racg* 

$CHOWNH $ORACLE_OWNER $CH/racg
$CHMOD u+rwx  $CH/racg

$CHOWNH $ORACLE_OWNER $CH/racg/admin 
$CHMOD u+rwx  $CH/racg/admin

$CHOWNH $ORACLE_OWNER $CH 
$CHMOD u+rwx $CH

for d in bin crs crs/init crs/profile crs/script crs/template crs/auth
do
    $CHOWNH $ORACLE_OWNER $CH/$d 
    $CHMOD u+rwx $CH/$d 
done

$CHOWNH -R $ORACLE_OWNER $CH/lib 
$CHMOD -R u+rwx $CH/lib

if [ -d $CH/lib32 ];
then
   $CHOWNH -R $ORACLE_OWNER $CH/lib32
   $CHMOD -R u+rwx $CH/lib32
fi

# These are writeable by all
for d in crs/public
do
    $CHOWNH $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

for d in crs css evm racg srvm has
do
    $CHMOD u+w $CH/$d/mesg/*.msb
    $CHMOD u+w $CH/$d/mesg/*.msg
done

$CHMOD u+w $CH/crs/admin/init.crs $CH/crs/admin/init.crsd 
$CHMOD u+w $CH/css/admin/init.cssd $CH/evm/admin/init.evmd

#Change permissions on CRSHome/install/root*
$CHMOD u+w $CH/install/root*

if [ $PLATFORM = "AIX" ]; then
#On AIX, due to an OS restriction, even after a process exits, it keeps the
#shared libraries loaded in the memory and does not release it immediately.
#Due to this some of the libraries does not get patched which was causing run
#time issues.
#To avoid this problem, the following actions need to be performed while
#running preupdate.sbs
#Do not use version numbers like 10, 11 for libocr* libraries. bug#6358864

        $CHMOD 755 $CH/lib/libhasgen*    2>/dev/null ;
        $CHMOD 755 $CH/lib/libskgxn*     2>/dev/null ;
        $CHMOD o-r $CH/lib/libskgxnr.a   2>/dev/null ;
        $CHMOD o-r $CH/lib/libskgxnr.so  2>/dev/null ;
        $CHMOD o-r $CH/lib/libskgxns.a   2>/dev/null ;
        $CHMOD o-r $CH/lib/libskgxns.so  2>/dev/null ;
        $CHMOD o-r $CH/lib/libskgxn2.a   2>/dev/null ;
        $CHMOD o-r $CH/lib/libskgxn2.so  2>/dev/null ;
        $CHMOD o-r $CH/lib/libhasgen*.so 2>/dev/null ;
        $CHMOD o-r $CH/lib/libttsh*.so   2>/dev/null ;
        $CHMOD o-r $CH/lib/libocr*.so    2>/dev/null ;
        $CHMOD o-r $CH/lib/libocrb*.so   2>/dev/null ;
        $CHMOD o-r $CH/lib/libocrutl*.so 2>/dev/null ;
        $CHMOD o-r $CH/lib/libclntsh.a   2>/dev/null ;
        $CHMOD o-r $CH/lib/libclntsh.so  2>/dev/null ;
fi
