#!/bin/sh

######################################################################
# Copyright (c) 2004, 2011, Oracle and/or its affiliates. All rights reserved. 
#
#  phnguyen  5/17/04   Create and support
#                          -jdk, -jre, -oh
#  shgangul  6/21/04   Check for OPATCH_DEBUG env variable
#  shgangul  7/12/04   opatch can be invoked from any directory
#  shgangul  7/15/04   use jdk to invoke opatch, otherwise use jre
#  shgangul  7/16/04   Display the Java call for debug
#  phnguyen  7/19/04   add "./" to class path
#  phnguyen  7/31/04   more jre parsing, error if can't find java
#  shgangul  8/05/04   change opatch to oracle.opatch package
#  shgangul  8/27/04   sync up with opatch.bat to support OPATCH_PLATFORM_ID
#  phnguyen  8/04/04   Support OPATCH_PLATFORM_ID=integer
#  shgangul  8/27/04   add fallback schemes for jre/jdk
#  phnguyen  8/30/04   Print "OPatch Succeeded" if OK
#  shgangul  9/01/04   Further changes for jre/jdk priorities
#  shgangul  9/07/04   java not present in jdk/jre option, error out
#  shgangul  12/15/04  Supply PATH variable to java
#  shgangul  02/02/05  Minor bug in parsing -jdk option
#  phnguyen  02/01/05  Introduce OPATCH_NO_FUSER to bypass fuser check
#  vsriram   02/28/05  Print "OPatch Succeeded" if exit code is b/w 171 and 200
#  shgangul  03/15/05  Pass in properties to OPatch
#  phnguyen  05/14/05  add opatchprereq.jar and opatchutil.jar to class path 
#  shgangul  07/18/05  Do not pass PATH env var to OPatch JVM
#  vsriram   12/06/05  Change the permissions of .patch_storage and set umask
#  vsriram   12/13/05  Use -d inside if condition, as -e doesnot work on Solaris.
#  vsriram   01/24/06  Remove umask setting and don't change the perm of 
#                      .patch_storage contents
#  vsriram   05/10/06  Return 0 as exit code, even for warnings.
#  vsriram   06/15/06  Put the OPatch/jlib/xmlparserv2.jar in classPath
#  vsriram   06/21/06  Look for Jre 1.5 and then fall back to JDK.
#  vsriram   08/01/06  Remove dependency on xmlparserv2.
#  vsriram   08/17/06  Add opatchactions.jar into classpath
#  phnguyen  10/25/07  Get JRE_MEMORY_OPTIONS fr oui/oraparam.ini, bugsnhkcnt
#  vganesan  02/27/08  Pass $BASE (running directory of 'opatch') as a property
#                      to java invocation. This is needed to locate ocm.zip in 
#                      OPatch.
#  vganesan  03/04/08  Add 'emocmutl.jar' to the classpath
#  vganesan  08/19/08  Check osInfo API/class in emocmutl.jar for IBM based
#                      platforms if 'ocm.zip' is bundled
#  phnguyen  05/20/08  Bug bmmbhmb
#  phnguyen  05/20/08  Bug 7114214

#  vganesan  05/16/08  Bug bkbtkcm
#  vganesan  05/16/08  Bug 7038091
#  phnguyen  10/25/07  Get JRE_MEMORY_OPTIONS fr oui/oraparam.ini, bug-snhkcnt
#  phnguyen  10/25/07  Get JRE_MEMORY_OPTIONS fr oui/oraparam.ini, bug6520958
#  vganesan  03/13/09  Add 'orai18n-mapping.jar' to the classpath
#  phnguyen  06/05/09  Support 'opatch auto [-rollback]'. Need to deal with "abc"
#  phnguyen  06/08/09  Error out of 'opatch auto' is called on a non-crs bundle
#  vganesan  06/23/09  Add 'opatchext.jar' to OPatch classpath 
#  supal     06/29/09  Set MW_HOME, ORACLE_HOME when invoked from FMW Oracle Home
#  vganesan  07/04/09  Add 'opatchfmw.jar' to OPatch classpath
#  supal     10/05/09  Better messages for MW/OH consistency check
#  vganesan  09/09/09  Fix Bug #8881921
#  vganeasn  10/20/09  Initialize FMW_ERROR to -1
#  phnguyen  11/13/09  Support 11.2 auto_patch.pl
#  supal     11/11/09  Pass Common Home Location to OPatch
#  phnguyen  05/10/10  Remove custom/server check in 'opatch auto'
#  vganesan  02/22/11  Change to look for OsInfo from emocmcommon.jar
#                      Double quote JRE_MEMORY_OPTIONS value in fmw area
#  cawan     05/17/11  Fix Bug #10212388,#10225431
####################################################################

