#!/bin/sh
#++
# Copyright (c) 2005, 2009, Oracle and/or its affiliates. All rights reserved. 
#    
#    NAME
#      emocmrsp - OCM Install Config Response Generator
# 
#    DESCRIPTION
#      This script is invoked by the user to create the response
#      file for silent installations of OCM.
#
#    Note
#      From a design/convention point, variables local to functions are
#      prefixed with a '_' character. Global Variables are prefixed with
#      'G_' and are entirely in upper case (The exception to this rule
#      are environment variables that are meant to abstract OS Commands
#      e.g. SED, CUT, ...)
#
#      Functions are listed in alphabetical order and precede the flow
#      of logic.
#    
#    EXIT CODES
#      0 - Success
#      1 - Prerequisite failure
#      2 - Invalid argument specified
#      3 - Invalid Usage
#      7 - Missing command qualifier value
#      11 - Unexpected Installation failure

#
# 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
ERR_INVALID_ARG=2
ERR_INVALID_USAGE=3
MISSING_QUALIFIER_VALUE=7
ERR_UNEXPECTED_FAILURE=11

# Define the paths for /usr/bin. These are used in subsequent path
# specifications for native commands.
_usrLocalBinDir=/usr/local/bin
_usrBinDir=/usr/bin
_binDir=/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.
DIRNAME=${_usrBinDir}/dirname
ECHO=${_binDir}/echo
# Define PWDD for pwd as PWD is a built variable on some platforms.
PWDD=${_binDir}/pwd
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

PLATFORM=`$UNAME | $CUT -f1`

if [ $PLATFORM = "OSF1" ]
then
  if [ -z "${BIN_SH}" ]
  then
    BIN_SH=xpg4
    export BIN_SH
    $0 "$@"
    exit $?
  fi
fi

#+
# Check to insure that the prerequisites for running the configuration utility
# are set and valid.
#--
checkPrerequisites()
{
    _originalShellFlags=$-
    set +e

    if [ ! -z "${JAVA_HOME_CCR}" ]
    then
        _JAVA_HOME=${JAVA_HOME_CCR}
        _JAVA_BIN=${JAVA_HOME_CCR}/bin
        _javaLoc="The JAVA_HOME_CCR environment variable"
    elif [ ! -z "${JAVA_HOME}" ]
        then
            _JAVA_HOME=$JAVA_HOME
            _JAVA_BIN=${JAVA_HOME}/bin
            _javaLoc="JAVA_HOME"
        else
            _CCR_PARENT=`$DIRNAME ${OCM_HOME}`

            # If not in the grandparent of the directory containing the
            # emocmrsp script, then try the great grandparent. This is 
            # in support of opatch distribution which pushes the 
            # emocmrsp utility down a directory lower.
            if [ ! -f ${_CCR_PARENT}/jdk/bin/java ]; then
                _CCR_PARENT=`$DIRNAME ${_CCR_PARENT}`
            fi
            _JAVA_HOME=${_CCR_PARENT}/jdk
            _JAVA_BIN=${_JAVA_HOME}/bin
            _javaLoc="The ORACLE_HOME"
    fi

    # find jre home, if jdk home is not found
    if [ ! -f ${_JAVA_BIN}/java ]
    then
        _CCR_PARENT=`$DIRNAME ${OCM_HOME}`
        if [ ! -f ${_CCR_PARENT}/jre/bin/java ]
        then
          _CCR_PARENT=`$DIRNAME ${_CCR_PARENT}`
        fi
        _JAVA_HOME=${_CCR_PARENT}/jre
        _JAVA_BIN=${_JAVA_HOME}/bin
    fi

    if [ ! -f ${_JAVA_BIN}/java ]
    then
        $ECHO "${_javaLoc} does not contain java."
        $ECHO "${_javaLoc} does not contain a valid JDK/JRE."
        reportInvalidJDKError
        exit $ERR_PREREQ_FAILURE
    fi

    java_version=`${_JAVA_BIN}/java -version 2>&1`
    cmd_exit=$?
    java_version=`$ECHO $java_version | \
                  $EGREP 'java version' | $CUT -d'"' -f2 | $CUT -d'"' -f1`
    java_major_version=`$ECHO $java_version | $CUT -d'.' -f1`
    java_minor_version=`$ECHO $java_version | $CUT -d'.' -f2`

    if [ $cmd_exit != 0 -o \
         -z "${java_major_version}" -o \
         -z "${java_minor_version}" ]
    then
        $ECHO "${_javaLoc} does not contain a valid JDK/JRE."
        reportInvalidJDKError
        exit $ERR_PREREQ_FAILURE
    fi

    if [ "$java_major_version" -ne "1" -o \
         "$java_minor_version" -eq "1" ]
    then
        $ECHO "Java Version $java_version is less than minimum required (1.2.2)."
        $ECHO "${_javaLoc} does not contain a valid JDK/JRE."
        reportInvalidJDKError
        exit $ERR_PREREQ_FAILURE
    fi

    JAVA_HOME=${_JAVA_HOME}
    export JAVA_HOME

    # Reset original shell flags only if there were any
    if [ ! -z "${_originalShellFlags}" ]
    then
        set ${_originalShellFlags}
    fi
}

