#!/usr/bin/ksh
#
# Copyright 2003 Sun Microsystems, Inc. All Rights Reserved.
#
# preconfig -- ESM Capacity Reporter cr_agent component preconfig
#
# An Agent host is considered to be exactly ONE of these types:
#         Stealth - Agent bits are installed, but not registered with the platform
#     SimpleAsset - Stealth, plus registration with the platform
#   ClusterMember - SimpleAsset, plus base cluster configuration
#     ClusterRoot - ClusterMember, plus cluster root configuration
###

# This script is ONLY applicable to the Installation context,
# so quietly exit with success if the ESM context is anything
# other than "install".
#
[ "$ESM_CONTEXT" = 'install' ] || exit 0

# 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 "$*" ; fi
    exit 1
}

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


#------------------------------------------------------------------------------
# Check to see if the local version of Solaris is one of the unsupported
# versions, and if so, have the user confirm the installation.
#
__solminver=`SolarisMinorVersion`

if [ "$__solminver" -lt 8 ] ; then
    el_slog 1 "Unsupported version of Solaris detected: $__solminver"
    el_slog 1 "Confirming unsupported installation..."
    /usr/bin/cat << END_HERE
**********************************************************
WARNING!  This system is running Solaris 2.${__solminver}

Installation and use of ESM 2.0 Capacity Reporter agents
is NOT supported on Solaris releases older than 2.8.
**********************************************************
END_HERE
    echo
    el_isEsmTrue `/usr/bin/ckyorn -Q -d no -p "Continue with the installation"`
    if [ $? -ne 0 ] ; then
	el_assert "Installation aborted by user request."
    fi
fi

#------------------------------------------------------------------------------
# If the installation does NOT include the Management Station (ie, Agent-only),
# then change certain configvars from being optional to being required for
# prompting by the user.  Note that the detection of the Management Station is
# keyed off the existence of the "platform" component name in the InstallTypes
# configvar.
#
el_configvar_attr_contains InstallTypes value platform
if [ $? -ne 0 ] ; then
    #
    # Set SERVICE_LOCATION (ie, Management Station hostname) to a nullified
    # value/default, and force the user to enter a value during Configuration.
    #
    el_configvar_set              SERVICE_LOCATION ''
    el_configvar_setattr          SERVICE_LOCATION default ''
    el_configvar_attr_remove_word SERVICE_LOCATION gates   InstallOptional
fi

#------------------------------------------------------------------------------
# Detect whether this host is in a cluster and set all related configvars,
# accordingly.
#
# CLUSTER_TYPE is set to a known, valid cluster type identifier if this host
# is in a cluster; otherwise, it is set to null to indicate the host is not
# in any kind of supported cluster.
#
el_slog 1 "Detecting cluster configuration..."

cluster_setEnv   # Detect and setup the cluster environment

if [ -z "$CLUSTER_TYPE" ]
then
    # This host is not in a cluster, so disable all cluster-related configvars
    #
    el_slog 1 "Host is NOT a member of a supported cluster"

    el_configvar_set     ENABLE_CLUSTER no
    el_configvar_setattr ENABLE_CLUSTER promptorder ''
else
    # This host is part of a cluster, so enable cluster-related configvars
    #
    el_slog 1 "Host is a member of a '$CLUSTER_TYPE' cluster"

    el_configvar_set ENABLE_CLUSTER yes

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

    # If this host is the root of the cluster, then setup all root-related
    # configvars.
    #
    IS_CLUSTER_ROOT=`cluster_isRoot`
    checkforfatal "Failure to resolve critical cluster information"

    el_slog 1 "Is this host the cluster root?  $IS_CLUSTER_ROOT"

    el_configvar_set IS_CLUSTER_ROOT $IS_CLUSTER_ROOT

    if el_isEsmTrue $IS_CLUSTER_ROOT ; then
        # Resolve the cluster name and virtual host (aka access path).
        #
        # For the virtual host, we just use the first one in the resolved
        # list; if a different one is desired, the user will have to enter
        # it during the Configuration phase.
        #
        # Here we ignore fails to obtain the information; the user will
        # just have to manually enter it during the Configuration phase.
        #
	el_configvar_set CLUSTER_NAME "`cluster_getName`"
	el_configvar_set CLUSTER_VIRTUAL_SERVER "`cluster_getVirtualServers | cut -d' ' -f1`"
    else
	# This host is in the cluster, but is NOT the root, so disable
	# the root-only cluster configvars from being prompted.
	#
	# NOTE: this is NOT the best way to achieve this goal; instead,
	# a new boolean configvar named IS_CLUSTER_ROOT should be created
	# (with NO prompt order, to be invisible to the user) that is
	# initialized accordingly, and the following cluster-related configvars
	# should include IS_CLUSTER_ROOT in their "gates" attribute.
	#
	# This is a cheezy, get-em-out-by-Friday approach to...er...get the
	# this configuration done by Friday.   ...jay
	#
	el_configvar_setattr CLUSTER_NAME           promptorder ''
	el_configvar_setattr CLUSTER_VIRTUAL_SERVER promptorder ''
    fi
fi

exit 0