BASE=`dirname $0`;
PWD=`pwd`;

autopatch=0;
autohelp=0;

argCount=$#

# Look for OPATCH_DEBUG env variable
DEBUG=${OPATCH_DEBUG}

# Check for OPATCH_DEBUG
DEBUGVAL=false
if [ "$DEBUG" = "TRUE" ]; then
    DEBUGVAL=true;
fi
if [ "$DEBUG" = "true" ]; then
    DEBUGVAL=true;
fi


if [ "$1" = "auto" ]; then
  autopatch=1
fi

if [ $autopatch = 1 ]; then
  if [ $argCount -gt 1 ]; then
    if [ "$2" = "-h" ]; then
      autohelp=1
    elif [ "$2" = "-help" ]; then
      autohelp=1
    fi
  fi
fi

if [ $autopatch = 1 ]; then
   CMD_ID_EXIST=`whereis id`
   if [ "$CMD_ID_EXIST" = "id:" ]; then
       echo id command not available!
       echo opatch auto can not proceed without id command!
       exit 1
   fi
fi

UID_INFO=`id`
IS_ROOT=`echo $UID_INFO | grep "^uid=0("`
if [ ! "$IS_ROOT" = "" ]; then
    if [ "$1" = "-h" ]; then
        autohelp=1
    elif [ "$1" = "-help" ]; then
        autohelp=1
    fi
fi
                                    
if [ $autohelp = 1 ]; then
  $BASE/crs/auto_patch.pl -h
  exit 
fi

