#!/bin/sh
# $Header: emll/lib/getdbinfo /main/4 2009/11/26 23:05:14 aghanti Exp $
#
# getdbinfo
#
# Copyright (c) 2005, 2009, Oracle and/or its affiliates. All rights reserved. 
#
#    NAME
#      getdbinfo - Wrapper for PERL workhorse of getdbinfo.pl
#
#    DESCRIPTION
#      This script is invoked by the user to install the software
#      which is the OCM collector. It also configures it for a
# 

setHostName()
{
  CCR_HOST_NAME=`$HOSTNAME`

  export CCR_HOST_NAME
}

setOCMDirPaths()
{
  setHostName

  # 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
      # 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
    fi
  else
    CCR_CONFIG_HOME=${OCM_HOME}
  fi

  export CCR_CONFIG_HOME
}

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

# 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.
DIRNAME=${_usrBinDir}/dirname
ECHO=${_binDir}/echo
HOSTNAME=${_binDir}/hostname
SED=${_binDir}/sed
TR=${_usrBinDir}/tr

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

AWK=${_binDir}/awk
PLATFORM=`$UNAME | $CUT -f1`
if [ "$PLATFORM" = "SunOS" ]
then
    if [ -f ${_binDir}/nawk ]
    then
        AWK=${_binDir}/nawk
    elif [ -f ${_usrBinDir}/nawk ]
    then
        AWK=${_usrBinDir}/nawk
    fi
fi

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

#
# Determine the absolute path for the OCM location.
#
OCM_LIB=`$DIRNAME $0 | $TR -s '/'`
# First remove './'s in the beginning
OCM_LIB=`$ECHO $OCM_LIB | $AWK '{ if (match($0,/^(\.\/)*/)){ sub(/^(\.\/)*/,"",$0) }; print $0; }'`
# Next replace all '/./'s in between with '/'
OCM_LIB=`$ECHO $OCM_LIB | $AWK '{ if (match($0,/\/(\.\/)+/)){ gsub(/\/(\.\/)+/,"/",$0) }; print $0;}'`
# Lastly remove '/.' at the end
OCM_LIB=`$ECHO $OCM_LIB | $AWK '{ if (match($0,/\/\.$/)){ sub(/\/\.$/,"",$0) }; print $0; }'`
# Now convert to absolute path
OCM_LIB=`$ECHO $OCM_LIB | $AWK -f ${OCM_LIB}/../bin/strip_path.awk PWD=$PWD`
OCM_HOME=`$DIRNAME $OCM_LIB`

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

setOCMDirPaths

# 
# Set constants for the execution of perl
#
PERL5LIB=${OCM_HOME}/engines/${PLATFORM}/perl/lib:${OCM_HOME}/engines/${PLATFORM}/perl/lib/site_perl
PERL5LIB=${OCM_HOME}/sysman/admin/scripts:${PERL5LIB}

if [ "${PLATFORM}" = "HP-UX" ]
then
    SHLIB_PATH=${OCM_HOME}/engines/${PLATFORM}/perl/lib:${OCM_HOME}/engines/${PLATFORM}/perl/lib/5.8.3
    export SHLIB_PATH
elif [ "${PLATFORM}" = "AIX" ]
then
    LIBPATH=${OCM_HOME}/engines/${PLATFORM}/perl/lib:${OCM_HOME}/engines/${PLATFORM}/perl/lib/5.8.3
    export LIBPATH
else
    LD_LIBRARY_PATH=${OCM_HOME}/engines/${PLATFORM}/perl/lib:${OCM_HOME}/engines/${PLATFORM}/perl/lib/5.8.3
    export LD_LIBRARY_PATH
fi

export OCM_HOME
export PERL5LIB

# 
# If the perl interpretter exists try to execute the perl script.
#
if [ -f ${OCM_HOME}/engines/${PLATFORM}/perl/bin/perl ]
then
    ${OCM_HOME}/engines/${PLATFORM}/perl/bin/perl ${OCM_LIB}/getdbinfo.pl "$@"
    exit $?
else
    exit 0
fi
