#!/bin/sh
#
# $Header: emll/bin/emSnapshotEnv /main/25 2012/06/14 11:40:58 fmorshed Exp $
#
# emSnapshotEnv
#
# Copyright (c) 2005, 2012, Oracle and/or its affiliates. All rights reserved. 
#
#    NAME
#      emSnapshotEnv - captures the environmental variables
#
#    DESCRIPTION
#      Captures the environmental variables required by daemon
#      tasks. Allows for re-capture
#
#    NOTES
#      Used by emCCR and other scripts to insure a consistent configuration
#      picture
#
#    MODIFIED   (MM/DD/YY)
#    proxy       07/01/10 - bug_fix_9861559 Adding ORACLE_INSTANCE env variable
#    ckalivar    11/11/09 - Snapshot env variable ORACLE_CCR_TESTS
#    aghanti     10/08/09 - Removal of dependency on xpg4 utilities
#    jsutton     11/24/08 - BEAHOME should be BEA_HOME
#    jsutton     11/21/08 - Add WLS env requireds
#    jsutton     03/26/08 - iAS R1 discovery/collection issues with EBS
#                           environment
#    ndutko      03/11/08 - Snapshot the ORACLE_OCM_SERVICE environmental
#                           variable
#    ndutko      10/17/07 - XbranchMerge ndutko_bug-6486004_1 from
#                           st_emll_10.2.7
#    ndutko      10/17/07 - Define the SED constant
#    ndutko      10/15/07 - XbranchMerge ndutko_bug-6486004 from st_emll_10.2.7
#    ndutko      10/15/07 - Set the CCR_CONFIG_HOME environmental variable (bug
#                           6486004)
#    kgupta      07/17/07 - Support shared Oracle homes
#    ndutko      12/12/06 - Export the saved environmental values
#    cvaishna    12/11/06 - Fixing PWD issue
#    ndutko      09/13/06 - Addition of EBS INSTANCE ORACLE_CONFIG_HOME
#    ndutko      03/07/06 - Conditional definition of UNAME 
#    ndutko      02/09/06 - Add support for USER_ORACLE_HOME being passed to 
#                           discovery and collections. USER_ORACLE_HOME is the 
#                           value set by the user when the installation was 
#                           performed. 
#    ndutko      01/25/06 - Save the JAVA_HOME_CCR as a precidence order for 
#                           JAVA location before JAVA_HOME 
#    ndutko      12/14/05 - setLocale to English 
#    ndutko      12/08/05 - Persist the TZ env variable if set 
#    ndutko      11/30/05 - Allow for conditional location of GREP for Solaris 
#    ndutko      10/18/05 - Allow for alternate locations of ENV 
#    ndutko      10/13/05 - ndutko_bug-4672792
#    ndutko      10/13/05 - Creation
#

# Defines the hostname for the location of the default writeable state sub
# directory
setHostName()
{
  CCR_HOST_NAME=`${HOSTNAME}`
  export CCR_HOST_NAME
}

# Defines the CCR_CONFIG_HOME if not defined to where the full configuation
# writeable tree will reside.
setOCMDirPaths()
{
  # Use what's in the environment if already set
  if [ -z "${CCR_CONFIG_HOME}" ]
  then
    # Check whether its a shared OCM install
    if [ ${G_sharedHomesSupported} -eq 0 ]
    then
      if [ -z "${ORACLE_CONFIG_HOME}" ]
      then
        CCR_CONFIG_HOME="${CCR_HOME}/hosts/${CCR_HOST_NAME}"
      elif [ -d ${ORACLE_CONFIG_HOME} ]
      then
        # strip any trailing dirsep
        ORACLE_CONFIG_HOME=`$ECHO ${ORACLE_CONFIG_HOME} | $SED -e 's/\/*$//'`
        CCR_CONFIG_HOME="${ORACLE_CONFIG_HOME}/ccr"
      else
        $ECHO '$ORACLE_CONFIG_HOME should point to the directory containing OCM configurations for the host.'
        exit $ERR_PREREQ_FAILURE
      fi
    else
      CCR_CONFIG_HOME=${CCR_HOME}
    fi
  fi

  export CCR_CONFIG_HOME
}

#
# Reset the locale to ENGLISH for command line operations.
LC_ALL=C
export LC_ALL

# Constant declarations for exit values.
SUCCESS=0
ERR_PREREQ_FAILURE=1

# Define the paths for /usr/bin. These are used in subsequent path
# specifications for native commands.
_binDir=/bin
_usrBinDir=/usr/bin

# Set the variables that map to explicit paths to prevent from trojan
# horse type attacks and allow for more consistent installation experience
# by eliminating the use of aliases.
AWK=${_binDir}/awk
DIRNAME=${_usrBinDir}/dirname
ECHO=${_binDir}/echo
HOSTNAME=${_binDir}/hostname
SED=${_binDir}/sed
TR=${_usrBinDir}/tr

if [ -f ${_binDir}/egrep ]
then
    EGREP=${_binDir}/egrep
elif [ -f ${_usrBinDir}/egrep ]
then
    EGREP=${_usrBinDir}/egrep
fi

if [ -f ${_binDir}/uname ]
then
    UNAME=${_binDir}/uname
elif [ -f ${_usrBinDir}/uname ]
then
    UNAME=${_usrBinDir}/uname
fi

if [ -f ${_binDir}/cut ]
then
    CUT=${_binDir}/cut
elif [ -f ${_usrBinDir}/cut ]
then
    CUT=${_usrBinDir}/cut
fi

if [ -f ${_binDir}/env ]
then
    ENV=${_binDir}/env
elif [ -f ${_usrBinDir}/env ]
then
    ENV=${_usrBinDir}/env
fi

ROOTCHAR=`$DIRNAME $0 | $CUT -b1`
if [ "$ROOTCHAR" != "/" -a -z "$PWD" ]
then
    $ECHO "The current shell is not supported for executing commands using relative paths."
    $ECHO "Either execute the command under the bash or ksh shells, or execute the "
    $ECHO "command using the absolute path."
    exit 1
fi

#
# Determine the absolute path for the CCR location.
#
CCR_BIN=`$DIRNAME $0 | $TR -s '/'`
CCR_BIN=`$ECHO $CCR_BIN | $AWK -f ${CCR_BIN}/strip_path.awk PWD=$PWD`
CCR_HOME=`$DIRNAME $CCR_BIN`

#
# Determine if Shared Oracle Home support is available. This is determined
# thru the new directory 'hosts' under the CCR_HOME.
#
if [ -d "${CCR_HOME}/hosts" ]; then
    G_sharedHomesSupported=0
else
    G_sharedHomesSupported=1
fi

# Construct the paths
setHostName
setOCMDirPaths

# Vars
# NOTE: These vars are also defined in deriveCCR!!!
VARS="CRS_HOME|CLUSTER_NAME|LD_PRELOAD|JAVA_HOME_CCR|JAVA_HOME|TNS_ADMIN|ORAINST_LOC|EMTAB|EMAGENT_PERL_TRACE_LEVEL|TZ|ORACLE_HOME|ORACLE_CONFIG_HOME|ORACLE_OCM_SERVICE|IAS_CONFIG_HOME|WL_HOME|BEA_HOME|ORACLE_INSTANCE|ORACLE_CCR_TESTS"

$ENV | $EGREP "^($VARS)=" > ${CCR_CONFIG_HOME}/config/emCCRenv
$ENV | $EGREP "^($VARS)=" | $CUT -f1 -d'=' | $AWK '{print "export ",$1}' >> \
                              ${CCR_CONFIG_HOME}/config/emCCRenv

exit 0
