#!/usr/bin/ksh
#
# Copyright 2003 Sun Microsystems, Inc. All Rights Reserved.
#
# preconfig -- ESM Capacity Reporter platform component preconfig
#
###

# Define the paths used by this script
SCRIPT=$0
SCRNAME=`basename $SCRIPT`
COMPONENT_DIR=`dirname $0`

# Include function libraries
. $ESM_BASE/lib/esm_lib.ksh
. $ESM_BASE/lib/esm_globals.ksh
. $ESM_BASE/lib/configvars_lib.ksh

. $ESM_BASE/component/cr_common/bin/component_combo_check_lib.ksh

. $COMPONENT_DIR/component_lib.ksh


if [ "$DEBUG" = "1" ]; then
    el_debug
    cl_debug
    el_secho 1 $ESM_LOG n "Local Environment Variables:"
    el_secho 1 $ESM_LOG n "    COMPONENT_DIR |$COMPONENT_DIR|"
    el_secho 1 $ESM_LOG n "    SCRIPT        |$SCRIPT|"
    el_secho 1 $ESM_LOG n "    SCRNAME       |$SCRNAME|"
fi

fatal ()
{
    if [ -n "$*" ] ; then
	el_log 1 "\nERROR: $*\n"
    fi
    exit 1
}

checkforfatal ()
{
    if [ $? -ne 0 ] ; then
        fatal "$*"
    fi
}


#------------------------------------------------------------------------------
# Initialize the values of certain platform configvars based on the local host
# environment.
#

# Ensure compatibility of CR components on this system
#
is_invalid_cluster_installation &&
    el_assert "Invalid Capacity Reporter installation configuration."

# SERVICE_LOCATION defaults to the local hostname
#
el_configvar_set SERVICE_LOCATION "`hostname`"  ||
    fatal "Failure to initialize all platform configvars"

# Ensure the ORACLE_HOME environment variable is set
#
if [ -z "$ORACLE_HOME" ]; then
    fatal "The environment variable ORACLE_HOME is not set."
fi

# Ensure ORACLE_HOME points to a real Oracle directory
# by testing for one of the critical Oracle utilities.
#
_ck_cmd=$ORACLE_HOME/bin/tnsping

if [ ! -x "$_ck_cmd" ]; then
    fatal "Program $_ck_cmd is not executable.\n"\
          "Perhaps ORACLE_HOME is not set properly?"
fi

# ORACLE_HOME appears to be set properly, so now ensure
# that LD_LIBRARY_PATH is set correctly by running one
# of the critical Oracle utilities to see if it loads.
#
_ck_cmd=$ORACLE_HOME/bin/sqlplus

$_ck_cmd >/dev/null 2>&1 </dev/null
checkforfatal "Program $_ck_cmd does not execute properly.\n"\
              "Perhaps LD_LIBRARY_PATH is not set properly for Oracle?"

# Ensure no standard properties files exist in the ESM installation directory
# by renaming any existing versions found.
#
# WARNING: the paths to properties files are currently HARD-CODED.
# While this should work just fine for ESM 2.0 (since relocation is not
# yet supported), this test/remove block needs to be updated to dynamically
# reference the proper ESM_BASE as it pertains to the final installation
# (and not the cdimage setting for that variable).
#
# This next code section is ONLY applicable to the Installation context,
# so quietly exit with success if the ESM context is anything other than
# "install".
#
if [ "$ESM_CONTEXT" = 'install' ] ; then

    _installed_esm_config=/opt/SUNWstm/etc

    for pfile in esm.properties esm_sensitive.properties
    do
	pfile2=$_installed_esm_config/$pfile

	if [ -f $pfile2 ] ; then
	    el_log  1 "Preserving the existing $pfile file..."
	    el_slog 1 "Renaming $pfile2 to ${pfile2}.old"

	    /usr/bin/mv $pfile2 ${pfile2}.old ||
		el_assert "Failure to rename the existing $pfile2 file"
	fi
    done
fi

exit 0
