#!/bin/sh

######################################################################
# Copyright (c) 2004, 2011, Oracle and/or its affiliates. All rights reserved. 
#
# skkurapa 10/01/11  set environment settings needed for middleware based OH
#
#####################################################################

BASE=`dirname $0`

if [ ! -z "${OH}" ]; then 
   
   if [ ! -z "${FMW_COMPONENT_HOME}" ]; then
      C_ORACLE_HOME=${FMW_COMPONENT_HOME}
   else
           if [ -x "${OH}/common/bin/setHomeDirs.sh" ] && [ -x "${OH}/common/bin/setWlstEnv.sh" ]; then
                   C_ORACLE_HOME=${OH}
                   FMW_COMPONENT_HOME=${OH}
           else
                   TEMP_MW_HOME=`cd ${OH}/.. ; pwd`
                   for SUB_DIR in $TEMP_MW_HOME/*
                   do
                           if [ -x "${SUB_DIR}/common/bin/setHomeDirs.sh" ] && [ -x "${SUB_DIR}/common/bin/setWlstEnv.sh" ]; then
                                   FMW_COMPONENT_HOME=$SUB_DIR
                                   C_ORACLE_HOME=$SUB_DIR
                           fi
                   done
                   if [ -z "${FMW_COMPONENT_HOME}" ]; then
                           if [ "$DEBUGVAL" = "true" ]; then
                                   echo OPatch was not able to set FMW_COMPONENT_HOME by itself
                           fi
                           C_ORACLE_HOME=${OH}
                   fi
          fi

   fi
   # Calculate Middleware Home simply by moving up IFF User set Oracle Home
   C_MW_HOME=`cd ${C_ORACLE_HOME}/.. ; pwd`
fi

export C_ORACLE_HOME
export C_MW_HOME

# 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 "OPatch was not able to set weblogic home. The Fusion Middleware home seems to be corrupted"
         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
   TEMP_ORACLE_HOME=${ORACLE_HOME}
   ORACLE_HOME=${C_ORACLE_HOME}
   if [ -x "${C_ORACLE_HOME}/common/bin/setWlstEnv.sh" ]; then
      . "${C_ORACLE_HOME}/common/bin/setWlstEnv.sh" > /dev/null
   fi
   CURRENT_HOME=${COMMON_COMPONENTS_HOME}
   if [ -x "${COMMON_COMPONENTS_HOME}/common/bin/setWlstEnvExtns.sh" ]; then
      . "${COMMON_COMPONENTS_HOME}/common/bin/setWlstEnvExtns.sh"
   fi
   CURRENT_HOME=
   ORACLE_HOME=${TEMP_ORACLE_HOME}
   # Reset the MW_HOME variable if set lest OPatch think otherwise
   MW_HOME=${SET_MW_HOME}

   export ORACLE_HOME
   export MW_HOME

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

   export OH
   export MWH
   export FMW_ERROR
   #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}
      export JDK
   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
   export JAVA_VM_OPTION

   # check if this is a FA Oracle Home and set required environment
   . "${BASE}/scripts/opatch_fa"    
fi