if [ $autopatch = 1 ]; then
  # it must be 
  # 'opatch auto'
  # 'opatch auto <patch loc>'
  # 'opatch auto -rollback'
  # 'opatch auto -rollback <patch loc>' 

  if [ $argCount -eq 1 ]; then
     # this must be just 'opatch auto'
     givenLoc=$PWD
     patchdir=`dirname $givenLoc`
     id=`basename $givenLoc`

     # Check to make sure <patch_loc>/custom/server exist
     # if [ ! -d $givenLoc/custom/server ]; then
     #    echo "$givenLoc is not a valid CRS bundle patch location."
     #    $BASE/crs/auto_patch.pl -h
     #    exit -1;         
     # fi

     shift
     # echo $BASE/crs/auto_patch.pl -patchdir $patchdir -patchn $id
     $BASE/crs/auto_patch.pl -patchdir $patchdir -patchn $id
     exit

  elif [ $argCount -gt 1 ] && [ $2 != "-rollback" ]; then
     # either 'opatch auto [other-args]' or 'opatch auto <patch_loc> [other-args]'

     givenLoc=$2
     if [ -d $givenLoc ]; then
        # must be 'opatch auto <patch_loc> other-args'
        # echo "opatch auto <patch_loc> [other-args]"
        givenLoc=$2
        patchdir=`dirname $givenLoc`
        id=`basename $givenLoc`       

        # Check to make sure <patch_loc>/custom/server exist
        # if [ ! -d $givenLoc/custom/server ]; then
        #   echo "$givenLoc is not a valid CRS bundle patch location."
        #   $BASE/crs/auto_patch.pl -h
        #   exit -1;
        # fi

        shift
        shift
        # echo $BASE/crs/auto_patch.pl -patchdir $patchdir -patchn $id $@
        $BASE/crs/auto_patch.pl -patchdir $patchdir -patchn $id $@
        exit
     else
        # must be 'opatch auto [other-args]' or bad patch loc
        # just let auto_patch decide, assume it is 'opatch auto [other-args]
        # echo "opatch auto [other-args]"
        givenLoc=$PWD
        patchdir=`dirname $givenLoc`
        id=`basename $givenLoc`

        # Check to make sure <patch_loc>/custom/server exist
        # if [ ! -d $givenLoc/custom/server ]; then
        #   echo "$givenLoc is not a valid CRS bundle patch location."
        #   $BASE/crs/auto_patch.pl -h
        #   exit -1;
        # fi

        shift
        # echo $BASE/crs/auto_patch.pl -patchdir $patchdir -patchn $id $@
        $BASE/crs/auto_patch.pl -patchdir $patchdir -patchn $id $@
        exit 
     fi 

  elif [ $argCount -gt 1 ] && [ $2 = "-rollback" ]; then
     givenLoc=$2
     # either 'opatch auto -rollback [other-args] or 'opatch auto -rollback <patchloc> [other-args]'
     if [ $argCount -eq 2 ]; then
        # must be just 'opatch auto rollback' 
        givenLoc=$PWD
        patchdir=`dirname $givenLoc`
        id=`basename $givenLoc`

        # Check to make sure <patch_loc>/custom/server exist
        # if [ ! -d $givenLoc/custom/server ]; then
        #   echo "$givenLoc is not a valid CRS bundle patch location."
        #   $BASE/crs/auto_patch.pl -h
        #   exit -1;
        # fi

        shift
        shift
        # echo $BASE/crs/auto_patch.pl -rollback -patchdir $patchdir -patchn $id $@
        $BASE/crs/auto_patch.pl -rollback -patchdir $patchdir -patchn $id $@
        exit
    
     elif [ -d $3 ]; then
        # must be 'opatch auto -rollback <patch_loc> [other-args]
        # echo "opatch auto -rollback <patch_loc> [other-args]"
        givenLoc=$3
        patchdir=`dirname $givenLoc`
        id=`basename $givenLoc`

        # Check to make sure <patch_loc>/custom/server exist
        # if [ ! -d $givenLoc/custom/server ]; then
        #  echo "$givenLoc is not a valid CRS bundle patch location."
        #   $BASE/crs/auto_patch.pl -h
        #   exit -1;
        # fi

        shift
        shift
        shift
        # echo $BASE/crs/auto_patch.pl -rollback -patchdir $patchdir -patchn $id $@
        $BASE/crs/auto_patch.pl -rollback -patchdir $patchdir -patchn $id $@
        exit
     else
        # must be 'opatch auto -rollback [other-args]' or bad patch loc
        # just let auto_patch decide, assume it is 'opatch auto -rollback [other-args]
        # echo "opatch auto -rollback [other-args]"
        givenLoc=$PWD
        patchdir=`dirname $givenLoc`
        id=`basename $givenLoc`

        # Check to make sure <patch_loc>/custom/server exist
        # if [ ! -d $givenLoc/custom/server ]; then
        #   echo "$givenLoc is not a valid CRS bundle patch location."
        #   $BASE/crs/auto_patch.pl -h
        #   exit -1;
        # fi

        shift
        shift
        # echo $BASE/crs/auto_patch.pl -rollback -patchdir $patchdir -patchn $id $@
        $BASE/crs/auto_patch.pl -rollback -patchdir $patchdir -patchn $id $@
        exit

     fi
  else 
 
    echo "Please run 'opatch auto [-rollback] <patch location>'"
    # echo "           'opatch auto [-rollback]' if you are in the patch location." 
    exit -1
  fi
fi

# Get ORACLE_HOME from environment variable "ORACLE_HOME"
OH=${ORACLE_HOME}