# Called from checkPrerequisite, this function performs conditional logic
# on the type of message to return with corrective action
reportInvalidJDKError()
{   
    # If this is a prereq check in a complete install, refer to
    # emSnapshotEnv, otherwise just give the error and terminate.
    if [ -f ${OCM_HOME}/bin/emCCR ]
    then
        $ECHO "Redefine JAVA_HOME to refer to a JDK/JRE 1.2.2 or greater and invoke "
        $ECHO "  $ORACLE_HOME/ccr/bin/emSnapshotEnv."
    else
        $ECHO "Redefine JAVA_HOME to refer to a JDK/JRE 1.2.2 or greater."
    fi
}

setHostName()
{
  CCR_HOST_NAME=`hostname`

  export CCR_HOST_NAME
}

setOCMDirPaths()
{
  # Use what's in the environment if already set
  if [ -z "${CCR_CONFIG_HOME}" ]
  then
    # Check whether its a shared OCM install
    if [ ${_sharedHomesSupported} -eq 0 ]
    then
      if [ -z "${ORACLE_CONFIG_HOME}" ]
      then
        CCR_CONFIG_HOME="${OCM_HOME}/hosts/${CCR_HOST_NAME}"
      elif [ -d ${ORACLE_CONFIG_HOME} ]
      then
        _isRelative=`$ECHO ${ORACLE_CONFIG_HOME} | $EGREP -c '^/'`
        if [ ${_isRelative} -eq 0 ] ; then
          $ECHO '$ORACLE_CONFIG_HOME refers to a relative path.'
          $ECHO 'Redefine ORACLE_CONFIG_HOME to refer to an absolute path, or'
          $ECHO 'unset it if the configuration state is in the ccr directory tree.'
          exit $ERR_PREREQ_FAILURE
        else
          # strip any trailing dirsep
          ORACLE_CONFIG_HOME=`$ECHO ${ORACLE_CONFIG_HOME} | $SED -e 's/\/*$//'`
          CCR_CONFIG_HOME="${ORACLE_CONFIG_HOME}/ccr"
        fi
      fi
    else
      CCR_CONFIG_HOME=${OCM_HOME}
    fi
  fi

  export CCR_CONFIG_HOME
}

# Construct the CCR installation directory root based upon the bin
# directory being a child.
# Extract the binary directory specification where this script resides. 
# The enclosed code will come up with an absolute path. 
_OCMBinDir=`$DIRNAME $0 | $TR -s '/'`
_savedDir=`$PWDD`
cd ${_OCMBinDir}
_OCMBinDir=`$PWDD`
cd ${_savedDir}

OCM_HOME=`$DIRNAME ${_OCMBinDir}`
export OCM_HOME

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

# OCM is expanded into a home, so the the parent of the install *IS* the
# Oracle home
ORACLE_HOME=`$DIRNAME ${OCM_HOME}`
export ORACLE_HOME

# If ORACLE_CONFIG_HOME is definded, use it.
_OCH=""
if [ ! -z "${ORACLE_CONFIG_HOME}" ]; then
    _OCH="-DORACLE_CONFIG_HOME=${ORACLE_CONFIG_HOME}"
fi

if [ ! -z "${ORACLE_CCR_DEV}" ]; then
  setHostName
  setOCMDirPaths
fi

#
# Main function processing now
#
checkPrerequisites

# determine OS architecture - IBM JVM needs special consideration for SSL
# per bug 5894282
_bootClassPath=""
_osArch=`${JAVA_HOME}/bin/java -cp ${OCM_HOME}/lib/emocmcommon.jar OsInfo`
if [ "${_osArch}" = "ppc" -o \
     "${_osArch}" = "ppc64" -o \
     "${_osArch}" = "s390" -o \
     "${_osArch}" = "s390x" ]; then
    _bootClassPath="-Xbootclasspath/a:${OCM_HOME}/lib/emocmclnt.jar:${OCM_HOME}/lib/emocmcommon.jar:${OCM_HOME}/lib/emocmclnt-14.jar:${OCM_HOME}/lib/osdt_core3.jar:${OCM_HOME}/lib/osdt_jce.jar:${OCM_HOME}/lib/http_client.jar:${OCM_HOME}/lib/regexp.jar:${OCM_HOME}/lib/jcert.jar:${OCM_HOME}/lib/jnet.jar:${OCM_HOME}/lib/jsse.jar:${OCM_HOME}/lib/log4j-core.jar:${OCM_HOME}/lib/xmlparserv2.jar"
fi

_ocmEndpoint=""
if [ ! -z "${ORACLE_OCM_SERVICE}" ]; then
    _ocmEndpoint="-Docm.endpoint=${ORACLE_OCM_SERVICE}"
fi

_ocmLog=""
if [ ! -z "${CCR_DEBUG}" ]; then
    _ocmLog="-DOCM_LOG_LEVEL=DEBUG"
fi


#
# Test-related environmental
#
_oracleCCRTests=""
if [ ! -z "${ORACLE_CCR_TESTS}" ]
then
   _oracleCCRTests="-DORACLE_CCR_TESTS=${ORACLE_CCR_TESTS}"
fi


# Use the OH as CCR_CONFIG_HOME to force the use of config parameters strictly
# from the binary home.
exec $JAVA_HOME/bin/java ${_bootClassPath} ${_ocmEndpoint} ${_ocmLog} ${_oracleCCRTests} -DOCM_ROOT=${OCM_HOME} -DORACLE_HOME=$ORACLE_HOME -DCCR_CONFIG_HOME=${CCR_CONFIG_HOME} ${_OCH} -jar ${OCM_HOME}/lib/emocmclnt.jar "$@"
