#!/bin/sh
# Copyright (c) 2004, Oracle. All rights reserved.  
#
# prepatchverify.sh
#    Script run as part of the CRS Home patch procedure. This script is:
#
#    1) invoked as the CRS Home user (non-root)
#    2) invoked by opatch
#    3) the stdout of the script is shown to the user for error messages
#    4) verifies that prepatch.sh and prerootpatch.sh have been run.
#    5) verifies that the CRS stack is down on the local node
#

# Fetch the CRS Home path from the environment. The user is expected
# to have set this environment variable.
CH=$ORACLE_HOME
PARAMETER_FILE=$CH/install/params.crs
CRSD=$CH/bin/crsd

ECHO="/bin/echo"
PSE="/bin/ps -e"
UNAME="/bin/uname"
GREP="/bin/grep"
CAT="/bin/cat"
RM="/bin/rm"
RMF="$RM -f"

if [ -z "$CH" -o ! -d "$CH" ]; then
  $ECHO "Define the ORACLE_HOME environment variable to point"
  $ECHO "  to your CRS home directory before invoking opatch."
  exit 1;
fi

if [ ! -d "$CH/crs/admin" ]; then
  $ECHO "CRS Home not found at path $CH. Define the ORACLE_HOME environment"
  $ECHO "  variable to point to your CRS home directory before invoking opatch."
  exit 1;
fi

if [ ! -w "$CRSD" ]; then
  $ECHO "Pre-patch script prerootpatch.sh has not been run."
  $ECHO "Run prerootpatch.sh and prepatch.sh before invoking opatch."
  exit 1;
fi

if [ ! -f "$PARAMETER_FILE" ]; then
  $ECHO "Pre-patch script prepatch.sh has not been run."
  $ECHO "Run prerootpatch.sh and prepatch.sh before invoking opatch."
  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 "CRS daemons are still online."
  $ECHO "Run prerootpatch.sh and prepatch.sh before invoking opatch."
  $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

$ECHO "Pre-patch script verification complete."
exit 0;