# Get Middleware Home from environment variable "MW_HOME"
MWH=${MW_HOME}

# Look for OPATCH_PLATFORM_ID
PLATFORM=${OPATCH_PLATFORM_ID}

# Look for OPATCH_NO_FUSER
NO_FUSER=${OPATCH_NO_FUSER}

# need implicit Oracle Home?
NO_IMPLICIT_ORACLE_HOME=${OPATCH_NO_IMPLICIT_OH}

# Look for ORACLE_OCM_SERVICE
oracleOcmService=""
if [ "$ORACLE_OCM_SERVICE" != "" ] ; then
  oracleOcmService="-Docm.endpoint=${ORACLE_OCM_SERVICE}"
fi


# Preserve the PATH environment variable
PATHENV=${PATH}

# BugFix for 4745222
PLATFORMNAME=`uname`;
if [ "$PLATFORMNAME" = "AIX" ] ; then
   OBJECT_MODE="32_64"
   export OBJECT_MODE
fi

# If -oh is specified, use it to over-ride env. var. ORACLE_HOME
getOH=0

# All the OPatch properties to be passed in to Java
# Format for properties is abc=xyz
PROPERTIES=""

# If -jre or -jdk are specified, use it to launch opatch,
#   with -jdk > -jre.  And we expect there is a "bin/java" underneath
#   the value supplied
getJRE=0
getJDK=0

JDK=""
JRE=""

# If -mw_home is specified, use it for the next of the session 
# after verification of its integrity
getMWHOME=0

for arg in "$@"; do
  if [ $getOH = 1 ]; then
     OH=$arg
     getOH=0
  fi
  if [ $getJRE = 1 ]; then
     JRE=$arg
     getJRE=0
  fi
  if [ $getJDK = 1 ]; then
     JDK=$arg
     getJDK=0
  fi
  if [ $getMWHOME = 1 ]; then
     MWH=$arg
     getMWHOME=0
  fi

  if [ "$arg" = "-oh" ]; then
     getOH=1;
  fi
  if [ "$arg" = "-jre" ]; then
     getJRE=1;
  fi
  if [ "$arg" = "-jdk" ]; then
     getJDK=1;
  fi
  if [ "$arg" = "-mw_home" ]; then
     getMWHOME=1;
  fi
done

# Error out if OH is not set and env var OPATCH_NO_IMPLICIT_OH is set to 'true'
if [ "$OH" = "" ] && [ "$NO_IMPLICIT_ORACLE_HOME" = "true" ]; then
    echo Oracle Home is not set. OPatch cannot proceed!
    echo "OPatch returns with error code = 1"
    exit 1
fi


# Calculate the ORACLE_HOME/MW_HOME relative to this script...
if [ -z "$OH" ]; then

   # Note: this will not work if the script is sourced (. ./wlst.sh)
   SCRIPTNAME=$0
   case ${SCRIPTNAME} in
   /*)  SCRIPTPATH=`dirname ${SCRIPTNAME}` ;;
    *)  SCRIPTPATH=`dirname ${PWD}/${SCRIPTNAME}` ;;
   esac

   C_ORACLE_HOME=`cd ${SCRIPTPATH}/.. ; pwd`
   C_MW_HOME=`cd ${C_ORACLE_HOME}/.. ; pwd`
   if [ "$DEBUGVAL" = "true" ]; then
      echo ORACLE_HOME is NOT set at OPatch invocation 
   fi
else

   # User set Oracle Home, check later if MW home is consistently set too
   if [ ! -d "${OH}" ]; then
      echo The Oracle Home $OH is not valid directory. Please give proper Oracle Home.
      echo "OPatch returns with error code = 1"
      exit 1;
   fi
   C_ORACLE_HOME=${OH}
   # Calculate Middleware Home simply by moving up IFF User set Oracle Home
   C_MW_HOME=`cd ${OH}/.. ; pwd`
   if [ "$DEBUGVAL" = "true" ]; then
      echo ORACLE_HOME is set at OPatch invocation 
   fi
fi

# Check consistency of Oracle Home and Middleware Home values (within each
# OPatch cmd only if applicable)
if [ ! -z "${MWH}" ] ; then
   # The user could have set Middleware Home either with the cmd line option
   # or by setting the environment variable MW_HOME, it is captured in MWH
   # Fixup if they put the PATH separator at the end
   #
   MWH=`echo $MWH | sed -e 's|/$||g'`
fi

#Check if this is really a FMW home - WebTier and some others create fake
#Middleware homes. If TRUE Middleware Home, we are guaranteed of the
#presence of registry.dat
#Note: A TRUE Middleware Home can be established ONLY by the WebLogic installer
FMW_ERROR=0
if [ -f "${C_MW_HOME}/registry.dat" ]; then
   #Invoking the setHomeDirs.sh script to set the WebLogic environment

   # Set the MW_HOME env variable temporarily for the following scripts
   SET_MW_HOME=${MW_HOME}
   MW_HOME=${C_MW_HOME}

   if [ -x "${C_ORACLE_HOME}/common/bin/setHomeDirs.sh" ]; then
      . "${C_ORACLE_HOME}/common/bin/setHomeDirs.sh"
   fi
   if [ "$DEBUGVAL" = "true" ]; then
      echo WL_HOME is set by setHomeDirs.sh script to $WL_HOME 
   fi
   if [ "$WL_HOME" = "" ] || [ ! -d "$WL_HOME" ]; then
      # Is it still possible to run without the WL_HOME? Only if
      # we are able to find the JVM launcher (JDK/JRE). The OPatch
      # Java code can decide which commands can be run without the
      # Weblogic environment
      FMW_ERROR=-1
      if [ "$DEBUGVAL" = "true" ]; then
         echo "Fusion Middleware Home maybe corrupted (WebLogic Home is not found)!"
         echo "OPatch will proceed only if JVM launcher found"
      fi 
   elif [ -x "${WL_HOME}/common/bin/commEnv.sh" ]; then
      . "${WL_HOME}/common/bin/commEnv.sh"
   else
      FMW_ERROR=-2
      if [ "$DEBUGVAL" = "true" ]; then
         echo "Fusion Middleware Home maybe corrupted (Common Env Script missing or Not executable)!"
         echo "OPatch will proceed only if JVM launcher found"
      fi
   fi

   # Reset the MW_HOME variable if set lest OPatch think otherwise
   MW_HOME=${SET_MW_HOME}

   # All verifications done now ...
   if [ "$OH" = "" ]; then
      OH=${C_ORACLE_HOME}
   fi
   if [ "$MWH" = "" ]; then
      MWH=${C_MW_HOME}
   fi

   #We will use the JDK used by WebLogic unless user knows better 
   #and wants to override
   if [ $FMW_ERROR -eq 0 ] &&[ "$JDK" = "" ] && [ "$JRE" = "" ]; then # no -jdk/jre specified
      JDK=${JAVA_HOME}
   fi
   JRE_MEMORY_OPTIONS="${MEM_ARGS} ${JVM_D64}"
   export JRE_MEMORY_OPTIONS
   case $JAVA_VENDOR in
     Oracle)
       JAVA_VM_OPTION=-client
     ;;
     HP)
       JAVA_VM_OPTION=-client
     ;;
     IBM)
       JAVA_VM_OPTION=
     ;;
     Sun)
       JAVA_VM_OPTION=-client
     ;;
     *)
       JAVA_VM_OPTION=
     ;;
   esac
else
   # Regular Oracle Home - We allow implicit setting of Oracle Home for OUI based homes
   if [ "$OH" = "" ]; then
      if [ ! -d "${C_ORACLE_HOME}/oui" ]  ; then
         echo The Oracle Home $C_ORACLE_HOME is not OUI based home. Please give proper Oracle Home.
         exit 1;
      fi
      OH=${C_ORACLE_HOME}
   fi
fi


CP=$OH/oui/jlib
# Use ORACLE_HOME to set Java CLASS_PATH
JAVA= # Default location

# Use JRE if supplied
if [ "$JAVA" = "" ] && [ "$JRE" != "" ]; then # from -jre
  JAVA=$JRE/bin/java
  JAVA_HOME=$JRE
fi


# Use JDK if supplied
if [ "$JAVA" = "" ] && [ "$JDK" != "" ]; then # now from -jdk
  JAVA=$JDK/bin/java
  JAVA_HOME=$JDK
fi


# use OH/jre/*, it should be 1.5 or above
if [ "$JAVA" = "" ] ; then
  JRE_HIGH=
  if [ -d $OH/jre ]; then
    for i in `ls -1 $OH/jre | sort` ;
    do
      JRE_HIGH=$i ;
    done
    if [ "$JRE_HIGH" != "" ]; then
      JRE_HIGH_FINAL=`echo $JRE_HIGH | awk '
                            BEGIN {FS="."}
                            {
                                if (($1 >= 1) && ($2 >= 5))
                                    print $0
                            }'`
      if [ "$JRE_HIGH_FINAL" != "" ] && [ -x $OH/jre/$JRE_HIGH_FINAL/bin/java ]; then
          JAVA=$OH/jre/$JRE_HIGH_FINAL/bin/java
          JAVA_HOME=$OH/jre/$JRE_HIGH_FINAL
      fi
    fi
  fi
fi

# Now Check for jdk location inside OH
if [ "$JAVA" = "" ] ; then  # Now look for jdk in OH
  JAVA=$OH/jdk/bin/java
  JAVA_HOME=$OH/jdk
fi


# Last option is to look inside oraparam.ini for JRE_LOCATION
if [ "$JAVA" = "" ] && [ -r $OH/oui/oraparam.ini ]; then
  JRE_LOCATION=`grep "JRE_LOCATION=" $OH/oui/oraparam.ini | sed 's/JRE_LOCATION=//'`
  ABS_PATH=`echo $JRE_LOCATION | grep "^\/"`
  if [ "$ABS_PATH" = "" ]; then # not absolute path
    JAVA=$OH/oui/bin/$JRE_LOCATION/bin/java
    JAVA_HOME=$OH/oui/bin/$JRE_LOCATION
  else
    JAVA=$JRE_LOCATION/bin/java
    JAVA_HOME=$JRE_LOCATION
  fi
fi


# Now check if Java executable exists and has execute permission, exit otherwise
# Because from this point on we cannot proceed without Java 
if [ "$JAVA" = "" -o ! -x "$JAVA" ]; then
   if [ $FMW_ERROR -eq -1 ]; then
      echo "Fusion Middleware Home is corrupted (WebLogic Home is not found)!"
   fi
   if [ $FMW_ERROR -eq -2 ]; then
      echo "Fusion Middleware Home is corrupted (Common Env Script missing or Not executable)!"
   fi
   if [ "$JAVA" = "" ]; then
      echo Java could not be located. OPatch cannot proceed!
      echo "OPatch returns with error code = 1"
      exit 1
   fi
   if [ ! -x "$JAVA" ]; then
      echo $JAVA is not a valid executable for this platform. OPatch cannot proceed!
      echo "OPatch returns with error code = 1"
      exit 1
   fi
fi

if  [ "$DEBUGVAL" = "true" ]; then
    _osArch=`$JAVA -cp $BASE/jlib/opatch.jar oracle.opatch.JavaSystemProp os.arch`
    _javaVMSpecVersion=`$JAVA -cp $BASE/jlib/opatch.jar oracle.opatch.JavaSystemProp java.vm.specification.version`
    _javaVMSpecVendor=`$JAVA -cp $BASE/jlib/opatch.jar oracle.opatch.JavaSystemProp java.vm.specification.vendor`
    _javaVMSpecName=`$JAVA -cp $BASE/jlib/opatch.jar oracle.opatch.JavaSystemProp java.vm.specification.name`
    _javaVMVendor=`$JAVA -cp $BASE/jlib/opatch.jar oracle.opatch.JavaSystemProp java.vm.vendor`
    _javaJRESpecVersion=`$JAVA -cp $BASE/jlib/opatch.jar oracle.opatch.JavaSystemProp java.specification.version`
    _javaJRESpecVendor=`$JAVA -cp $BASE/jlib/opatch.jar oracle.opatch.JavaSystemProp java.specification.vendor`
    _javaJRESpecName=`$JAVA -cp $BASE/jlib/opatch.jar oracle.opatch.JavaSystemProp java.specification.name`
    _javaSupportedClassVersion=`$JAVA -cp $BASE/jlib/opatch.jar oracle.opatch.JavaSystemProp java.class.version`
    if [ -x "$JAVA_HOME/bin/javap" ]; then 
      _javaCompiledClassMajorVersion=`$JAVA_HOME/bin/javap -verbose -classpath $BASE/jlib/opatch.jar oracle.opatch.JavaSystemProp |grep major|sed 's/^ *\(.*\) *$/\1/'`
      _javaCompiledClassMinorVersion=`$JAVA_HOME/bin/javap -verbose -classpath $BASE/jlib/opatch.jar oracle.opatch.JavaSystemProp|grep minor|sed 's/^ *\(.*\) *$/\1/'`
    fi
    _unameall=`uname -a`
    echo "Machine Info: $_unameall"
    echo "_osArch is $_osArch"
    echo "_javaVMSpecVersion is $_javaVMSpecVersion"
    echo "_javaVMSpecVendor is $_javaVMSpecVendor"
    echo "_javaVMSpecName is $_javaVMSpecName"
    echo "_javaVMVendor is $_javaVMVendor"
    echo "_javaJRESpecVersion is $_javaJRESpecVersion"
    echo "_javaJRESpecVendor is $_javaJRESpecVendor"
    echo "_javaJRESpecName is $_javaJRESpecName"
    echo "_javaSupportedClassVersion is $_javaSupportedClassVersion"
    echo "OPatch compiled with $_javaCompiledClassMajorVersion, $_javaCompiledClassMinorVersion"
fi

# set _bootClassPath for IBM platforms (bug 7038091 and 7114214)
_osArch=""
if [ -r $BASE/ocm/lib/emocmcommon.jar ] && [ -r $BASE/ocm/ocm.zip ]; then
   _osArch=`$JAVA -cp $BASE/ocm/lib/emocmcommon.jar OsInfo`
fi
_bootClassPath=""

if  [ "$DEBUGVAL" = "true" ]; then
    echo "_osArch (from OCM API) is $_osArch"
fi
if [ "${_osArch}" = "ppc" -o "${_osArch}" = "ppc64" -o "${_osArch}" = "s390"  -o "${_osArch}" = "s390x" ]; then
      _bootClassPath="-Xbootclasspath/a:${BASE}/ocm/lib/emocmclnt.jar:${BASE}/ocm/lib/emocmcommon.jar:${BASE}/ocm/lib/emocmclnt-14.jar:${BASE}/ocm/lib/osdt_core3.jar:${BASE}/ocm/lib/osdt_jce.jar:${BASE}/ocm/lib/http_client.jar:${BASE}/ocm/lib/regexp.jar:${BASE}/ocm/lib/jcert.jar:${BASE}/ocm/lib/jnet.jar:${BASE}/ocm/lib/jsse.jar:${BASE}/ocm/lib/log4j-core.jar:${BASE}/ocm/lib/xmlparserv2.jar"
      if  [ "$DEBUGVAL" = "true" ]; then
        echo "IBM platforms: set _bootClassPath to $_bootClassPath"
      fi
fi


# JRE_MEMORY_OPTIONS 
if [ -z "${JRE_MEMORY_OPTIONS}" ]; then
   JRE_MEMORY_OPTIONS=""
   if [ -r $OH/oui/oraparam.ini ]; then
      JRE_MEMORY_OPTIONS=`grep "JRE_MEMORY_OPTIONS=" $OH/oui/oraparam.ini | sed 's/JRE_MEMORY_OPTIONS=\"//' | sed 's/\"//'`
   fi
fi

# Remove double quotes globally - whatever u get
JRE_MEMORY_OPTIONS=`echo $JRE_MEMORY_OPTIONS | sed 's/\"//g'`


# Change the permission of OH/.patch_storage directory and set umask.
# umask 0007 
# Do not set umask.
patchStorage="$OH/.patch_storage"
chmodResult=0
if [ -d $patchStorage ]; then
   chmod o-rwx $patchStorage > /dev/null
   chmodResult=$?
fi

if [ $chmodResult != 0 ]; then
    echo "The chmod command failed for ".$patchStorage.". It is possible that you have no permission to access it. OPatch cannot proceed!"
    echo "OPatch returns with error code = 1"
    exit 1
fi
            
echo "========================================================"
echo "GENERIC OPATCH VERSION - FOR USE IN SAP ENVIRONMENT ONLY"
echo "========================================================"

if [ "$DEBUGVAL" = "true" ]; then
   echo "$JAVA $JAVA_VM_OPTION $JRE_MEMORY_OPTIONS -cp $BASE/ocm/lib/emocmclnt.jar:$CP/OraInstaller.jar:$CP/OraPrereq.jar:$CP/share.jar:$CP/srvm.jar:$CP/orai18n-mapping.jar:$CP/xmlparserv2.jar:$BASE/jlib/opatch.jar:$BASE/jlib/opatchutil.jar:$BASE/jlib/opatchprereq.jar:$BASE/jlib/opatchactions.jar:$BASE/jlib/opatchext.jar:$BASE/jlib/opatchfmw.jar:$WEBLOGIC_CLASSPATH -DOPatch.ORACLE_HOME=$OH -DOPatch.DEBUG=$DEBUGVAL -DOPatch.RUNNING_DIR=$BASE -DOPatch.MW_HOME=$MWH -DOPatch.WL_HOME=$WL_HOME -DOPatch.COMMON_COMPONENTS_HOME=$COMMON_COMPONENTS_HOME ${oracleOcmService} ${_bootClassPath} oracle/opatch/OPatch $@"
fi

$JAVA $JAVA_VM_OPTION $JRE_MEMORY_OPTIONS -cp $BASE/ocm/lib/emocmclnt.jar:$CP/OraInstaller.jar:$CP/OraPrereq.jar:$CP/share.jar:$CP/srvm.jar:$CP/orai18n-mapping.jar:$CP/xmlparserv2.jar:$BASE/jlib/opatch.jar:$BASE/jlib/opatchutil.jar:$BASE/jlib/opatchprereq.jar:$BASE/jlib/opatchactions.jar:$BASE/jlib/opatchext.jar:$BASE/jlib/opatchfmw.jar:$WEBLOGIC_CLASSPATH -DOPatch.ORACLE_HOME=$OH -DOPatch.DEBUG=$DEBUGVAL -DOPatch.RUNNING_DIR=$BASE -DOPatch.MW_HOME=$MWH -DOPatch.WL_HOME=$WL_HOME -DOPatch.COMMON_COMPONENTS_HOME=$COMMON_COMPONENTS_HOME ${oracleOcmService} ${_bootClassPath} oracle/opatch/OPatch $@

RESULT=$?

if [ $RESULT -ge  201 ] && [ $RESULT -le 203 ]; then
  echo
  echo "OPatch stopped on request."
  RESULT=0
elif [ $RESULT -ge 204 ] && [ $RESULT -le 210 ];then
  echo
  echo "OPatch completed with warnings.";
  RESULT=0
elif [ $RESULT != 0 ]; then
  echo
  echo "OPatch failed with error code $RESULT";
else
  echo 
  echo "OPatch succeeded."
fi

exit ${RESULT}
