#! /usr/bin/sh
#
# ident "@(#)es-inst 1.160     03/01/14 SMI"
#
# Copyright 2002-2003 Sun Microsystems, Inc.  All rights reserved.
# Use is subject to license terms.
#


#
#This part of the code does copy of scripts to tmp location
#and then calls itself from there.
#


firstargument="$0"
scriptdirname=`/usr/bin/dirname $firstargument` 
prev_dir=`/usr/bin/pwd`
cd $scriptdirname
l_dir=`/usr/bin/pwd`

check_for_l10n() {

    L10N_REQUIRED=0
    if [ -n "$LANG" ] && [ "$LANG" != "C" ]; then
        if [ "$LANG" != "zh_TW" ]; then
            L10N_CODE=`/usr/bin/expr "$LANG" : '\(..\).*'`
            LC_MES_CODE=`/usr/bin/expr "$LC_MESSAGES" : '\(..\).*'`
            if [ "$LC_MES_CODE" = "$L10N_CODE" ]; then
              LC_MESSAGES=$LANG
              export LC_MESSAGES
            fi
        else
            L10N_CODE=$LANG
        fi
        [ "$L10N_CODE" != "en" ] && L10N_REQUIRED=1
    else
        L10N_CODE=C
    fi
}

check_if_called_from_installed_place ()
{
    echo "$l_dir" | /usr/bin/grep "SUNWsymon/sbin"  > /dev/null 2>&1
    if [ $? -eq 0 ] ; then
        return 0
    fi
    return 1
}

#
#This function returns 0 if being called from disk image
#else, it returns 1.
#

check_if_called_from_disk_image ()
{
    if [ -d "../../disk1/image" ] ; then
        return 0
    fi 
    return 1
}

#
#This function returns 0 if the users shell dir is not
#in the cdrom path. Else it returns 1.
#

check_if_userdirisin_cdromfilesystem ()
{
    mountpoint=`/usr/bin/dirname $l_dir`
    echo "$prev_dir" | /usr/bin/grep "$mountpoint" > /dev/null 2>&1
    if [ $? -eq 0 ] ; then
        return 1
    fi
    return 0
}

COPY_FLAG=0
check_if_called_from_disk_image 
if [ $? -ne 0 ] ; then
    check_if_called_from_installed_place 
    if [ $? -ne 0 ] ; then
        #
        #Assume that it is being called from CD
        #
        if [ -d "../image" ] ; then
            COPY_FLAG=1
            check_if_userdirisin_cdromfilesystem
            if [ $? -eq 0 ] ; then
                EXEC_FLAG=1
            else
                EXEC_FLAG=0
            fi
        fi
    fi
fi

#
#Here if COPY_FLAG = 1 then this script is called from
#CD. If EXEC_FLAG = 1 then it needs to exec cmdtool else
#it does not need to exec cmdtool.
#If COPY_FLAG = 0 then we dont need to set TEXTDOAMIN or
#TEXETDOMAINDIR at this time. But if COPY_FLAG = 1 then
#we need to set the TEXTDOMAIN and TEXTDOMAINDIR for the
#following messages related to copy of scripts to tmp dir.
#One thing to note here is if we exec es-inst, it carries
#all other env variables but not LANG. So we will have to
#pass LANG somehow.
#


prog_dirname=`/usr/bin/dirname $0`
if [ $COPY_FLAG -eq 0 ] ; then
    cd $prev_dir
else
    OPTION=""
    SRC_DIR=""
    SRC_OPTION_FOUND=0
    while getopts vR:S:T:C:A:a OPT
    do
	case $OPT in
	    R)
		OPTION="$OPTION -R $OPTARG"
		;;
	    S)  SRC_DIR="$OPTARG"
		OPTION="$OPTION -S $SRC_DIR"
		SRC_OPTION_FOUND=1
		;;
	    T)
		OPTION="$OPTION -T $OPTARG"
		;;
	    C)
		OPTION="$OPTION -C $OPTARG"
		;;
	    A)
		OPTION="$OPTION -A $OPTARG"
		;;
	    a)
		OPTION="$OPTION -a"
		;;
	    v)
		OPTION="$OPTION -v"
		;;
	esac
    done
    #
    #Put the LANG value also in the option
    #
    if [ -z "$LANG" ] ; then
        LANG=C
    fi

    OPTION="$OPTION -L $LANG"
    CD_SBIN_DIR="$l_dir"
    cd $CD_SBIN_DIR
    if [ $SRC_OPTION_FOUND -eq 0 ] ; then
	cd ..
	#POSSIBLE_SRC_DIR=`/usr/bin/pwd`/image
        SRC_DIR=`/usr/bin/pwd`/image
	cd $CD_SBIN_DIR
	if [ -d "$SRC_DIR" ] ; then
	    OPTION="$OPTION -S $SRC_DIR"
	fi
    fi
 
    #
    #Setup TEXTDOMAIN and TEXTDOMAINDIR here. Check if the directory
    #$SRC_DIR/PE/$OSVERS/locale exists. If yes then set TEXTDOMAINDIR
    #else leave for english.
    #

    check_for_l10n
    if [ $L10N_REQUIRED -eq 1 ] ; then
	OSVERS=`/usr/bin/uname -r`
	case $OSVERS in
	    5.5.1*) OSVERS=2.5.1 ;;
	    5.6*)   OSVERS=2.6   ;;
	    5.7*)   OSVERS=7     ;;
	    5.8*)   OSVERS=8     ;;
	    5.9*)   OSVERS=9     ;;
	    *)      echolog ''
		echolog 'Unsupported OS version: $2' "$OSVERS"
		exit 1           ;;
	esac
	if [ -d "$SRC_DIR/PE/Solaris_$OSVERS/locale" ] ; then
	    TEXTDOMAINDIR="$SRC_DIR/PE/Solaris_$OSVERS/locale"
	    TEXTDOMAIN="ES_SCRIPTS"
	    export TEXTDOMAINDIR
	    export TEXTDOMAIN
	fi
    fi

    ME=`/usr/ucb/whoami`
    if [ "$ME" != "root" ] ; then
	/usr/bin/gettext "You need to be root before running this program.\n"
	exit 1
    fi

    if [ -d /tmp/SunMC30Install ] ; then

	 /usr/bin/rm -Rf /tmp/SunMC30Install
	 if [ $? -ne 0 ] ; then
	     /usr/bin/gettext "Can not remove the directory /tmp/SunMC30Install\n"
	     exit 1
	 fi
    fi

    /usr/bin/mkdir -p /tmp/SunMC30Install/sbin
    if [ $? -ne 0 ] ; then
	/usr/bin/gettext "Can not create the directory /tmp/SunMC30Install/sbin\n"
	exit 1
    fi
    /usr/bin/cp -Rf $CD_SBIN_DIR/* /tmp/SunMC30Install/sbin/
    if [ $? -ne 0 ] ; then
	/usr/bin/gettext "Can not copy the required files to /tmp/SunMC30Install\n"
	exit 1
    fi
    cd /tmp/SunMC30Install/sbin
    if [ $EXEC_FLAG -eq 1 ] ; then
        commandline=`echo "exec /tmp/SunMC30Install/sbin/es-inst $OPTION"`
        $commandline
    else
        if [ ! -x /usr/openwin/bin/xhost ] ; then
            /usr/bin/gettext "Can not open the display. Either X server is not allowing\n"
            /usr/bin/gettext "the connection or you are running it from console login.\n"
            /usr/bin/gettext "Please read Sun Management Center installation Readme and\n"
            /usr/bin/gettext "run installation as mentioned.\n"
            exit 0
        fi
        /usr/openwin/bin/xhost > /dev/null 2>&1
        if [ $? -eq 0 ] ; then
            if [ ! -x /usr/openwin/bin/cmdtool ] ; then
                if [ ! -x /usr/dt/bin/dtterm ] ; then
                    /usr/bin/gettext "Can not find /usr/openwin/bin/cmdtool or /usr/dt/bin/dtterm."
                    /usr/bin/gettext "Please run Sun Management Center installation by using the"
                    /usr/bin/gettext "complete path after changing directory to /."
                    exit 0
                else
	            commandline=`echo "/usr/dt/bin/dtterm -e /tmp/SunMC30Install/sbin/es-inst $OPTION"`
                fi
            else
	        commandline=`echo "/usr/openwin/bin/cmdtool  /tmp/SunMC30Install/sbin/es-inst $OPTION"`
            fi
	    $commandline &
        else
            /usr/bin/gettext "Can not open the display. Either X server is not allowing\n"
            /usr/bin/gettext "the connection or you are running it from console login.\n"
            /usr/bin/gettext "Please read Sun Management Center installation Readme and\n"
            /usr/bin/gettext "run installation as mentioned.\n"
        fi
    fi
    exit 0

fi



smcdb_check_memory_available() {
    if [ "$RELOCATED_ROOT" = "" -o "$RELOCATED_ROOT" = "/" ] ; then
        #
        # get the phisical memory size of this machine
        #
        MEMSIZE=`/usr/sbin/prtconf | /usr/bin/grep "Memory size: " | /usr/bin/cut -f3 -d' '`

        #
        # verify the memory requirement
        #
        if [ "$MEMSIZE" -lt "256" ]; then
            echolog "A minimum of 256 MB RAM is required to install Sun Management Center server."
            echolog 'Current system has $2 MB RAM.' "$MEMSIZE"
            echolog "Can not continue to install Sun Management Center server."
            /usr/bin/sleep 5
            exit 1
        fi

        MEMSIZE=`/usr/bin/expr $MEMSIZE \* 1024 \* 1024 / 2`
    else
        echolog "A minimum of 256 MB RAM is required to run Sun Management Center server."
        echolog "Before doing setup, please make sure you have 256 MB RAM."
    fi
}


continue_confirm() {
    [ "$AUTOCONFIG_RUNFILE" != "" ] && return 
    $CMD_ECHO "    \c"
    ask_user '    Do you want to proceed?'
    echolog ''
    if [ $answer -eq 0 ]; then
	quit
    fi
}

continue_confirmv() {
    if [ $VERBOSE = "ON" ]; then
      $CMD_ECHO "    \c"
      ask_user '    Do you want to proceed?'
      echolog ''
      if [ $answer -eq 0 ]; then
  	quit
      fi
    else
      answer=1
    fi
}

# Exit the program
quit() {
    trap 2
    echolog ''
    echolog 'Exiting Sun Management Center installation.'
    echolog ''
    cleanUp
    $CMD_SLEEP 2
    exit
}

# Adds to the summary
# $1 the message
addSummary() {
    if [ "$INSTALLATION_SUMMARY" != "" ]; then
	INSTALLATION_SUMMARY="${INSTALLATION_SUMMARY}\n${1}"
    else
	INSTALLATION_SUMMARY="$1"
    fi
}

# Get the internationalized value of a key.
# Parameter: $1 = the key
# Return: $MSG
getI18NMessage() {
    MSG=""
    if [ -f "$INSTALLATION_PROPERTY" ] ; then
	MSG=`$CMD_AWK "/^\s*$1=/" $INSTALLATION_PROPERTY | $CMD_SED -e "s/^\s*$1=//"`
    fi
    [ -z "$MSG" ] && MSG="$1"
    return
}

# Gets the product name.
# Return: $PRODUCT_KEY
getProductKey() {
    PRODUCT_KEY=`$CMD_GREP -e "PRODUCT_KEY=" $INSTALLATION_INFO | $CMD_GREP -v "\s*#" | $CMD_SED -e "s/PRODUCT_KEY=//"`
    PRODUCT_KEY=`$CMD_ECHO $PRODUCT_KEY | $CMD_SED -e "s/^\s*//"`
}

getAskQuestion() {
    ASK_QUESTION=""
    ASK_QUESTION=`$CMD_GREP -e "ASK_QUESTION=" $INSTALLATION_INFO | $CMD_GREP -v "\s*#" | $CMD_SED -e "s/ASK_QUESTION=//"`
    ASK_QUESTION=`$CMD_ECHO $ASK_QUESTION | $CMD_SED -e "s/^\s*//"`
    ASK_QUESTION=`$CMD_ECHO $ASK_QUESTION | $CMD_SED -e "s/\s*$//"`
}

# Gets the subdirectory where a pack is located.
# $1 - pack key
# Return: $PACK_SUBDIR
getPackSubdir() {
    LINENUM_START=`$CMD_GREP -n -e "PACK_KEY=$1" "$INSTALLATION_INFO" | $CMD_GREP -v "\s*#" | $CMD_SED -e "s/:.*//"`
    if [ -z "$LINENUM_START" ]; then
	echolog 'Error: Cannot find pack key $2.' "$1"
	quit
    fi
    LINENUM_END=`$CMD_SED -n -e "${LINENUM_START},${EOL}p" "$INSTALLATION_INFO" | $CMD_GREP -n -e "PACK_SUBDIR" | $CMD_SED -e "s/:.*//"`
    LINENUM_END=`$CMD_ECHO $LINENUM_END | $CMD_SED -e "s/ .*//"`
    LINENUM=`$CMD_EXPR $LINENUM_START + $LINENUM_END - 1`
    PACK_SUBDIR=`$CMD_SED -n -e "${LINENUM},${LINENUM}p" "$INSTALLATION_INFO" | $CMD_SED -e "s/\s*PACK_SUBDIR=//"`
    PACK_SUBDIR=`$CMD_ECHO $PACK_SUBDIR | $CMD_SED -e "s/^\s*//"`
}

# Check the license for a pack, if applicable
#License will be asked only on server machine
# $1 - pack key
checkLicense() {

    #
    #The if block below is for agent only installation. This will be used
    #when user has used our script to create agent only product from our 
    #CD and wants to install it. User will run es-inst -a to set AGENT_ONLY=1
    #

    if [ $AGENT_ONLY -eq 1 ] ; then
        return
    fi
    $CMD_PKGINFO $RELOC_PARAM -qi SUNWessdk > /dev/null 2>&1
    if [ $? -eq 0 ] ; then
        return
    fi
    $CMD_PKGINFO $RELOC_PARAM -qi SUNWessrv > /dev/null 2>&1
    if [ $? -ne 0 ] ; then
        return
    fi
    LINENUM_START=`$CMD_GREP -n -e "PACK_KEY=$1" $INSTALLATION_INFO | $CMD_GREP -v "\s*#" | $CMD_SED -e "s/:.*//"`
    if [ -z "$LINENUM_START" ]; then
	echolog 'Error: Cannot find pack key $2.' "$1"
	quit
    fi
    LINENUM_END=`$CMD_SED -n -e "${LINENUM_START},${EOL}p" $INSTALLATION_INFO | $CMD_GREP -n -e "PACK_LICENSE" | $CMD_SED -e "s/:.*//"`
    [ "$LINENUM_END" = "" ] && return
    LINENUM_END=`$CMD_ECHO $LINENUM_END | $CMD_SED -e "s/ .*//"`
    LINENUM=`$CMD_EXPR $LINENUM_START + $LINENUM_END - 1`
    tmp=`$CMD_SED -n -e "${LINENUM},${LINENUM}p" $INSTALLATION_INFO | $CMD_SED -e "s/\s*PACK_LICENSE=//"`
    tmp=`$CMD_ECHO $tmp | $CMD_SED -e "s/^\s*//"`
    [ "$tmp" = "" ] && return 1

    tmp_license_key=`$CMD_ECHO $tmp | $CMD_CUT -d: -f1`
    tmp_license_question=`$CMD_ECHO $tmp | $CMD_CUT -d: -f2`
    getI18NMessage "$tmp_license_question"

    get_license "$MSG"
}
get_license () {
    QUESTION_TEXT="$1"
    get_input_from_user "$QUESTION_TEXT"
    if [ ! -z "$answer" ] ; then
	echo "$answer" | /usr/bin/cat >> $VARDIR/.license
    fi
}

# Select the layers
# Return: $INSTALL_LAYERS
selectLayers() {
    INSTALL_LAYERS=""

    #
    #The if block below is for agent only installation. This will be used
    #when user has used our script to create agent only product from our 
    #CD and wants to install it. User will run es-inst -a to set AGENT_ONLY=1
    #

    if [ $AGENT_ONLY -eq 1 ] ; then
        INSTALL_LAYERS="LAYER.AGENT"
        return
    fi
    if [ "$INSTALL_TYPE" = "ADDON" -o "$INSTALL_TYPE" = "ADDON_ONLY" ]; then
	packageExists "SUNWesmod" # Agent layer
	if [ $? -eq 1 ]; then
	    if [ "$INSTALL_LAYERS" = "" ]; then
		INSTALL_LAYERS="LAYER.AGENT"
	    else
		INSTALL_LAYERS="${INSTALL_LAYERS},LAYER.AGENT"
	    fi
	fi

	packageExists "SUNWescon" # Console layer
	if [ $? -eq 1 ]; then
	    if [ "$INSTALL_LAYERS" = "" ]; then
		INSTALL_LAYERS="LAYER.CONSOLE"
	    else
		INSTALL_LAYERS="${INSTALL_LAYERS},LAYER.CONSOLE"
	    fi
	fi

	packageExists "SUNWessrv" # Server layer
	if [ $? -eq 1 ]; then
	    if [ "$INSTALL_LAYERS" = "" ]; then
		INSTALL_LAYERS="LAYER.SERVER"
	    else
		INSTALL_LAYERS="${INSTALL_LAYERS},LAYER.SERVER"
	    fi
	fi
    else
        if [ "$OSVERS" = "2.5.1" ] ; then
	    LAYER_LIST="LAYER.AGENT"
            selectable_layers=1
        else
	    LAYER_LIST="LAYER.SERVER LAYER.AGENT LAYER.CONSOLE"
            selectable_layers=3
        fi
	if [ "$INSTALL_TYPE" != "DE" ]; then
	    echolog 'There are $2 layers for your selection:' "$selectable_layers"
	fi

	for i in $LAYER_LIST
	do
	    if [ "$INSTALL_TYPE" != "DE" ]; then
		answer=0
		getI18NMessage $i
		if [ "$AUTOCONFIG_RUNFILE" != "" ] ; then
		    tmp_auto=`getAutoConfig "CORE_LAYERS"`
		    tmp_auto=`$CMD_ECHO $tmp_auto | $CMD_GREP $i`
		    [ "$tmp_auto" = "" ] && continue
		    $CMD_ECHO "  \c"
		    echolog '  Selecting layer: $2' "$MSG"
		    answer=1
		else
		    $CMD_ECHO "  \c"
		    ask_user '  Do you want to install components in layer: $2?' "$MSG"
		fi
		
		if [ $answer -eq 1 ]; then
		    [ "$i" = "LAYER.SERVER" -o "$i" = "LAYER.CONSOLE" ] && INSTALL_JAVA=1

		    if [ -z "$INSTALL_LAYERS" ]; then
			INSTALL_LAYERS="$i"
		    else
			INSTALL_LAYERS="${INSTALL_LAYERS},$i"
		    fi
		fi
	    else
		[ "$i" = "LAYER.SERVER" -o "$i" = "LAYER.CONSOLE" ] && INSTALL_JAVA=1

		if [ "$INSTALL_LAYERS" = "" ]; then
		    INSTALL_LAYERS="$i"
		else
		    INSTALL_LAYERS="${INSTALL_LAYERS},$i"
		fi
	    fi
	done
	if [ "$AUTOCONFIG_SAVEFILE" != "" ] ; then # Save automated config
	    saveAutoConfig "CORE_LAYERS" "$INSTALL_LAYERS"
	fi
        #
        #If selected layers contain server then check for RAM requirement.
        #
        echo "$INSTALL_LAYERS" | /usr/bin/grep "LAYER.SERVER" > /dev/null 2>&1
        if [ $? -eq 0 ] ; then
	    smcdb_check_memory_available
        fi
    fi

    if [ "$INSTALL_LAYERS" = "" ]; then
	echolog ''
	if [ "$INSTALL_TYPE" != "DE" ]; then
	    echolog 'No layers were selected for installation.'
	    echolog ''
	    #[ "$AUTOCONFIG_RUNFILE" != "" ] && quit # No layers for autoconfig
	    continue_confirm
	else
	    echolog 'No layers were found for installation.'
	    echolog ''
	    quit
	fi
    fi
}

# Get a list of pack keys.
# Return: $PACK_LIST
getPackList() {
    PACK_LIST=""
    LINES=`$CMD_AWK "/PACK_KEY=/" $INSTALLATION_INFO | $CMD_GREP -v "\s*#"`
    for line in $LINES
    do
	RESULT=`$CMD_ECHO $line | $CMD_SED -e "s/^\s*PACK_KEY=//"`
	if [ -n "$RESULT" ]; then
	    if [ -n "$PACK_LIST" ]; then
		PACK_LIST=$PACK_LIST,$RESULT
	    else
		PACK_LIST=$RESULT
	    fi
	fi
    done

    if [ -z "$PACK_LIST" ]; then
	echolog 'No packs are defined in the installation configuration file.'
	echolog ''
	quit
    fi
}

selectPacks() {
    getPackList

    # Filter out non-installable addon packs
    if [ "$INSTALL_TYPE" = "ADDON" ]; then
	tmp_pack_list="$PACK_LIST"
	PACK_LIST=""
	for pack_key in `$CMD_ECHO $tmp_pack_list | $CMD_AWK -F ',' '{ for (i = 1; i <= NF; i++) print $i }'`
	do
	    tmp_component_ok=0
	    getComponentList "$pack_key"
	    for component_key in `$CMD_ECHO "$COMPONENT_LIST" | $CMD_AWK -F ',' '{ for (i = 1; i <= NF; i++) print $i }'`
	    do
		getComponentInfo "$pack_key" "$component_key" "PACKAGES" "$LOCALE_CODE"
		tmp_package_list="$COMPONENT_INFO"
		for pkg in `$CMD_ECHO "$tmp_package_list" | $CMD_AWK -F ',' '{ for (i = 1; i <= NF; i++) print $i }'`
		do
		    getPackageDirectory "$pack_key" "$pkg"
		    if [ $? -eq 1 ]; then
			isAddonPackageInstallable "${PACKAGE_DIR}/pkginfo"
			if [ $? -eq 1 ]; then
			    tmp_component_ok=1
			    break
			fi
		    fi
		done
		[ $tmp_component_ok -eq 1 ] && break
	    done

	    # Add installable addon pack to PACK_LIST
	    if [ $tmp_component_ok -eq 1 ]; then
		if [ "$PACK_LIST" = "" ]; then
		    PACK_LIST="$pack_key"
		else
		    PACK_LIST="${PACK_LIST},$pack_key"
		fi
	    fi
	done
    fi

    tmp_comma=`$CMD_ECHO $PACK_LIST | $CMD_GREP ","`
    INSTALL_PACKS=""
    if [ "$INSTALL_TYPE" != "DE" -a "$tmp_comma" != "" ]; then
	echolog ''
	echolog 'Please select the packs to install:'
    fi

    for i in `$CMD_ECHO "$PACK_LIST" | $CMD_AWK -F ',' '{ for (i = 1; i <= NF; i++) print $i }'`
    do
	if [ "$INSTALL_TYPE" = "DE" -o "$tmp_comma" = "" ]; then
	    checkLicense "$i"
	    if [ "$INSTALL_PACKS" != "" ]; then
		INSTALL_PACKS="${INSTALL_PACKS},$i"
	    else
		INSTALL_PACKS="$i"
	    fi
	else
	    answer=0
	    getI18NMessage $i
	    if [ "$AUTOCONFIG_RUNFILE" != "" ] ; then # Run autoconfig
		tmp_auto=`getAutoConfig "CORE_PACKS"`
		tmp_auto=`$CMD_ECHO $tmp_auto | $CMD_GREP $i`
		[ "$tmp_auto" = "" ] && continue
		$CMD_ECHO "  \c"
		echolog '  $2 will be installed.' "$MSG"
		answer=1
	    else
		$CMD_ECHO "  \c"
		ask_user '  Do you want to install $2?' "$MSG"
	    fi
	    
	    if [ $answer -eq 1 ]; then
		checkLicense "$i"
		if [ -n "$INSTALL_PACKS" ]; then
		    INSTALL_PACKS="${INSTALL_PACKS},$i"
		else
		    INSTALL_PACKS="$i"
		fi
	    fi
	fi
    done

    if [ "$INSTALL_PACKS" = "" ]; then
	echolog ''
	echolog 'No packs were selected for installation.'
	echolog ''
	[ "$AUTOCONFIG_RUNFILE" != "" ] && quit # No packs for autoconfig
	continue_confirm
    else
	if [ "$AUTOCONFIG_SAVEFILE" != "" ] ; then # Save autoconfig
	    saveAutoConfig "CORE_PACKS" "$INSTALL_PACKS"
	fi
    fi
}

# Get the list of component keys.
# Parameter: $1 = a pack key; $2 = <a comma-delimited list of layer keys>
#            If $2 is not specified, all layers will be assumed
# Return: $COMPONENT_LIST
getComponentList() {
    COMPONENT_LIST=""
    # Find pack
    setLineRange "PACK_KEY=$1" "</PACK>"
    LIST=`$CMD_SED -n -e "${LINENUM_START},${LINENUM_END}p" $INSTALLATION_INFO | $CMD_GREP -v "\s*#" | $CMD_GREP -e "COMPONENT_KEY"`
    for line in $LIST
    do
	line=`$CMD_ECHO $line | $CMD_SED -e "s/COMPONENT_KEY=//"`
	if [ -z "$2" ]; then
	    EXISTS=`$CMD_ECHO $COMPONENT_LIST | $CMD_GREP -e "$line"`
	    if [ -z "$EXISTS" ]; then
		if [ -z "$COMPONENT_LIST" ]; then
		    COMPONENT_LIST="$line"
		else
		    COMPONENT_LIST="${COMPONENT_LIST},$line"
		fi
	    fi
	else
	    LAYERS=`$CMD_ECHO $2 | $CMD_AWK -F ',' '{ for (i = 1; i <= NF; i++) print $i; print "\n" }'`
	    for layer in $LAYERS
	    do
		EXISTS=`$CMD_ECHO $COMPONENT_LIST | $CMD_GREP -e "$line"`
		if [ -z "$EXISTS" ]; then
		    getComponentInfo $1 $line LAYER
		    FIT_LAYERS=`$CMD_ECHO $COMPONENT_INFO | $CMD_GREP $layer`
		    if [ -n "$FIT_LAYERS" ]; then
			if [ -z "$COMPONENT_LIST" ]; then
			    COMPONENT_LIST="$line"
			else
			    COMPONENT_LIST="${COMPONENT_LIST},$line"
			fi
		    fi
		fi
	    done
	fi
    done
}

# Get a particular info of a component in a pack.
# Parameters: $1 = a pack key; $2 = a component key; $3 = a component info key; $4 = locale code (optional)
# Return: $COMPONENT_INFO
getComponentInfo() {
    # Find pack
    setLineRange "PACK_KEY=$1" "</PACK>"

    # Find component
    LINENUM1=`$CMD_SED -n -e "${LINENUM_START},${LINENUM_END}p" $INSTALLATION_INFO | $CMD_GREP -n -e "COMPONENT_KEY=$2" | $CMD_SED -e "s/:.*//"`
    LINENUM1=`$CMD_ECHO $LINENUM1 | $CMD_SED -e "s/.+\n.*//"`
    if [ -z "$LINENUM1" ]; then
	echolog 'Error: Cannot find component key $2.' "$2"
	quit
    fi
    LINENUM_START=`$CMD_EXPR $LINENUM1 + $LINENUM_START - 1`
    LINENUM_END=`$CMD_SED -n -e "${LINENUM_START},${EOL}p" $INSTALLATION_INFO | $CMD_GREP -n -e "</COMPONENT>" | $CMD_SED -e "s/:.*//"`
    LINENUM_END=`$CMD_ECHO $LINENUM_END | $CMD_SED -e "s/ .*//"`
    LINENUM_END=`$CMD_EXPR $LINENUM_START + $LINENUM_END - 1`

    # Find info
    COMPONENT_INFO=`$CMD_SED -n -e "${LINENUM_START},${LINENUM_END}p" $INSTALLATION_INFO | $CMD_GREP -v "\s*#" | $CMD_GREP -e "$3=" | $CMD_SED -e "s/${3}=//"`
    COMPONENT_INFO=`$CMD_ECHO $COMPONENT_INFO | $CMD_SED -e "s/^\s*//"`
    if [ -n "$4" ]; then
	PATTERN='${LOCALE_CODE}'
        if [ "$3" = "PACKAGES" ] ; then
            case "$4" in
		zh_TW) HELP_CODE=h  ;;
		zh)    HELP_CODE=c  ;;
		C)     HELP_CODE=en ;;
		*)     HELP_CODE="$4" ;;
            esac
        fi
	COMPONENT_INFO=`$CMD_ECHO $COMPONENT_INFO | $CMD_SED -e "s/$PATTERN/$HELP_CODE/"`
	#if [ "$3" = "PACKAGES" -a "$4" = "C" ]; then
	#    COMPONENT_INFO=`$CMD_ECHO $COMPONENT_INFO | $CMD_SED -e "s/$PATTERN/c/"`
	#else
	#    COMPONENT_INFO=`$CMD_ECHO $COMPONENT_INFO | $CMD_SED -e "s/$PATTERN/$4/"`
	#fi
    fi
}

# Set LINENUM_START and LINENUM_END for internal use
# Parameters: $1 = start pattern; $2 = end pattern
setLineRange() {
    LINENUM_START=`$CMD_GREP -n -e "$1" $INSTALLATION_INFO | $CMD_SED -e "s/:.*//"`
    if [ -z "$LINENUM_START" ]; then
	echolog 'Error: Cannot find key $2.' "$1"
	quit
    fi
    LINENUM_END=`$CMD_SED -n -e "${LINENUM_START},${EOL}p" $INSTALLATION_INFO | $CMD_GREP -n -e "$2" | $CMD_SED -e "s/:.*//"`
    LINENUM_END=`$CMD_ECHO $LINENUM_END | $CMD_SED -e "s/ .*//"`
    LINENUM_END=`$CMD_EXPR $LINENUM_START + $LINENUM_END - 1`
}

# Gets the package version
# $1 = absolute package directory
# Returns: $PACKAGE_VERSION
getPackageVersion() {
    PACKAGE_VERSION=""
    if [ ! -d "$1" ] || [ ! -f "$1/pkginfo" ]; then
	PACKAGE_VERSION="??"
	return
    fi

    PACKAGE_VERSION=`$CMD_PKGPARAM -f "$1/pkginfo" SUNW_PRODVERS`
}

# Checks whether a package exists on the host
# $1 - package name
# $2 - pack key (optional)
# Return: 0 = does not exist; 1 = exist and versions matched; 2 = exist and versions not matched
packageExists() {
    $CMD_PKGINFO $RELOC_PARAM -qi "$1"
    if [ $? -ne 0 ] ; then
	return 0
    else # Exists
	[ "$2" = "" ] && return 1

	# Check package version
	SADM_PKG=`getRelocPathName "/var/sadm/pkg"`
	getPackageVersion "$SADM_PKG/$1"
	ipkg="$PACKAGE_VERSION"
	getPackageDirectory "$2" "$1"
	[ $? -eq 0 ] && return 0
	getPackageVersion "$PACKAGE_DIR"
	[ "$ipkg" = "$PACKAGE_VERSION" ] && return 1 # Version matched
	return 2
    fi
}

# Checks if the packages have already been installed.
# $1 - pack key
# $2 - packages list
# Return: 0 for false; 1 if versions matched; 2 if versions not matched
packagesExist() {
    INSTALLED_PKGS=""
    MATCH_VERSION=1
    for pkgname in `$CMD_ECHO "$2" | $CMD_AWK -F ',' '{ for (i = 1; i <= NF; i++) print $i }'`
    do
	# Check for existence
	packageExists "$pkgname" "$1"
	ret="$?"
	[ $ret -eq 0 ] && return 0
	[ $ret -eq 2 ] && MATCH_VERSION=0
	if [ "$INSTALLED_PKGS" = "" ]; then
	    INSTALLED_PKGS="$pkgname"
	else
	    INSTALLED_PKGS="${INSTALLED_PKGS}, $pkgname"
	fi
    done

    if [ $MATCH_VERSION -eq 1 ]; then
	return 1
    else
	return 2
    fi
}

# Check if the dependent component was installed
# $1 the pack key that contains the component
# $2 dependent component list with the format: pack:component,pack:component
checkComponentDependencies() {
    DEPENDENT_COMPONENTS=""
    if [ -n "$2" ]; then
        for item in `$CMD_ECHO "$2" | $CMD_AWK -F ',' '{ for (i1 = 1; i1 <= NF; i1++) print $i1 }'`
        do
            pack_key=`$CMD_ECHO "$item" | $CMD_AWK -F ':' '{ print $1 }'`
            component_key=`$CMD_ECHO "$item" | $CMD_AWK -F ':' '{ print $2 }'`
	    getI18NMessage "$component_key"
	    getComponentInfo "$pack_key" "$component_key" "PACKAGES" "$LOCALE_CODE"
	    if [ $? -eq 0 -a "$COMPONENT_INFO" != "" ]; then
		packagesExist "$1" "$COMPONENT_INFO"
		if [ $? -eq 1 ]; then # Package exists
		    $CMD_ECHO "    \c"
		    echolog '    Component $2 has already been installed.' "$MSG"
		    addSummary "    Component $MSG has already been installed."
		    $CMD_ECHO "    \c"
		    echolog '    The corresponding packages are: $2' "$INSTALLED_PKGS"
		else # Not exist or versions not matched
		    EXISTS=`$CMD_ECHO $INSTALL_COMPONENTS | $CMD_GREP ${pack_key}:${component_key}`
		    if [ -z "$EXISTS" ]; then
			addComponent "$pack_key" "$component_key"
			getI18NMessage "$pack_key"
			PACK_VALUE="$MSG"
			getI18NMessage "$component_key"
			if [ -n "$DEPENDENT_COMPONENTS" ]; then
			    DEPENDENT_COMPONENTS="${DEPENDENT_COMPONENTS}, ${PACK_VALUE}:$MSG"
			else
			    DEPENDENT_COMPONENTS="${PACK_VALUE}:$MSG"
			fi
		    fi
		fi
            fi
	done
    fi
    if [ -n "$DEPENDENT_COMPONENTS" ]; then
        return 1
    fi
    return 0
}

# Checks the validity of the source directory
# $1 - the source directory
# $2 - if exist, indicates DE
# Returns: 1 if valid; 0 otherwise
checkSourceDirectory() {
    DIR=""
    if [ "$1" != "" ]; then # Source dir exists
	INSTALL_TYPE="UNKNOWN"
	tmp_src_backup="$SRC_DIR"
	SRC_DIR="$1"
	getCorrectDirectory
	if [ $? -eq 0 ]; then
	    $CMD_ECHO "  \c"
	    echolog '  Invalid source directory.'
	    echolog ''
            return 0
	else
	    DIR="$CORRECT_DIR"
	fi
	SRC_DIR="$tmp_src_backup"
	return 1
    fi

    INSTALL_TYPE="ERROR"
    return 0
}

# Gets the source directory.
getSourceDirectory() {
    addSummary "Date: `$CMD_DATE`"
    temp=0
    if [ "$AUTOCONFIG_RUNFILE" != "" ] ; then # Run automated installation
	answer=`getAutoConfig "CORE_SOURCE"`
	SRC_DIR="$answer"
	checkSourceDirectory "$answer"
	temp=$?
    else # No automated installation
	checkSourceDirectory "$SRC_DIR"
	if [ $? -eq 1 ]; then # Valid input source directory
	    temp=1
	else
	    if [ -d "../image" ]; then # Default directory exists
		SRC_DIR=`getPathName ../image`
		checkSourceDirectory "$SRC_DIR"
		temp=$?
	    fi
	fi
    fi

    if [ $temp -eq 0 ] ; then # No valid source directory
	while get_input_from_user 'Please enter the source directory:'
	do
	    [ "$answer" = "" ] && continue
	    temp_dir=`getPathName $answer`
	    SRC_DIR="$temp_dir"
	    checkSourceDirectory "$temp_dir"
	    if [ $? -eq 1 ] ; then
		if [ "$AUTOCONFIG_SAVEFILE" != "" ] ; then # Save config
		    saveAutoConfig "CORE_SOURCE" "$SRC_DIR"
		fi
		break
	    fi
	done
    else
	if [ "$AUTOCONFIG_SAVEFILE" != "" ] ; then # Save config
	    saveAutoConfig "CORE_SOURCE" "$SRC_DIR"
	fi
	echolog 'Source directory: $2' "$SRC_DIR"
	return
    fi
}

# Checks whether SunMC 2.1 exists
# Return: 1 if exists; 0 otherwise
oldSunMCExists() {
    $CMD_PKGINFO -qi $RELOC_PARAM SUNWescom
    [ $? -ne 0 ] && return 0

    PKGVER=`$CMD_PKGPARAM $RELOC_PARAM SUNWescom VERSION`
    tmp_pkgversion=`$CMD_EXPR "$PKGVER" : '\(...\).*'`
    [ "$tmp_pkgversion" = "3.0" ] && return 0
    return 1
}

# Checks whether SunMC 2.1 data only exists
# Return: 1 if exists; 0 otherwise
oldSunMCDataOnlyExists() {

 ret_code=0

 # Do data-only migration only if the current installation is not 3.0
 # This additional check is required as you can incrementally do ADD-ON
 # installations on top of an existing 3.0 installation.

 $CMD_PKGINFO -qi $RELOC_PARAM SUNWescom
 if [ $? -eq 0 ] ; then
     PKGVER=`$CMD_PKGPARAM $RELOC_PARAM SUNWescom VERSION`
     tmp_pkgversion=`$CMD_EXPR "$PKGVER" : '\(...\).*'`
     if [ "$tmp_pkgversion" = "3.0" ] ; then
         return $ret_code
     fi
 fi

 if [ "$RELOCATED_ROOT" != "" ]
 then
    tmp_var1="$RELOCATED_ROOT${VAROPTDIR}/db"
    tmp_var2="$RELOCATED_ROOT${VAROPTDIR}/cfg"
 else
    tmp_var1="${VAROPTDIR}/db"
    tmp_var2="${VAROPTDIR}/cfg"
 fi

 #
 # both the raima data directory and the old 2.1 cfg
 # directory must exist in order to return true
 # If both subdirs exists, then we need to do export
 #
 if [ -d $tmp_var1 -a -d $tmp_var2 ]
 then
   ret_code=1
 fi

 #
 # If only the cfg subdir exists, then we still need to
 # copy over the persistent dat files
 #
 if [ -d $tmp_var2 ]
 then
   cfg_cnt=`/usr/bin/ls -1 $tmp_var2|/usr/bin/wc -l`
   if [ $cfg_cnt -gt 0 ] ; then
       ret_code=1
   fi
 fi

 return $ret_code

}

# Checks whether a product exists
# $1 - "PE" | "DE"
# Return: 1 if exists; 0 otherwise
productExists() {
    $CMD_PKGINFO -qi $RELOC_PARAM SUNWescom
    [ $? -ne 0 ] && return 0

    if [ "$1" = "DE" ]; then # Check for existence of DE
	ps_system="SYSTEM=DEVELOPMENT"
    elif [ "$1" = "PE" ]; then # Check for existence of PE
	ps_system="SYSTEM=PRODUCTION"
    else
	echolog ''
	echolog 'Invalid parameter for productExists\\\(\\\): $2' "$1"
	quit
    fi

    ps_basedir=`$CMD_PKGPARAM $RELOC_PARAM SUNWescom BASEDIR`
    if [ "$RELOCATED_ROOT" != "" ]; then
	ps_cfgfile="$RELOCATED_ROOT/$ps_basedir/SUNWsymon/sbin/.cfginfo"
    else
	ps_cfgfile="$ps_basedir/SUNWsymon/sbin/.cfginfo"
    fi

    if [ -f "$ps_cfgfile" ]; then
        $CMD_CAT $ps_cfgfile | $CMD_GREP $ps_system > /dev/null 2>&1
	[ $? -eq 0 ] && return 1
    fi

    return 0
}

# Set the product (PE or DE)
# $1 - 1 for PE; 2 for DE
# Return: 1 for success; 0 for invalid entry
setProduct() {
    answer=$1
    case "$answer" in
	1)  INSTALL_TYPE="PE"
            INSTALLATION_PROPERTY=`getI18NPropertyFile "${SRC_DIR}/PE/Solaris_${OSVERS}"`
	    INSTALLATION_INFO="${SRC_DIR}/PE/Solaris_${OSVERS}/installation.info"
	    echolog ""
	    echolog 'Production Environment Installation'
	    echolog '-----------------------------------'
	    ;;
	2)  INSTALL_TYPE="DE"
	    TEMP_INFO="${LOGDIR}/installation.info.$$"
	    TEMP_PROPERTY="${LOGDIR}/installation.properties.$$"
	    tmp_pe_dir="${SRC_DIR}/PE/Solaris_${OSVERS}"
	    tmp_de_dir="${SRC_DIR}/DE/Solaris_${OSVERS}"
	    $CMD_CAT $tmp_pe_dir/installation.info $tmp_de_dir/installation.info > $TEMP_INFO
	    $CMD_CAT `getI18NPropertyFile "$tmp_pe_dir"` `getI18NPropertyFile "$tmp_de_dir"` > $TEMP_PROPERTY
	    INSTALLATION_PROPERTY="$TEMP_PROPERTY"
	    INSTALLATION_INFO="$TEMP_INFO"
	    echolog ""
	    echolog 'Developer Environment Installation'
	    echolog '----------------------------------'

	    # Check for valid license
	    cmd_checkkey=`$CMD_ECHO "$0" | $CMD_SED -e "s%es-inst%checkkey%"`
	    if [ ! -f $cmd_checkkey ]; then
		echolog ''
		echolog 'Cannot find $2.' "$cmd_checkkey"
		quit
	    fi
	    get_input_from_user "Enter the development system license: "
	    number=-1
	    [ "$answer" != "" ] && number=`$cmd_checkkey -k $answer`
	    if [ "$answer" = "" -o $number -eq -1 ]; then
		$CMD_ECHO "  \c"
		echolog "Invalid License, please buy a valid license."
		#quit
	    else
		[ ! -d "$VARDIR" ] && $CMD_MKDIR -p "$VARDIR"
		$CMD_ECHO $answer > "$VARDIR/.license"
	    fi
	    ;;
	*)  $CMD_ECHO "  \c"
	    echolog '  Invalid entry.'
	    echolog ''
	    return 0
	    ;;
    esac

    if [ ! -f "$INSTALLATION_PROPERTY" -a ! -f "$INSTALLATION_INFO" ]; then
	$CMD_ECHO "  \c"
	echolog '  Installation configuration files not found.'
	quit
    fi

    return 1
}

# Installs PE or DE
installSunMC() {
    if [ "$INSTALL_TYPE" = "ADDON" -o "$INSTALL_TYPE" = "ADDON_ONLY" ]; then
	return
    fi

    tmp_auto=`getAutoConfig "CORE_PRODUCT"`
    [ "$tmp_auto" = "" ] && tmp_auto=0
    tmp_product_exists=0
    productExists "PE"
    if [ $? -eq 1 ]; then # PE exists
	if [ $tmp_auto -eq 2 ] ; then # Product mismatch in autoconfig
	    echolog 'PE is already installed in this machine.'
	    echolog 'Unable to continue automated installation due to product mismatch.'
	    quit
	fi
	setProduct 1
	tmp_product_exists=1
    else
	productExists "DE"
	if [ $? -eq 1 ]; then # DE exists
	    if [ $tmp_auto -eq 1 ] ; then # Product mismatch in autoconfig
		echolog 'DE is already installed in this machine.'
		echolog 'Unable to continue automated installation due to product mismatch.'
		quit
	    fi
	    setProduct 2
	    tmp_product_exists=1
	fi
    fi

    if [ $tmp_product_exists -eq 0 ]; then

	#
	#The if block below is for agent only installation. This will be used
	#when user has used our script to create agent only product from our 
	#CD and wants to install it. User will run es-inst -a to set AGENT_ONLY=1
	#

        if [ $AGENT_ONLY -eq 1 ] ; then
            setProduct 1
        else
	    if [ "$OSVERS" = "2.5.1" ] ; then
		echolog "You can install only agent components on Solaris 2.5.1"
		setProduct 1
	    else
		echolog ''
		echolog 'Select one of the following:'
		echolog ''
		$CMD_ECHO "  \c"
		echolog '\\\(1\\\) Production Environment \\\(PE\\\)'
		$CMD_ECHO "  \c"
		echolog '\\\(2\\\) Developer Environment \\\(DE\\\)'
		echolog ''

		if [ "$AUTOCONFIG_RUNFILE" != "" ] ; then
		    echolog 'Enter your choice: [1\\\|2] $2' "$tmp_auto"
		    setProduct $tmp_auto
		else
		    while get_input_from_user 'Enter your choice: [1\\\|2]'
		    do
			tmp_auto=$answer
			setProduct $answer
			if [ $? -eq 1 ] ; then
			    if [ "$AUTOCONFIG_SAVEFILE" != "" ] ; then
				saveAutoConfig "CORE_PRODUCT" "$tmp_auto"
			    fi
			    break;
			fi
		    done
		fi
	    fi
        fi
    fi

    installProduct
    copyLocaleFiles "$SRC_DIR/PE/Solaris_${OSVERS}"
}

# Checks the validity of the target directory
# Returns: 1 if valid; 0 otherwise
checkTargetDirectory() {
    # If some components are installed in a directory already,
    # force TARGET_DIR to be that directory.
    EXISTS=`$CMD_PKGPARAM $RELOC_PARAM SUNWescom BASEDIR 2> /dev/null`
    if [ $? -eq 0 ]; then # Exists
	RELOC_EXISTS=`getRelocPathName $EXISTS`
	if [ "$TARGET_DIR" != "" ]; then
	    echolog ''
	    echolog 'Some components have already been installed in $2' "$RELOC_EXISTS"
	    echolog 'The target directory will be set to $2' "$RELOC_EXISTS"
	    echolog ''
	fi
	ORIGINAL_TARGET_DIR="$EXISTS"
	TARGET_DIR="$RELOC_EXISTS"
	return 1
    fi

    if [ "$AUTOCONFIG_RUNFILE" != "" ] ; then # Run automated configuration
	ORIGINAL_TARGET_DIR=`getAutoConfig "CORE_TARGET"`
	TARGET_DIR=`getRelocPathName $ORIGINAL_TARGET_DIR`
	if [ ! -d "$TARGET_DIR" ] ; then # Directory doesn't exist
	    $CMD_MKDIR -p "$TARGET_DIR" > /dev/null 2>&1 # Create it
	    if [ $? -ne 0 ]; then
		$CMD_ECHO "    \c"
		echolog 'Cannot create the directory $2' "$TARGET_DIR"
		quit
	    fi
	fi
	return 1
    fi

    [ "$TARGET_DIR" = "" ] && return 0
    [ -d "$TARGET_DIR" ] && return 1
    $CMD_ECHO "  \c"
    echolog 'The directory $2 does not exist' "$TARGET_DIR"
    $CMD_ECHO "  \c"
    ask_user 'Do you want to create it?'
    [ $answer -eq 0 -o $answer -eq 2 ] && quit
    if [ $answer -eq 1 ]; then
	$CMD_MKDIR -p "$TARGET_DIR" > /dev/null 2>&1
	if [ $? -ne 0 ]; then
	    $CMD_ECHO "    \c"
	    echolog 'Cannot create the directory $2' "$TARGET_DIR"
	    quit
	else
	    return 1
	fi
    fi
    return 0
}

# Gets the target directory
getTargetDirectory() {
    echolog ''
    checkTargetDirectory
    if [ $? -eq 0 ]; then
	temp_dir="/opt"
	while get_input_from_user 'Please enter the target directory [$2]:' "$temp_dir"
	do
	    if [ -z "$answer" ]; then
		ORIGINAL_TARGET_DIR="$temp_dir"
		TARGET_DIR=`getRelocPathName "$temp_dir"`
	    else
		ORIGINAL_TARGET_DIR="$answer"
		TARGET_DIR=`getRelocPathName "$answer"`
	    fi
	    checkTargetDirectory
	    [ $? -eq 1 ] && break
	done
	echolog ''
    else # Uses the existing target directory
	echolog 'Target directory: $2' "$TARGET_DIR"
    fi

    if [ "$AUTOCONFIG_SAVEFILE" != "" ] ; then # Save config
	saveAutoConfig "CORE_TARGET" "$ORIGINAL_TARGET_DIR"
    fi
}

getTargetDirectoryWithReloc() {
    echolog ''
    checkTargetDirectory
    if [ $? -eq 0 ]; then
	temp_dir="/opt"
	#while get_input_from_user 'Please enter the target directory [$2]:' "$temp_dir"
	while get_input_from_user 'Please enter the target directory [$2]:' "${RELOCATED_ROOT}$temp_dir"
	do
	    if [ -z "$answer" ]; then
		ORIGINAL_TARGET_DIR="$temp_dir"
		TARGET_DIR=`getRelocPathName "$temp_dir"`
	    else
		answer=`verifyTargetDirectoryAndGetBaseDir $answer`
                if [ $? -eq 1 ] ; then
                    continue
                fi
		ORIGINAL_TARGET_DIR="$answer"
		TARGET_DIR=`getRelocPathName "$answer"`
	    fi
	    checkTargetDirectory
	    if [ $? -eq 1 ]; then
		break
	    else
		continue
	    fi
	done
	echolog ''
    else
	echolog 'Target directory: $2' "$TARGET_DIR"
    fi
}

verifyTargetDirectoryAndGetBaseDir () {
    local_target_dir=`/usr/bin/echo $1 | /usr/bin/sed -e 's|//*|/|g'`
    local_root_dir=`/usr/bin/echo $RELOCATED_ROOT | /usr/bin/sed -e 's|//*|/|g'`
    echo "$local_target_dir" | /usr/bin/grep "^$local_root_dir" > /dev/null 2>&1
    if [ $? -ne 0 ] ; then
        echolog 'The path entered is not located on $2' "$local_target_dir"
        return 1
    fi
    local_base_dir=`echo $local_target_dir | /usr/bin/sed -e "s|^$local_root_dir|/|"`
    echo "$local_base_dir" | /usr/bin/sed -e 's|//*|/|g'
    return 0
}

# Gets the absolute directory
# $1 - the input directory
getPathName() {
    arg="$1"
    saveDir=`$CMD_PWD`
    if [ -d "$arg" ]; then # Is a directory
	cd $arg
	ret=`$CMD_PWD`
    elif [ -f "$arg" ]; then # Is a file
	cd `$CMD_DIRNAME $arg`
	ret=`$CMD_PWD`/`$CMD_BASENAME $arg`
    else
	ret="$1"
    fi
    cd $saveDir

    $CMD_ECHO `$CMD_ECHO $ret | $CMD_SED -e 's/\/\//\//'`
}

# Gets the absolute directory (relocatable or otherwise)
# $1 - the input directory
getRelocPathName() {
    ret=`getPathName $1`
    if [ "$RELOCATED_ROOT" != "" ]; then
	if [ "$ret" = "/" ]; then
	    ret="$RELOCATED_ROOT"
	else
	    ret="$RELOCATED_ROOT/$ret"
	fi
    fi

    $CMD_ECHO `$CMD_ECHO $ret | $CMD_SED -e 's/\/\//\//'`
}

# Checks whether a component exists in the list for installation
# $1 - Pack key
# $2 - Component key
# Return: 1 if exists; 0 otherwise
componentExists() {
    EXISTS1=`echo $INSTALL_COMPONENTS | $CMD_GREP "$1:$2:"`
    EXISTS2=`echo $MASTER_INSTALL_COMPONENTS | $CMD_GREP "$1:$2:"`
    if [ -z "$EXISTS1" -a -z "$EXISTS2" ]; then
	return 0
    else
	return 1
    fi
}

# Get the correct directory
# $1 - the suffix to be added to the return directory
# Return: $CORRECT_DIR
getCorrectDirectory() {
    if [ "$INSTALL_TYPE" = "UNKNOWN" ]; then
	[ -f "${SRC_DIR}/DE/Solaris_${OSVERS}/installation.info" ] && [ -f "${SRC_DIR}/PE/Solaris_${OSVERS}/installation.info" ] && return 1
	[ -d "${SRC_DIR}/Addons" ] && INSTALL_TYPE="ADDON_ONLY" && return 1
    fi

    if [ "$INSTALL_TYPE" = "PE" -o "$INSTALL_TYPE" = "DE" ]; then
	CORRECT_DIR="${SRC_DIR}/PE/Solaris_${OSVERS}/$1" # OS
	[ -d "$CORRECT_DIR" ] && return 1

	CORRECT_DIR="${SRC_DIR}/PE/Common/$1" # OS
	[ -d "$CORRECT_DIR" ] && return 1

	if [ "$INSTALL_TYPE" = "DE" ]; then
	    CORRECT_DIR="${SRC_DIR}/DE/Solaris_${OSVERS}/$1" # OS
	    [ -d "$CORRECT_DIR" ] && return 1

	    CORRECT_DIR="${SRC_DIR}/DE/Common/$1" # OS
	    [ -d "$CORRECT_DIR" ] && return 1
	fi
    fi

    if [ "$INSTALL_TYPE" = "ADDON" -o "$INSTALL_TYPE" = "ADDON_ONLY" ]; then # Addons
	CORRECT_DIR="${SRC_DIR}/Addons/$VAR_ADDON_PRODUCT/Solaris_${OSVERS}/$1" # OS
	[ -d "$CORRECT_DIR" ] && return 1

	CORRECT_DIR="${SRC_DIR}/Addons/$VAR_ADDON_PRODUCT/Common/$1" # OS
	[ -d "$CORRECT_DIR" ] && return 1
    fi

    CORRECT_DIR=""
    return 0
}

# Get the component script
# $1 - pack key
# $2 - component script name
# Return: $COMPONENT_SCRIPT_FILE
getComponentScript() {
    getPackSubdir "$1"
    getCorrectDirectory "$PACK_SUBDIR"
    if [ $? -eq 1 -a -f "${CORRECT_DIR}/$2" ]; then # Found
	COMPONENT_SCRIPT_FILE="${CORRECT_DIR}/$2"
	return 1
    fi
    return 0
}

# Get the right source directory for a package
# $1 - pack key
# $2 - package name
# Return: $PACKAGE_DIR
getPackageDirectory() {
    getPackSubdir "$1"
    getCorrectDirectory "${PACK_SUBDIR}/$2"
    ret=$?
    PACKAGE_DIR="$CORRECT_DIR"
    return $ret
}

# Add component to the list for installation
# $1 - Pack key
# $2 - Component key
addComponent() {
    if [ "$AUTOCONFIG_SAVEFILE" != "" ] ; then # Save autoconfig
	if [ "$global_core_components" = "" ] ; then
	    global_core_components="$2"
	else
	    global_core_components="$global_core_components,${2}"
	fi
    fi

    getI18NMessage "$2"
    getComponentInfo "$1" "$2" "PACKAGES" "$LOCALE_CODE"
    PACKAGE_LIST="$COMPONENT_INFO"
    if [ $? -ne 0 -o "$PACKAGE_LIST" = "" ]; then
	return 0
    else
	NEW_PACKAGE_LIST=""
	for pkg in `$CMD_ECHO "$PACKAGE_LIST" | $CMD_AWK -F ',' '{ for (i = 1; i <= NF; i++) print $i }'`
	do
	    packageExists "$pkg" "$1"
	    ret="$?"
	    if [ $ret -eq 1 ]; then
		$CMD_ECHO "      \c"
		echolog 'Package $2 is already installed.' "$pkg"
		continue
	    fi

	    getPackageDirectory "$1" "$pkg"
	    if [ $? -eq 0 ]; then
		echolog ''
		echolog 'Package not found: $2' "$pkg"
		quit
	    fi

	    if [ "$NEW_PACKAGE_LIST" != "" ]; then
		NEW_PACKAGE_LIST="${NEW_PACKAGE_LIST},$pkg"
	    else
		NEW_PACKAGE_LIST="$pkg"
	    fi
	done
    fi

    if [ -n "$INSTALL_COMPONENTS_LOG" ]; then
	INSTALL_COMPONENTS_LOG="${INSTALL_COMPONENTS_LOG}, $MSG"
    else
	INSTALL_COMPONENTS_LOG="$MSG"
    fi

    [ "$NEW_PACKAGE_LIST" = "" ] && return # Nothing to add

    getPackCD "$1"
    if [ -n "$INSTALL_COMPONENTS" ]; then
	componentExists $1 $2
	[ $? -eq 0 ] && INSTALL_COMPONENTS="$INSTALL_COMPONENTS $1:$2:$PACK_CD:$NEW_PACKAGE_LIST"
    else
	INSTALL_COMPONENTS="$1:$2:$PACK_CD:$NEW_PACKAGE_LIST"
    fi
}

# Get the Pack CD
# $1 - Pack key
# Return: $PACK_CD
getPackCD() {
    LINENUM_START=`$CMD_GREP -n -e "PACK_KEY=$1" $INSTALLATION_INFO | $CMD_GREP -v "\s*#" | $CMD_SED -e "s/:.*//"`
    if [ -z "$LINENUM_START" ]; then
	echolog 'Error: Cannot find pack key $2.' "$1"
	quit
    fi
    LINENUM_END=`$CMD_SED -n -e "${LINENUM_START},${EOL}p" $INSTALLATION_INFO | $CMD_GREP -v "\s*#"| $CMD_GREP -n -e "PACK_CD" | $CMD_SED -e "s/:.*//"`
    if [ "$LINENUM_END" = "" ]; then # Default to 1
	PACK_CD=1
	return
    fi
    LINENUM_END=`$CMD_ECHO $LINENUM_END | $CMD_SED -e "s/ .*//"`
    LINENUM=`$CMD_EXPR $LINENUM_START + $LINENUM_END - 1`
    PACK_CD=`$CMD_SED -n -e "${LINENUM},${LINENUM}p" $INSTALLATION_INFO | $CMD_SED -e "s/\s*PACK_CD=//"`
    PACK_CD=`$CMD_ECHO $PACK_CD | $CMD_SED -e "s/^\s*//"`
}

# Remove a component from the installation list
# $1 - Pack key
# $2 - Component key
removeComponent() {
    getI18NMessage $2
    INSTALL_COMPONENTS_LOG=`$CMD_ECHO "$INSTALL_COMPONENTS_LOG" | $CMD_SED -e "s/$MSG, //" -e "s/$MSG//"`
    tmp_install_components=""
    for each_component in $INSTALL_COMPONENTS
    do
	each_component=`$CMD_ECHO $each_component | $CMD_SED -e "s/$1:$2:.*//"`
	[ "$each_component" != "" ] && tmp_install_components="$tmp_install_components $each_component"
    done
    INSTALL_COMPONENTS="$tmp_install_components"
}

# Component selection
selectComponents() {
    MASTER_INSTALL_COMPONENTS=""
    tmp_no_product_install=0
    global_core_components=""
    for i in `$CMD_ECHO "$INSTALL_PACKS" | $CMD_AWK -F ',' '{ for (i = 1; i <= NF; i++) print $i }'`
    do
	echologverbose ''
	addSummary ""
	getI18NMessage $i
	echologverbose 'Pack: $2' "$MSG"
	addSummary "Pack: $MSG"

	# Get the component list for a pack on the layers
	INSTALL_COMPONENTS=""
	INSTALL_COMPONENTS_LOG=""
	getComponentList "$i" $INSTALL_LAYERS
	# Filter out non-installable addon components
	if [ "$INSTALL_TYPE" = "ADDON_ONLY" ]; then
	    tmp_component_list="$COMPONENT_LIST"
	    COMPONENT_LIST=""
	    for component_key in `$CMD_ECHO "$tmp_component_list" | $CMD_AWK -F ',' '{ for (i = 1; i <= NF; i++) print $i }'`
	    do
		echo "$COMPONENTS_NOT_TO_BE_INSTALLED" | /usr/bin/grep "$VAR_ADDON_PRODUCT:$i:$component_key" > /dev/null 2>&1
		if [ $? -eq 0 ] ; then
		    echologverbose 'skipping component $2' "$component_key"
		    continue
		fi
		getComponentInfo "$i" "$component_key" "PACKAGES" "$LOCALE_CODE"
		tmp_package_list="$COMPONENT_INFO"
		for pkg in `$CMD_ECHO "$tmp_package_list" | $CMD_AWK -F ',' '{ for (i = 1; i <= NF; i++) print $i }'`
		do
		    getPackageDirectory "$i" "$pkg"
		    if [ $? -eq 1 ]; then
			isAddonPackageInstallable "${PACKAGE_DIR}/pkginfo"
			if [ $? -eq 1 ]; then # Add component to COMPONENT_LIST
			    if [ "$COMPONENT_LIST" = "" ]; then
				COMPONENT_LIST="$component_key"
			    else
				COMPONENT_LIST="${COMPONENT_LIST},$component_key"
			    fi
			    break
			fi
		    fi
		done
	    done
	fi

	if [ "$COMPONENT_LIST" != "" ]; then
	    tmp_comma=`$CMD_ECHO $COMPONENT_LIST | $CMD_GREP ","`

	    # Select components for installing in a pack
	    if [ "$INSTALL_TYPE" != "DE" ]; then
		$CMD_ECHO "\c"
		echologverbose '  Please select the components to install:'
	    fi
	    for j in `$CMD_ECHO "$COMPONENT_LIST" | $CMD_AWK -F ',' '{ for (i = 1; i <= NF; i++) print $i }'`
	    do
		getI18NMessage "$j"
		getComponentInfo "$i" "$j" "ESSENTIAL"
		componentExists "$i" "$j"
		if [ $? -eq 0 ]; then
		    if [ "$INSTALL_TYPE" = "DE" -o "$COMPONENT_INFO" = "true" ]; then
			$CMD_ECHO "\c"
			echologverbose '    Component $2 is essential and will be automatically installed.' "$MSG"
			addComponent "$i" "$j"
		    else
			answer=0
			if [ "$AUTOCONFIG_RUNFILE" != "" ] ; then # Run autoconfig
			    tmp_auto=`getAutoConfig "CORE_COMPONENTS:${i}"`
			    tmp_auto=`$CMD_ECHO $tmp_auto | $CMD_GREP $j`
			    [ "$tmp_auto" = "" ] && continue
			    answer=1
			    $CMD_ECHO "    \c"
			    echolog '    Selecting component: $2' "$MSG"
			else
			    $CMD_ECHO "    \c"
			    ask_user '    Do you want to install $2?' "$MSG"
			fi
			[ $answer -eq 1 ] && addComponent "$i" "$j"
		    fi
		fi
	    done

	    for j in `$CMD_ECHO "$INSTALL_COMPONENTS" | $CMD_AWK '{ for (i = 1; i <= NF; i++) print $i }'`
	    do
		# Get the packages list for the component
		j=`$CMD_ECHO "$j" | $CMD_AWK -F ':' '{ print $2 }'`
		getI18NMessage $j
		getComponentInfo "$i" "$j" "PACKAGES" "$LOCALE_CODE"
		if [ "$COMPONENT_INFO" != "" ]; then
		    # Check if the packages for the component have already been installed
		    packagesExist "$i" "$COMPONENT_INFO"
		    ret=$?
		    if [ $ret -eq 1 ]; then # Package exists
			$CMD_ECHO "    \c"
			echolog '    Component $2 has already been installed.' "$MSG"
			addSummary "    Component $MSG has already been installed."
			$CMD_ECHO "    \c"
			echolog '    The corresponding packages are: $2' "$INSTALLED_PKGS"
			echolog ''
		    else # Not exist or versions not matched
			# Check the component-level dependencies among components
			componentExists "$i" "$j"
			if [ $? -eq 1 ]; then
			    getComponentInfo "$i" "$j" "DEPENDENCIES"
			    if [ $? -eq 0 -a "$COMPONENT_INFO" != "" ]; then
				checkComponentDependencies "$i" "$COMPONENT_INFO"
				if [ $? -ne 0 ]; then
				    getI18NMessage $j
				    echologverbose ''
				    $CMD_ECHO "\c"
				    echologverbose '    Component $2 is dependent on: $3' "$MSG" "$DEPENDENT_COMPONENTS"
				    $CMD_ECHO "\c"
				    echologverbose '    The component\\\(s\\\) will be automatically installed.'
				    echologverbose ''
				fi
			    fi
			fi

		    fi
		fi
	    done

	    if [ $tmp_no_product_install -eq 1 ]; then
		INSTALL_COMPONENTS_LOG=""
		INSTALL_COMPONENTS=""
		MASTER_INSTALL_COMPONENTS=""
		return
	    fi

	    echologverbose ''
	    $CMD_ECHO "\c"
	    if [ "$INSTALL_COMPONENTS_LOG" != "" ]; then
		echologverbose '  Selected Components: $2' "$INSTALL_COMPONENTS_LOG"
		addSummary "  Selected Components: $INSTALL_COMPONENTS_LOG"
	    else
		echologverbose '  Selected Components: \\\<default\\\>'
		addSummary '  Selected Components: <default>'
	    fi
	    echologverbose ''

	fi
	if [ -n "$MASTER_INSTALL_COMPONENTS" ]; then
	    MASTER_INSTALL_COMPONENTS="${MASTER_INSTALL_COMPONENTS} $INSTALL_COMPONENTS"
	else
	    MASTER_INSTALL_COMPONENTS="$INSTALL_COMPONENTS"
	fi

	if [ "$AUTOCONFIG_SAVEFILE" != "" ] ; then # Save autoconfig
	    saveAutoConfig "CORE_COMPONENTS:${i}" "$global_core_components"
	fi
    done
}

# Input - None
# Returns - Sets BUNDLED_JAVA_USED
#
CheckIfJavaInstallRequired() {
   BUNDLED_JAVA_USED=0
   if [ $INSTALL_JAVA -eq 1 ]; then
      checkJavaOK 
      if [ $? -eq 0 ]; then
         echolog ''
         echolog 'The required version of Java is already installed : $2' "$JAVA_FULLVERSION"

         # Update JAVA_CFG_FILE This file is used at uninst time 
         # CFGINFOFILE  is set according to relocated root.
         # 
         JAVA_CFG_FILE="$CFGINFOFILE"
         put_var_in_cfgfile "$JAVA_CFG_FILE" "JAVADIR" "$SYMON_JAVAHOME" "="
         put_var_in_cfgfile "$JAVA_CFG_FILE" "INSTALLED" "0" "="
         put_var_in_cfgfile "$JAVA_CFG_FILE" "OVERWRITTEN" "0" "="
         put_var_in_cfgfile "$JAVA_CFG_FILE" "PKGLIST" " " "="
         return 0
      fi
       
      if [ "$JAVA_MINIMUM_REQUIRED" != "Solaris_JDK_1.2.2_06" ] # In case of s9 also this will quit as min java req is 1.4
      then                                                      # So no check for OS required here. 
         echolog ''
         echolog 'Sun Management Center requires complete installation of $2 or above.' "$JAVA_MINIMUM_REQUIRED"
         echolog 'Please follow the following steps before Sun Management Center installation.'
         echolog '1. Install supported JDK version.'
         echolog '2. set JAVA_HOME to directory where JDK is installed.'
         echolog '3. export JAVA_HOME'
         echolog ''
         quit
      fi # Else install 1.2.2_06
        
      BUNDLED_JAVA_USED=1
      echologverbose 'Install will try to use java bundled with SunMC.'
      echologverbose ''
  fi

}



# Install Java, if required
installJava() {
    if [ $INSTALL_JAVA -eq 1 ]; then

       [ "$BUNDLED_JAVA_USED" != "1" ] && return 0
       if [ "$INSTALL_TYPE" = "DE" ]; then
          install_java "ds"
       else
          install_java "ps"
       fi

       if [ $? -eq 1 ]; then
          echologverbose ''
          quit
       fi
	
       if [ -f "$VAROPTDIR/.java_home" ] ; then
          $CMD_CHMOD 644 $VAROPTDIR/.java_home > /dev/null 2>&1
       fi
   fi
}

# Calculate the required disk space
# $1 - the directory
getRequiredSpace() {
    REQUIRED_SPACE=0
    for each_pkglist in `$CMD_ECHO "$MASTER_INSTALL_COMPONENTS" | $CMD_AWK -F ' ' '{ for (i = 1; i <= NF; i++) print $i }'`
    do

	tmp_pack=`$CMD_ECHO "$each_pkglist" | $CMD_AWK -F ':' '{ print $1 }'`
	each_pkglist=`$CMD_ECHO "$each_pkglist" | $CMD_AWK -F ':' '{ print $4 }'`
	for each_pkg in `$CMD_ECHO "$each_pkglist" | $CMD_AWK -F ',' '{ for (i = 1; i <= NF; i++) print $i }'`
	do
	    getPackageDirectory "$tmp_pack" "$each_pkg"
	    [ $? -eq 0 ] && continue
	    each_pkg="$PACKAGE_DIR"
	    pkg_base=`$CMD_PKGPARAM -f "${each_pkg}/pkginfo" BASEDIR`

          if [ "$pkg_base" != "$1" ]
          then
            continue
          fi

	    MAPFILE="${each_pkg}/pkgmap"
	    if [ ! -f "$MAPFILE" ] ; then
		$CMD_ECHO "  \c"
		echolog '  Warning - Could not find pkgmap file for $2' "$each_pkg"
		$CMD_ECHO "  \c"
		echolog '  Space calculations will be wrong.'
		continue
	    fi
	    #  Assume that the package total will be in the first 10 lines
	    #  of the pkgmap file.
	    pkg_size=`$CMD_HEAD -10 "$MAPFILE" | $CMD_GREP '^:' | $CMD_AWK '{ print $3 }'`
	    REQUIRED_SPACE=`$CMD_EXPR $pkg_size + $REQUIRED_SPACE`
	done
    done
}

# Get the available disk space
# $1 - the directory
getAvailableSpace() {

    if [ -d $1 ]
    then
       df_line=`$CMD_DF -k "$1" | $CMD_TAIL -1`
       AVAILABLE_SPACE=`$CMD_ECHO "$df_line" | $CMD_AWK '{ print $4 }'`
       # Double the number reported by df -k to get 512-byte blocks to get
       # the same units as pkgmap
       AVAILABLE_SPACE=`$CMD_EXPR $AVAILABLE_SPACE \* 2`
    else
       AVAILABLE_SPACE=0
    fi
}

# Get the filesystem associated with a directory
# $1 - the directory
getFileSystem() {

    if [ -d $1 ]
    then
       FILESYSTEM_NAME=`$CMD_DF -k $1 | $CMD_TAIL -1 | $CMD_AWK '{ print $1 }'`
    else
       FILESYSTEM_NAME="unknown"
    fi

   return 0
}


# Check disk space for database
# Return: $db_required_space
# Return: $db_required_space_dbf
getDatabaseRequiredSpace() {
    db_required_space=0
    db_required_space_dbf=0
    DB_DIR="${SRC_DIR}/db"

    each_pkg="SUNWesora"
    pkg_base=`$CMD_PKGPARAM -f $DB_DIR/$each_pkg/pkginfo BASEDIR`
    MAPFILE="$DB_DIR/$each_pkg/pkgmap"
    if [ ! -f "$MAPFILE" ]; then
	echolog 'Warning - Could not find the pkgmap file for $2' "$each_pkg"
	echolog "  Space calculations will be wrong."
	return 1
    fi

    #  Assume that the package total will be in the first 10 lines of the pkgmap file.
    pkg_size=`$CMD_HEAD -10 "$MAPFILE" | $CMD_GREP '^:' | $CMD_AWK '{ print $3 }'`
    db_required_space=`$CMD_EXPR $pkg_size`



    each_pkg="SUNWestbl"

    #
    # a nonzero return code from the following indicates
    # that the package does not exist.
    #


    if [ "$RELOCATED_ROOT" != "" ]
    then
       $CMD_PKGINFO -R $RELOCATED_ROOT -q $each_pkg
       retcode=$?
    else
       $CMD_PKGINFO -q $each_pkg
       retcode=$?
    fi

    if [ $retcode -gt 0 ]
    then
      pkg_base=`$CMD_PKGPARAM -f $DB_DIR/$each_pkg/pkginfo BASEDIR`
      MAPFILE="$DB_DIR/$each_pkg/pkgmap"
      if [ ! -f "$MAPFILE" ]; then
        echolog 'Warning - Could not find the pkgmap file for $2' "$each_pkg"
        echolog "  Space calculations will be wrong."
	  return 1
      fi

      #  Assume that the package total will be in the first 10 lines of the pkgmap file.
      pkg_size=`$CMD_HEAD -10 "$MAPFILE" | $CMD_GREP '^:' | $CMD_AWK '{ print $3 }'`
      db_required_space_dbf=`$CMD_EXPR $pkg_size`
      db_required_space_dbf=`$CMD_EXPR $db_required_space_dbf + 200 \* 1024 \* 2`

    fi


    return 0
}


# Check the disk space
checkDiskSpace() {
    echolog ''
    echolog 'Checking disk space...'

    #
    #  Perform some parameter inits
    #

    TEMP_TARGET_DIR="$TARGET_DIR"
    [ ! -d "$TEMP_TARGET_DIR" ] && $CMD_MKDIR -p $TEMP_TARGET_DIR

    TEMP_DBFILE_DIR="/var/opt"

    #
    #  If the relocated root is defined then prepend it to the
    #  dbfiles and etc parameters.  We will remove it later.
    #

    if [ "$RELOCATED_ROOT" != "" ]
    then
      TEMP_DBFILE_DIR=$RELOCATED_ROOT$TEMP_DBFILE_DIR
      ETC=$RELOCATED_ROOT$ETC
      java_root_dir=$RELOCATED_ROOT
    else
      java_root_dir=""
    fi

    #
    #  If the dbfiles and etc directories don't already exist
    #  create them
    #
    if [ ! -d "$ETC" ]
    then
         $CMD_MKDIR -p $ETC
    fi

    if [ ! -d "$TEMP_DBFILE_DIR" ]
    then
          $CMD_MKDIR -p $TEMP_DBFILE_DIR
    fi

    #
    #  Working parameters init
    #

    quit_program=0

    fs_one=""  # used for software proper
    fs_two=""  # used for dbfiles if needed
    fs_three="" # used for etc stuff
    fs_four="" # used for java
    REQUIRED_SPACE_ETC=0  # actual required space for etc stuff
    REQUIRED_SPACE_SOFT=0 # actual required space for software proper
    AVAILABLE_SPACE_P=0 # available space for spftware proper
    REQUIRED_SPACE_DBF=0 # actual required space for dbfiles
    AVAILABLE_SPACE_D=0 #available space for dbfiles


    # System disk space check
    getRequiredSpace "/"
    space_required_etc=$REQUIRED_SPACE
    getAvailableSpace "$ETC"
    space_available_etc=$AVAILABLE_SPACE

    getFileSystem "$ETC"
    fs_three=$FILESYSTEM_NAME 


    # SunMC/Addon disk space check
    getRequiredSpace "/opt"

    trailslash=`echo $TEMP_TARGET_DIR | /usr/bin/grep -i "/$" | /usr/bin/wc -l`

    if [ $trailslash = 0 ]
    then
       TMP_VAR_PATH="$TEMP_TARGET_DIR/SUNWsymon"
    else
       TMP_VAR_PATH="${TEMP_TARGET_DIR}SUNWsymon"
    fi

    if [ -d "$TMP_VAR_PATH" ]
    then
        getAvailableSpace "$TMP_VAR_PATH"
        getFileSystem "$TMP_VAR_PATH"
        fs_one=$FILESYSTEM_NAME 
    else
        getAvailableSpace "$TEMP_TARGET_DIR"
        getFileSystem "$TEMP_TARGET_DIR"
        fs_one=$FILESYSTEM_NAME 
    fi

#    echolog "the required_space var = $REQUIRED_SPACE"
#    echolog "the available_space var = $AVAILABLE_SPACE"



    # initialize variables

    REQUIRED_SPACE_SOFT=$REQUIRED_SPACE
    AVAILABLE_SPACE_P=$AVAILABLE_SPACE

    # Oracle disk space check
    # This check should be done only for core product, not for addons.
    if [ "$INSTALL_TYPE" = "ADDON" -o "$INSTALL_TYPE" = "ADDON_ONLY" ]; then
        echo "\c"
    else
    TEST=`$CMD_ECHO $INSTALL_LAYERS | $CMD_GREP "LAYER.SERVER"`
    if [ "$TEST" != "" ]; then
        getDatabaseRequiredSpace "$TEMP_TARGET_DIR" "$TEMP_DBFILE_DIR"
        REQUIRED_SPACE_TMP=`$CMD_EXPR $db_required_space + $REQUIRED_SPACE`

        ORACLE_TARGET_DIR="$TEMP_TARGET_DIR"

        #
        #  Now check to see if the basedir/SUNWsymon already exist, if so, then
        #  use this new augmented path to determine the available space.  The 
        #  purpose of this logic is to catch specific partition splits that occur
        #  along valid install directory tree.
        #

        trailslash=`echo $ORACLE_TARGET_DIR | /usr/bin/grep -i "/$" | /usr/bin/wc -l`

		# check if /<base_dir>/SUNWsymon itself is a different partition (v/s base_dir)

        if [ $trailslash = 0 ]
        then
              TMP_VAR_PATH="$ORACLE_TARGET_DIR/SUNWsymon"
        else
              TMP_VAR_PATH="${ORACLE_TARGET_DIR}SUNWsymon"
        fi

        if [ -d "$TMP_VAR_PATH" ]
        then
              getAvailableSpace "$TMP_VAR_PATH"
              getFileSystem "$TMP_VAR_PATH"
              fs_one=$FILESYSTEM_NAME 
        else
              getAvailableSpace "$ORACLE_TARGET_DIR"
              getFileSystem "$ORACLE_TARGET_DIR"
              fs_one=$FILESYSTEM_NAME 
        fi

		# check if target dirrectory is on the same partition as /etc

        if [ "$fs_one" = "$fs_three" ]; then
              REQUIRED_SPACE_TMP=`$CMD_EXPR $space_required_etc + $REQUIRED_SPACE_TMP`
   	    fi


        while [ "$REQUIRED_SPACE_TMP" -ge "$AVAILABLE_SPACE" ]
        do
        echolog ''
        echolog 'Insufficient disk space to install Sun Management Center Software'
#       echolog 'Sun Management Center DB application software requires: $2 Kbytes' "$db_required_space"
        echolog 'Sun Management Center software requires: $2 Blocks' "$REQUIRED_SPACE_TMP"
        if [ -d "$ORACLE_TARGET_DIR/SUNWsymon" ] ; then
          echolog 'The installation directory \\\"$2\\\" has: $3 Blocks' "$ORACLE_TARGET_DIR/SUNWsymon" "$AVAILABLE_SPACE"
        else
          echolog 'The installation directory \\\"$2\\\" has: $3 Blocks' "$ORACLE_TARGET_DIR" "$AVAILABLE_SPACE"
        fi
        echolog ''

	#
	# For space checking, we will not support automatic installation. If 
	# enough space is not available then user will be asked the question.
	# This is because, even if we support that, it is not gonna help much.
	# Lets say, user entered /export at the first installation, then on the
	# second machine, he will have to make sure that /export or /opt has enough
	# space. So its better that he makes the space available in the specified
	# target directory.
	#

        #if [ "$AUTOCONFIG_RUNFILE" != "" ] ; then # Run autoconfig
        #    answer=`getAutoConfig "CORE_DIR"`
        #else
        #    get_input_from_user 'Enter the directory to install the software: '
        #    if [ -n "$answer" -a "$AUTOCONFIG_SAVEFILE" != "" ] ; then # Save autoconfig
        #    saveAutoConfig "CORE_DIR" "$answer"
        #    fi
        #fi

        get_input_from_user 'Enter the directory to install the software: '

        if [ -n "$answer" ]; then
            ORACLE_TARGET_DIR="$answer"

		    if [ ! -d "$ORACLE_TARGET_DIR" ]
                then
                   $CMD_MKDIR -p $ORACLE_TARGET_DIR
                   retcode=$?

                   if [ $retcode != 0 ]
                   then
                      echolog 'mkdir failed, try again!'
                      continue
                   fi
                fi

                    REQUIRED_SPACE_TMP=`$CMD_EXPR $db_required_space + $REQUIRED_SPACE`

                    #
                    #  Now check to see if the basedir/SUNWsymon already exist, if so, then
                    #  use this new augmented path to determine the available space.  The 
                    #  purpose of this logic is to catch specific partition splits that occur
                    #  along valid install directory tree.
                    #
     
                    trailslash=`echo $ORACLE_TARGET_DIR | /usr/bin/grep -i "/$" | /usr/bin/wc -l`

                    if [ $trailslash = 0 ]
                    then
                       TMP_VAR_PATH="$ORACLE_TARGET_DIR/SUNWsymon"
                    else
                       TMP_VAR_PATH="${ORACLE_TARGET_DIR}SUNWsymon"
                    fi

                    if [ -d "$TMP_VAR_PATH" ]
                    then
                       getAvailableSpace "$TMP_VAR_PATH"
                       getFileSystem "$TMP_VAR_PATH"
                       fs_one=$FILESYSTEM_NAME 
                    else
                       getAvailableSpace "$ORACLE_TARGET_DIR"
                       getFileSystem "$ORACLE_TARGET_DIR"
                       fs_one=$FILESYSTEM_NAME 
                    fi

                    if [ "$fs_one" = "$fs_three" ]; then
                       REQUIRED_SPACE_TMP=`$CMD_EXPR $space_required_etc + $REQUIRED_SPACE_TMP`
   	            fi

            [ "$REQUIRED_SPACE_TMP" -lt "$AVAILABLE_SPACE" ] && break
        else
            $CMD_ECHO "  \c"
            echolog 'Please supply a directory.'
        fi
        done

        TEMP_TARGET_DIR="$ORACLE_TARGET_DIR"
        TARGET_DIR="$TEMP_TARGET_DIR"
        REQUIRED_SPACE_SOFT=$REQUIRED_SPACE_TMP
        AVAILABLE_SPACE_P=$AVAILABLE_SPACE

          
          #
          #  Determine if the tbl package already exists on the system, if so
          #  then don't try to add it again. This logic is to accomadate the
          #  keep existing data option of es-uninst.
          #


          if [ "$RELOCATED_ROOT" != "" ]
          then
             $CMD_PKGINFO -R $RELOCATED_ROOT -q SUNWestbl
             retcode=$?
          else
             $CMD_PKGINFO -q SUNWestbl
             retcode=$?
          fi

          if [ $retcode -gt 0 ]
          then

            #
            #  Now check to see if the basedir/SUNWsymon already exist, if so, then
            #  use this new augmented path to determine the available space.  The 
            #  purpose of this logic is to catch specific partition splits that occur
            #  along valid install directory tree.
            #

            trailslash=`echo $TEMP_DBFILE_DIR | /usr/bin/grep -i "/$" | /usr/bin/wc -l`

            if [ $trailslash = 0 ]
            then
                TMP_VAR_PATH="$TEMP_DBFILE_DIR/SUNWsymon"
            else
                TMP_VAR_PATH="${TEMP_DBFILE_DIR}SUNWsymon"
            fi

            if [ -d "$TMP_VAR_PATH" ]
            then
                getAvailableSpace "$TMP_VAR_PATH"
                getFileSystem "$TMP_VAR_PATH"
                fs_two=$FILESYSTEM_NAME 
            else
                getAvailableSpace "$TEMP_DBFILE_DIR"
                getFileSystem "$TEMP_DBFILE_DIR"
                fs_two=$FILESYSTEM_NAME 
            fi

	      if [ "$fs_one" = "$fs_two" ]; then
                  REQUIRED_SPACE_TMP=`$CMD_EXPR $db_required_space + $REQUIRED_SPACE`
	  	  REQUIRED_SPACE_TMP=`$CMD_EXPR $db_required_space_dbf + $REQUIRED_SPACE_TMP`
	      else
		  REQUIRED_SPACE_TMP=$db_required_space_dbf
	      fi

	      if [ "$fs_two" = "$fs_three" ]; then
                  REQUIRED_SPACE_TMP=`$CMD_EXPR $space_required_etc + $REQUIRED_SPACE_TMP`
	      fi

            ORACLE_TARGET_DIR="$TEMP_DBFILE_DIR"
            while [ "$REQUIRED_SPACE_TMP" -ge "$AVAILABLE_SPACE" ]
            do
            echolog ''
            echolog 'Insufficient disk space to install Sun Management Center Database Datafiles'
            echolog 'Sun Management Center Datafiles requires: $2 Blocks' "$REQUIRED_SPACE_TMP"
            if [ -d "$ORACLE_TARGET_DIR/SUNWsymon" ] ; then
              echolog 'The installation directory \\\"$2\\\" has: $3 Blocks' "$ORACLE_TARGET_DIR/SUNWsymon" "$AVAILABLE_SPACE"
            else
              echolog 'The installation directory \\\"$2\\\" has: $3 Blocks' "$ORACLE_TARGET_DIR" "$AVAILABLE_SPACE"
            fi
            echolog ''

            #
            # For space checking, we will not support automatic installation. If 
            # enough space is not available then user will be asked the question.
            # This is because, even if we support that, it is not gonna help much.
            # Lets say, user entered /export at the first installation, then on the
            # second machine, he will have to make sure that /export or /opt has enough
            # space. So its better that he makes the space available in the specified
            # target directory.
            #

            #if [ "$AUTOCONFIG_RUNFILE" != "" ] ; then # Run autoconfig
            #    answer=`getAutoConfig "CORE_DB_DIR"`
            #else
            #    get_input_from_user 'Enter the directory to install the datafiles: '
            #    if [ -n "$answer" -a "$AUTOCONFIG_SAVEFILE" != "" ] ; then # Save autoconfig
            #    saveAutoConfig "CORE_DB_DIR" "$answer"
            #    fi
            #fi

            get_input_from_user 'Enter the directory to install the datafiles: '

              if [ -n "$answer" ]; then
                  ORACLE_TARGET_DIR="$answer"

  		      if [ ! -d "$ORACLE_TARGET_DIR" ]
                  then
                     $CMD_MKDIR -p $ORACLE_TARGET_DIR
                     retcode=$?

                     if [ $retcode != 0 ]
                     then
                        echolog 'mkdir failed, try again!'
                        continue
                     fi
                  fi

                     #
                     #  Now check to see if the basedir/SUNWsymon already exist, if so, then
                     #  use this new augmented path to determine the available space.  The 
                     #  purpose of this logic is to catch specific partition splits that occur
                     #  along valid install directory tree.
                     #

                    trailslash=`echo $ORACLE_TARGET_DIR | /usr/bin/grep -i "/$" | /usr/bin/wc -l`

                    if [ $trailslash = 0 ]
                    then
                       TMP_VAR_PATH="$ORACLE_TARGET_DIR/SUNWsymon"
                    else
                       TMP_VAR_PATH="${ORACLE_TARGET_DIR}SUNWsymon"
                    fi

                    if [ -d "$TMP_VAR_PATH" ]
                    then
                        getAvailableSpace "$TMP_VAR_PATH"
                        getFileSystem "$TMP_VAR_PATH"
                        fs_two=$FILESYSTEM_NAME 
                    else
                        getAvailableSpace "$ORACLE_TARGET_DIR"
                        getFileSystem "$ORACLE_TARGET_DIR"
                        fs_two=$FILESYSTEM_NAME 
                    fi

		      if [ "$fs_one" = "$fs_two" ]; then
                          REQUIRED_SPACE_TMP=`$CMD_EXPR $db_required_space + $REQUIRED_SPACE`
			  REQUIRED_SPACE_TMP=`$CMD_EXPR $db_required_space_dbf + $REQUIRED_SPACE_TMP`
		      else
			  REQUIRED_SPACE_TMP=$db_required_space_dbf
		      fi
 
                      if [ "$fs_two" = "$fs_three" ]; then
                         REQUIRED_SPACE_TMP=`$CMD_EXPR $space_required_etc + $REQUIRED_SPACE_TMP`
                      fi


                  [ "$REQUIRED_SPACE_TMP" -lt "$AVAILABLE_SPACE" ] && break
               else
                  $CMD_ECHO "  \c"
                  echolog 'Please supply a directory.'
               fi
             done

             TEMP_DBFILE_DIR="$ORACLE_TARGET_DIR"

             #
             #  Now check to see if the basedir/SUNWsymon already exist, if so, then
             #  use this new augmented path to determine the available space.  The 
             #  purpose of this logic is to catch specific partition splits that occur
             #  along valid install directory tree.
             #

            trailslash=`echo $TEMP_DBFILE_DIR | /usr/bin/grep -i "/$" | /usr/bin/wc -l`

            if [ $trailslash = 0 ]
            then
                TMP_VAR_PATH="$TEMP_DBFILE_DIR/SUNWsymon"
            else
                TMP_VAR_PATH="${TEMP_DBFILE_DIR}SUNWsymon"
            fi

            if [ -d "$TMP_VAR_PATH" ]
            then
                getAvailableSpace "$TMP_VAR_PATH"
                getFileSystem "$TMP_VAR_PATH"
                fs_two=$FILESYSTEM_NAME 
            else
                getAvailableSpace "$TEMP_DBFILE_DIR"	
                getFileSystem "$TEMP_DBFILE_DIR"
                fs_two=$FILESYSTEM_NAME 
            fi

	      if [ "$fs_one" = "$fs_two" ]; then
		  REQUIRED_SPACE_SOFT=$REQUIRED_SPACE_TMP
		  REQUIRED_SPACE_DBF=$REQUIRED_SPACE_TMP
	      else
		  REQUIRED_SPACE_DBF=$REQUIRED_SPACE_TMP
	      fi

              AVAILABLE_SPACE_D=$AVAILABLE_SPACE

          else

             #
             # Since the tbl package already exists, just obtain relevant details
             # from pkginfo
             #


	      if [ "$RELOCATED_ROOT" != "" ]
	      then
		 TEMP_DBFILE_DIR=`$CMD_PKGINFO -R $RELOCATED_ROOT -r $each_pkg`
	      else
		 TEMP_DBFILE_DIR=`$CMD_PKGINFO -r $each_pkg`
	      fi
             getAvailableSpace "$RELOCATED_ROOT/$TEMP_DBFILE_DIR"
             REQUIRED_SPACE_DBF=0
             AVAILABLE_SPACE_D=$AVAILABLE_SPACE

          fi
     # end of is this a server layer install test
    fi

    # Java disk space check

    server_selected=`$CMD_ECHO $INSTALL_LAYERS | $CMD_GREP "LAYER.SERVER"`
    console_selected=`$CMD_ECHO $INSTALL_LAYERS | $CMD_GREP "LAYER.CONSOLE"`
      REQUIRED_SPACE=0
      AVAILABLE_SPACE=0
      required_space_for_java=0
      available_space_for_java=0

      if [ "$BUNDLED_JAVA_USED" = "1" ] ;then
         if [ "$server_selected" != "" -o "$console_selected" != "" ] ; then
            checkJava
              ret="$?"
              if [ $ret -ge 1 -a $ret -le 3 ]; then
                 install_os_patches_for_java
                 [ $? -eq 1 ] && quit # Error or no to patch installation
     
                 if [ "$INSTALL_TYPE" = "DE" ]; then
                    input="ds"
                 else
                    input="ps"
                 fi
                 calculate_required_space_for_java "$input"
                 calculate_available_space_for_java "$java_base_dir"
        
                 REQUIRED_SPACE=`$CMD_EXPR $required_space_for_java + $REQUIRED_SPACE`

                 getFileSystem "$java_base_dir"
                 fs_four=$FILESYSTEM_NAME 
    
                 #
                 #  Add proper logic to check filesystem and adjust the respective
                 #  required space vars.
                 #
             fi #fi for if [ $ret -ge 1 -a $ret -le 3 ]; then
          fi #fi for if [ "$server_selected" != "" -o "$console_selected" != "" ] ; then

       fi
    fi #fi for if [ "$INSTALL_TYPE" = "ADDON" -o "$INSTALL_TYPE" = "ADDON_ONLY" ]; then


    echologverbose ''
    echologverbose 'Disk Space Requirements:'
    echologverbose '$2' "========================"

    if [ "$fs_three" != "" ]; then
      echologverbose 'the corresponding filesystem for etc: $2' "$fs_three"
    else
      echologverbose 'the corresponding filesystem for etc: na'
    fi

    echologverbose 'Space required etc\\\(in $2\\\):  $3 blocks.' "$ETC" "$space_required_etc"
    echologverbose 'Space available etc\\\(in $2\\\): $3 blocks.' "$ETC" "$space_available_etc"

    if [ "$fs_one" = "$fs_two" -a $REQUIRED_SPACE_DBF -gt 0 -a $AVAILABLE_SPACE_D -gt 0 ]; then
       echologverbose 'the software proper and the dbfiles share the same filesystem, required_space_soft reflects this fact'
    fi

    if [ "$fs_one" != "" ]; then
      echologverbose 'the corresponding filesystem for soft: $2' "$fs_one"
    else
      echologverbose 'the corresponding filesystem for soft: na'
    fi

    echologverbose 'Space required soft\\\(in $2\\\):  $3 blocks.' "$TEMP_TARGET_DIR" "$REQUIRED_SPACE_SOFT"
    echologverbose 'Space available soft\\\(in $2\\\): $3 blocks.' "$TEMP_TARGET_DIR" "$AVAILABLE_SPACE_P"

    if [ "$fs_one" = "$fs_two" -a $REQUIRED_SPACE_DBF -gt 0 -a $AVAILABLE_SPACE_D -gt 0 ]; then
       echologverbose 'the software proper and the dbfiles share the same filesystem, required_space_dbf reflects this fact'
    fi

    if [ "$fs_two" != "" ]; then
      echologverbose 'the corresponding filesystem for dbf: $2' "$fs_two"
    else
      echologverbose 'the corresponding filesystem for dbf: na'
    fi

    echologverbose 'Space required dbf \\\(in $2\\\):  $3 blocks.' "$TEMP_DBFILE_DIR" "$REQUIRED_SPACE_DBF"
    echologverbose 'Space available dbf \\\(in $2\\\): $3 blocks.' "$TEMP_DBFILE_DIR" "$AVAILABLE_SPACE_D"

   if [ "$BUNDLED_JAVA_USED" = "1" ] ; then
      if [ "$fs_four" != "" ]; then
        echologverbose 'the corresponding filesystem for java: $2' "$fs_four"
      else
        echologverbose 'the corresponding filesystem for java: na'
      fi

      echologverbose 'Space required java \\\(in $2\\\):  $3 blocks.' "$java_root_dir$java_base_dir" "$required_space_for_java"
      echologverbose 'Space available java \\\(in $2\\\): $3 blocks.' "$java_root_dir$java_base_dir" "$available_space_for_java"
      echologverbose ''
   fi


    if [ $REQUIRED_SPACE -gt 0 -a $AVAILABLE_SPACE -gt 0 ]; then
      if [ $REQUIRED_SPACE -gt $AVAILABLE_SPACE ]; then
    echolog ''
    echolog 'Insufficient disk space to install the requested Sun Management Center components - require_space'
    echolog ''
    quit_program=1
      fi
    fi


    if [ $REQUIRED_SPACE_SOFT -gt 0 -a $AVAILABLE_SPACE_P -gt 0 ]; then
      if [ $REQUIRED_SPACE_SOFT -ge $AVAILABLE_SPACE_P ]; then
    echolog ''
    echolog 'Insufficient disk space to install the requested Sun Management Center components - required_space_soft'
    echolog ''
    quit_program=1
      fi
    fi

    if [ $REQUIRED_SPACE_DBF -gt 0 -a $AVAILABLE_SPACE_D -gt 0 ]; then
      if [ $REQUIRED_SPACE_DBF -ge $AVAILABLE_SPACE_D ]; then
    echolog ''
    echolog 'Insufficient disk space to install the requested Sun Management Center components - required_space_dbf'
    echolog ''
    quit_program=1
      fi
    fi


    if [ $quit_program -eq 1 ]; then
    echolog ''
    echolog 'Here are some possible fixes to alleviate the disk space problem:'
    $CMD_ECHO "  \c"
    echolog '  - Respecify the components to install so it takes less space'
    $CMD_ECHO "  \c"
    echolog '  - Uninstall some existing packages to make room for Sun Management Center'
    $CMD_ECHO "  \c"
    echolog '  - Make a soft link for $2 that points to more space' "$TARGET_DIR"
    $CMD_ECHO "  \c"
    echolog '  - Ask your system administrator for help'
    echolog ''
    quit
    fi

    echologverbose ''


   #
   #  Now remove the defined relocated root from the dbfile and etc parms
   #  We have to do this after the logic above which could have re-targeted
   #  the original values also to output just above.
   #

    if [ "$RELOCATED_ROOT" = "" -o "$RELOCATED_ROOT" = "/" ]; then
      TARGET_DIR_WITHOUT_RELOC=$TARGET_DIR
    else
      TEMP_DBFILE_DIR=`/usr/bin/echo $TEMP_DBFILE_DIR | sed -e "s,^$RELOCATED_ROOT,/,g"`
      TEMP_DBFILE_DIR=`/usr/bin/echo "$TEMP_DBFILE_DIR"  | /usr/bin/sed -e 's|//*|/|g'`
      ETC=`/usr/bin/echo $ETC | sed -e "s,^$RELOCATED_ROOT,/,g"`
      ETC=`/usr/bin/echo "$ETC"  | /usr/bin/sed -e 's|//*|/|g'`

      #
      #  Don't forget to update the TARGET_DIR_WITHOUT_RELOC with the posibly relocated
      #  value based on logic above. This will ensure that the call to installDB
      # has a correct value for the first parameter.
      #
      TARGET_DIR_WITHOUT_RELOC=`/usr/bin/echo $TEMP_TARGET_DIR | sed -e "s,^$RELOCATED_ROOT,/,g"`
      TARGET_DIR_WITHOUT_RELOC=`/usr/bin/echo "$TARGET_DIR_WITHOUT_RELOC"  | /usr/bin/sed -e 's|//*|/|g'`
    fi


}

# Set up package admin files
setAdminFiles() {

    idependvalue="quit"
    if [ $IGNOREDEPENDFILE -eq 1 ] ; then
        idependvalue="nocheck"
    fi
    DATE_FORMAT=`eval echo "+\\\%y\\\%m\\\%d\\\%H\\\%M\\\%S"`
    TIMESTAMP=`/usr/bin/date $DATE_FORMAT`
    DEFADMIN="${LOGDIR}/defadminfile.$TIMESTAMP.$$"
    $CMD_CAT << !EOF > "$DEFADMIN"
mail=
instance=overwrite
partial=nocheck
runlevel=nocheck
idepend="$idependvalue"
rdepend=nocheck
space=nocheck
setuid=nocheck
conflict=nochange
action=nocheck
basedir=default
!EOF

    DATE_FORMAT=`eval echo "+\\\%y\\\%m\\\%d\\\%H\\\%M\\\%S"`
    TIMESTAMP=`/usr/bin/date $DATE_FORMAT`
    ADMIN="${LOGDIR}/adminfile.$TIMESTAMP.$$"
    $CMD_TOUCH "$ADMIN"
}

# Prompt for a CD change if required
# $1 - the CD number
# $2 - the pack key
# $3 - the package name
checkCD() {
    tmp_cd=$1
    tmp_pack="$2"
    tmp_package="$3"
    [ $tmp_cd -eq $VAR_CURRENT_CD ] && return

    getI18NMessage "$tmp_pack"
    while [ 1 -eq 1 ]
    do
	echologverbose '\\\"$2\\\" resides on CD \\\#$3.' "$MSG" "$tmp_cd"
	if [ -d "${SRC_DIR}/../../disk2/image" ]; then # Default CD #2
	    answer=`getPathName "${SRC_DIR}/../../disk2/image"`
	    echologverbose 'New source directory: $2' "$answer"
	else
            cd /
            eject cd
	    if [ "$AUTOCONFIG_RUNFILE" != "" ] ; then # Run autoconfig
		tmp_auto=`getAutoConfig "CORE_DISK2"`
	    else
		tmp_auto="/cdrom/cdrom0/image"
	    fi
	    get_input_from_user 'Please insert the second CD or enter the source directory from disk 2 \\\[$2\\\]' "$tmp_auto"
	fi
        if [ -z "$answer" ] ; then
            answer="/cdrom/cdrom0/image"
        fi

	if [ "$AUTOCONFIG_SAVEFILE" ] ; then
	    saveAutoConfig "CORE_DISK2" "$answer"
	fi

	echologverbose 'New source directory: $2' "$answer"
        cd_count=0
        while [ 1 ]
        do
            sleep 1
            if [ -d "$answer" ] ; then
                break
            fi
            cd_count=`/usr/bin/expr $cd_count + 1`
            if [ $cd_count -ge 10 ] ; then
                echolog 'Invalid directory $2' "$answer"
                break
            fi
        done
	if [ ! -d "$answer" ]; then
	    $CMD_ECHO "  \c"
	    echolog 'Invalid directory.'
	    echolog ''
	    continue_confirm
	fi
	SRC_DIR="$answer"
        if [ "$CURRENT_INSTALL_TYPE" = "2.xADDONS" ] ; then
            break
        fi
	INSTALLATION_PROPERTY=`getI18NPropertyFile "${SRC_DIR}/Addons/${VAR_ADDON_PRODUCT}/Solaris_${OSVERS}"`
	INSTALLATION_INFO="${SRC_DIR}/Addons/${VAR_ADDON_PRODUCT}/Solaris_${OSVERS}/installation.info"
	getPackageDirectory "$tmp_pack" "$tmp_package"
	if [ $? -eq 0 ] || [ ! -d "${PACKAGE_DIR}/reloc" ]; then
	    $CMD_ECHO "  \c"
	    echolog 'Invalid directory.'
	    echolog ''
	    continue_confirm
	else
	    break
	fi
    done
    VAR_CURRENT_CD=$tmp_cd
 
}

# Install the packages in the list
installPackages() {
    echologverbose ''
    echologverbose 'Installing the components...'
    echologverbose ''

    SDK_PKGS=""
    for each_pkglist in $MASTER_INSTALL_COMPONENTS
    do
	tmp_pack=`$CMD_ECHO "$each_pkglist" | $CMD_AWK -F ':' '{ print $1 }'`
	tmp_cd=`$CMD_ECHO "$each_pkglist" | $CMD_AWK -F ':' '{ print $3 }'`

	each_pkglist=`$CMD_ECHO "$each_pkglist" | $CMD_AWK -F ':' '{ print $4 }'`
	for each_pkg in `$CMD_ECHO "$each_pkglist" | $CMD_AWK -F ',' '{ for (i = 1; i <= NF; i++) print $i }'`
	do
	    PKG_NAME="$each_pkg"

	    packageExists "$PKG_NAME"
	    [ $? -eq 1 ] && continue

	    getPackageDirectory "$tmp_pack" "$PKG_NAME"
	    if [ $? -eq 0 ] || [ ! -d "${PACKAGE_DIR}/reloc" ]; then
		checkCD "$tmp_cd" "$tmp_pack" "$PKG_NAME"
	    fi

	    each_pkg="$PACKAGE_DIR"
	    PKG_DIR=`$CMD_DIRNAME $PACKAGE_DIR`

	    confirm_value=`$CMD_PKGPARAM -f "${each_pkg}/pkginfo" ES_ADDON_CONFIRM`
	    if [ -n "$confirm_value" ]; then
		confirm_value=`$CMD_ECHO "$confirm_value" | $CMD_TR [A-Z] [a-z]`
		if [ "$confirm_value" = "true" ] ; then
		    confirmPackage "$PKG_NAME" "${each_pkg}/pkginfo"
		    [ $? -ne 0 ] && continue
		fi
	    fi

	    pkg_def_base=`$CMD_PKGPARAM -f "${each_pkg}/pkginfo" BASEDIR`

	    SDK_PKGS="$SDK_PKGS $PKG_NAME"

	    installPackage "$pkg_def_base" "$PKG_DIR" "$PKG_NAME"
	    packageExists "$PKG_NAME"
	    if [ $? -ne 1 ] ; then
		echolog ''
		echolog 'Error installing package: $2' "$PKG_NAME"
		echolog ''
		if [ "$INSTALL_TYPE" = "PE" -o "$INSTALL_TYPE" = "DE" ]; then
		    quit
		else
		    continue_confirm
		fi
	    fi
	done
    done

    echolog ''
}

# Installs a package
# $1 - pkg_def_base
# $2 - PKG_DIR
# $3 - PKG_NAME
installPackage() {
    /usr/bin/pkginfo $RELOC_PARAM -qi $3 > /dev/null 2>&1
    if [ $? -eq 0 ] ; then
        return
    fi
    idependvalue="quit"
    if [ $IGNOREDEPENDFILE -eq 1 ] ; then
        idependvalue="nocheck"
    fi
    if [ "$1" = "/" ]; then
	tmp_dir1=""
	tmp_dir2=""
    else
	tmp_dir1=`$CMD_EXPR "$1" : '\(/opt\).*'`
	tmp_dir2=`$CMD_EXPR "$1" : '/opt\(.*\)'`
    fi
    if [ "$tmp_dir1" = "/opt" ]; then
	if [ "$tmp_dir2" = "" ]; then
	    tmp_basedir="$TARGET_DIR_WITHOUT_RELOC"
	else
	    tmp_basedir="${TARGET_DIR_WITHOUT_RELOC}$tmp_dir2"
	fi

	$CMD_CAT << !EOF > "$ADMIN"
mail=
instance=overwrite
partial=nocheck
runlevel=nocheck
idepend="$idependvalue"
rdepend=nocheck
space=nocheck
setuid=nocheck
conflict=nochange
action=nocheck
basedir="$tmp_basedir"
!EOF
  	  $CMD_PKGADD -a $ADMIN -d $2 $RELOC_PARAM $3 2>&1 | $CMD_TEE -a $LOGFILE
    else
	  $CMD_PKGADD -a $DEFADMIN -d $2 $RELOC_PARAM $3 2>&1 | $CMD_TEE -a $LOGFILE
    fi
}

# Shows selection summary
showSummary() {
    echologverbose ''
    echologverbose '---------------------------------------------------------------------'
    $CMD_ECHO " \c"
    echologverbose ' Selection Summary'
    echologverbose '---------------------------------------------------------------------'
    echologverbose '$2' "$INSTALLATION_SUMMARY"
    echologverbose ''
    echologverbose '---------------------------------------------------------------------'
    [ "$AUTOCONFIG_RUNFILE" = "" ] && continue_confirmv # Not autoconfig
}

# Checks whether a product is valid for installation
# $1 - the top directory of an addon product
# Return: 1 if valid; 0 otherwise
checkAddonProduct() {
    INSTALLATION_PROPERTY=`getI18NPropertyFile "${1}/Solaris_${OSVERS}"`
    INSTALLATION_INFO="${1}/Solaris_${OSVERS}/installation.info"
    if [ ! -f "$INSTALLATION_INFO" ] || [ ! -f "$INSTALLATION_PROPERTY" ]; then
	return 0
    fi
    product_installable=0
    VAR_ADDON_PRODUCT=`$CMD_BASENAME $1`
    getPackList
    for pack_key in `$CMD_ECHO $PACK_LIST | $CMD_AWK -F ',' '{ for (i = 1; i <= NF; i++) print $i }'`
    do
	getComponentList "$pack_key"
	pack_installable=0
	for component_key in `$CMD_ECHO "$COMPONENT_LIST" | $CMD_AWK -F ',' '{ for (i = 1; i <= NF; i++) print $i }'`
	do
	    component_installable=0

            #
            #Get the layer information for this component, if does not support
            #installed layer then skip this component.
            #
	    getComponentInfo "$pack_key" "$component_key" "LAYER"
	    if [ "$COMPONENT_INFO" != "" ]; then 
		supports_installed_layer=0
		OLDIFS=$IFS
		IFS=,
		for each_layer in $INSTALL_LAYERS
		do
		    $CMD_ECHO "$COMPONENT_INFO" | $CMD_GREP "$each_layer" > /dev/null 2>&1
		    if [ $? -eq 0 ]; then
                        supports_installed_layer=1
			break
		    fi
		done
		IFS="$OLDIFS"
                if [ $supports_installed_layer -eq 0 ] ; then
                    continue
                fi
            fi

	    # Execute the specified installation script
	    getComponentInfo "$pack_key" "$component_key" "COMPONENT_SCRIPT"
	    if [ "$COMPONENT_INFO" != "" ]; then # Script exists
		getComponentScript "$pack_key" "$COMPONENT_INFO"
		if [ $? -eq 1 ]; then # Component script found
#Added the paranthesis to call the component scripts in 
#sub-shell for bug  4394793
		    (. $COMPONENT_SCRIPT_FILE
		    runComponentScript)
		    comp_script_retcode=$?
		    if [ $comp_script_retcode -eq 2 ] ; then
			#
			#Script returned 2, means skip this component.
			#
			if [ -z "$COMPONENTS_NOT_TO_BE_INSTALLED" ] ; then
			    COMPONENTS_NOT_TO_BE_INSTALLED="$VAR_ADDON_PRODUCT:$pack_key:$component_key"
			else
			    COMPONENTS_NOT_TO_BE_INSTALLED="$COMPONENTS_NOT_TO_BE_INSTALLED,$VAR_ADDON_PRODUCT:$pack_key:$component_key"
			fi
			continue
                    else
			if [ $comp_script_retcode -ne 1 ] ; then
			    #
			    #Script returned either 0 or something unknown. Dont install this product
			    #
			    return 0
			fi     
			#
			#Script returned 1, So proceed with normal processing.
			#
                    fi
		fi
	    fi
	    
	    getComponentInfo "$pack_key" "$component_key" "PACKAGES" "$LOCALE_CODE"
	    tmp_package_list="$COMPONENT_INFO"
	    for pkg in `$CMD_ECHO "$tmp_package_list" | $CMD_AWK -F ',' '{ for (i = 1; i <= NF; i++) print $i }'`
	    do
		getPackageDirectory "$pack_key" "$pkg"
		if [ $? -eq 1 ]; then
		    isAddonPackageInstallable "${PACKAGE_DIR}/pkginfo"
		    if [ $? -eq 1 ] ; then
			component_installable=1
			break
                    fi
		fi
	    done
	    if [ $component_installable -eq 0 ] ; then
		if [ -z "$COMPONENTS_NOT_TO_BE_INSTALLED" ] ; then
		    COMPONENTS_NOT_TO_BE_INSTALLED="$VAR_ADDON_PRODUCT:$pack_key:$component_key"
		else
		    COMPONENTS_NOT_TO_BE_INSTALLED="$COMPONENTS_NOT_TO_BE_INSTALLED,$VAR_ADDON_PRODUCT:$pack_key:$component_key"
		fi
            else
		pack_installable=1
	    fi
	done
	if [ $pack_installable -eq 1 ] ; then
	    product_installable=1
        fi
    done
    if [ $product_installable -eq 1 ] ; then
	return 1
    fi

    return 0
}

# Do SunMC addon installation
installAddons() {
    if [ "$INSTALL_TYPE" = "ADDON" ]; then # Don't install if already installed
	return
    fi

    # Don't install addons if the core product is not installed
    productExists "PE"
    if [ $? -eq 0 ]; then
	productExists "DE"
	[ $? -eq 0 ] && return
    fi

    INSTALL_TYPE="ADDON_ONLY"
    selectLayers
    [ "$INSTALL_LAYERS" = "" ] && return # Don't install if no layers are found


    DIR_LIST=`$CMD_LS "${SRC_DIR}/Addons"`
    PRODUCT_LIST_30=""
    # Check for SunMC 3.0 products
    for dir in $DIR_LIST
    do
	if [ "$dir" != "SunMC2.X" -a "$dir" != "Windows" ]; then # Not SunMC 2.0 or Windows addon products
	    checkAddonProduct "${SRC_DIR}/Addons/${dir}"
	    [ $? -eq 1 ] && PRODUCT_LIST_30="$PRODUCT_LIST_30 $dir"
	fi
    done

    # Check for SunMC 2.X addon products
    getOldAddonPackages
    if [ "$PRODUCT_LIST_30" != "" -o "$PRODUCT_LIST_2X" != "" ]; then # There is at least one product
	SELECTION_LINE="====================================================================="
	echolog ''
	echolog '$2' "$SELECTION_LINE"
	$CMD_ECHO "                \c"
	echolog '                Sun Management Center 3.0 Addons Product Selection:                 '
	echolog '$2' "$SELECTION_LINE"
	echolog ''
    fi

    # 3.0 addon product selection
    if [ "$PRODUCT_LIST_30" != "" ]; then
	for product in $PRODUCT_LIST_30
	do
	    VAR_ADDON_PRODUCT="$product"
	    INSTALLATION_INFO="${SRC_DIR}/Addons/${product}/Solaris_${OSVERS}/installation.info"
	    INSTALLATION_PROPERTY=`getI18NPropertyFile "${SRC_DIR}/Addons/${product}/Solaris_${OSVERS}"`
	    getProductKey
	    getI18NMessage "$PRODUCT_KEY"
	    getAskQuestion
	    if [ "$ASK_QUESTION" = "FALSE" ] ; then
		answer=1
	    else
		if [ "$AUTOCONFIG_RUNFILE" != "" ] ; then
		    tmp_auto=`getAutoConfig "$PRODUCT_KEY"`
                    if [ -z "$tmp_auto" ] ; then
                        answer=0
                    else
			answer="$tmp_auto"
                    fi
		else
		    $CMD_ECHO "  \c"
		    ask_user 'Do you want to install the product: $2?' "$MSG"
		fi
	    fi
	    if [ $answer -eq 1 ]; then
		if [ -z "$INSTALL_PRODUCTS" ]; then
		    INSTALL_PRODUCTS="${product}:$PRODUCT_KEY"
		else
		    INSTALL_PRODUCTS="${INSTALL_PRODUCTS},${product}:$PRODUCT_KEY"
		fi
		if [ "$AUTOCONFIG_SAVEFILE" != "" ] ; then # Save automated config
		    saveAutoConfig "$PRODUCT_KEY" "1"
                fi
            else
                if [ "$AUTOCONFIG_SAVEFILE" != "" ] ; then # Save automated config
                    saveAutoConfig "$PRODUCT_KEY" "0"
                fi
	    fi
	done
    fi

    # 2.X addon product selection
    if [ "$PRODUCT_LIST_2X" != "" ]; then # SunMC 2.X addons exist
	BACKUP_LIST="$PRODUCT_LIST_2X"
	PRODUCT_LIST_2X=""
	for product in $BACKUP_LIST
	do
	    param_prod=`$CMD_ECHO $product | $CMD_AWK -F ':' '{ print $1 }'`
	    param_pkglist=`$CMD_ECHO $product | $CMD_AWK -F ':' '{ print $2 }'`
	    $CMD_ECHO "  \c"
	    if [ "$AUTOCONFIG_RUNFILE" != "" ] ; then
		tmp_auto=`getAutoConfig "$param_prod"`
		if [ -z "$tmp_auto" ] ; then
		    answer=0
		else
		    answer="$tmp_auto"
		fi
	    else
		$CMD_ECHO "  \c"
		ask_user 'Do you want to install the product: $2?' "$param_prod"
	    fi
	    if [ $answer -eq 1 ]; then
		PRODUCT_LIST_2X="$PRODUCT_LIST_2X ${param_prod}:$param_pkglist"
		if [ "$AUTOCONFIG_SAVEFILE" != "" ] ; then # Save automated config
		    saveAutoConfig "$param_prod" "1"
                fi
            else
                if [ "$AUTOCONFIG_SAVEFILE" != "" ] ; then # Save automated config
                    saveAutoConfig "$param_prod" "0"
                fi
	    fi
	done
    fi

    # Install 3.0 addon products
    if [ "$PRODUCT_LIST_30" != "" ]; then
        #
        #A global variable to indicate the type of current installation
        #
        CURRENT_INSTALL_TYPE="3.0ADDONS"
	for product in `$CMD_ECHO $INSTALL_PRODUCTS | $CMD_AWK -F ',' '{ for (i = 1; i <= NF; i++) print $i }'`
	do
	    VAR_ADDON_PRODUCT=`$CMD_ECHO $product | $CMD_AWK -F ':' '{ print $1 }'`
	    PRODUCT_KEY=`$CMD_ECHO $product | $CMD_AWK -F ':' '{ print $2 }'`
	    INSTALLATION_PROPERTY=`getI18NPropertyFile "${SRC_DIR}/Addons/${VAR_ADDON_PRODUCT}/Solaris_${OSVERS}"`
	    INSTALLATION_INFO="${SRC_DIR}/Addons/${VAR_ADDON_PRODUCT}/Solaris_${OSVERS}/installation.info"
	    getProductKey
	    getI18NMessage "$PRODUCT_KEY"
	    echologverbose ''
	    echolog 'Installing the product: $2' "$MSG"
	    echologverbose ''
	    installProduct
	    copyLocaleFiles "${SRC_DIR}/Addons/${VAR_ADDON_PRODUCT}/Solaris_${OSVERS}"
	done
	echolog ''
    fi

    # Install 2.X addon products
    if [ "$PRODUCT_LIST_2X" != "" ]; then
        CURRENT_INSTALL_TYPE="2.xADDONS"
	INSTALL_TYPE="ADDON_ONLY"
	echolog ''
	for product in $PRODUCT_LIST_2X
	do
	    PACKAGE_LIST=`$CMD_ECHO $product | $CMD_AWK -F ':' '{ print $2 }'`
	    product=`$CMD_ECHO $product | $CMD_AWK -F ':' '{ print $1 }'`
	    for package in `$CMD_ECHO $PACKAGE_LIST | $CMD_AWK -F ',' '{ for (i = 1; i <= NF; i++) print $i }'`
	    do
		packageExists "$package"
		if [ $? -eq 1 ] ; then
		    $CMD_ECHO "  \c"
		    echolog 'Package $2 is already installed on this system.' "$package"
		    echolog ""
		    continue
		fi

		PKG_DIR="${SRC_DIR}/Addons/SunMC2.X/Solaris_${OSVERS}/$product"
		if [ ! -d "${PKG_DIR}/${package}/reloc" ]; then
		    checkCD "2" "$product" "$PKG_NAME"
		    PKG_DIR="${SRC_DIR}/Addons/SunMC2.X/Solaris_${OSVERS}/$product"
		fi

		pkginfo="${PKG_DIR}/${package}/pkginfo"
		if [ ! -f "$pkginfo" ]; then # Common directory
		    PKG_DIR="${SRC_DIR}/Addons/SunMC2.X/Common/$product"
		    pkginfo="${PKG_DIR}/${package}/pkginfo"
		    if [ ! -f "$pkginfo" ]; then # Package not found
			echolog ''
			echolog 'Cannot find pkginfo file for package: $2' "$package"
			quit
		    fi
		fi

		confirm_value=`$CMD_PKGPARAM -f "$pkginfo" ES_ADDON_CONFIRM`
		if [ -n "$confirm_value" ]; then
		    confirm_value=`$CMD_ECHO "$confirm_value" | $CMD_TR [A-Z] [a-z]`
		    if [ "$confirm_value" = "true" ] ; then
			confirmPackage "$package" "$pkginfo"
			[ $? -ne 0 ] && continue
		    fi
		fi

                pkg_def_base=`$CMD_PKGPARAM -f "$pkginfo" BASEDIR`
		installPackage "$pkg_def_base" "$PKG_DIR" "$package"
		packageExists "$package"
		if [ $? -ne 1 ] ; then
		    echolog ''
		    $CMD_ECHO "  \c"
		    echolog 'Error installing package: $2' "$package"
		    echolog ''
		    continue_confirm
		fi
	    done

	    copyLocaleFiles "${SRC_DIR}/Addons/SunMC2.X/Solaris_${OSVERS}/$product"
	done
    fi
}

# Compare package version (for SunMC 2.1 addon packages)
# $1 - minimum version number of the package
# $2 - the version number of the current install
# Return: 0 = compare successful
compareVersions() {
    # We need to compare the minimum version (PKGVER) with the actual version (SYMVER).
    # This is a little tricky. We have to make some assumptions about the form of the
    # Symon version number. Assume it is of the form X.X[.X].
    # We already know that the versions are not equal

    #  Break up the version number into major version (X.X) and anything after that
    pkg_maj=`$CMD_EXPR "$1" : '\(...\).*'`
    sym_maj=`$CMD_EXPR "$2" : '\(...\).*'`
    if [ "$pkg_maj" != "$sym_maj" ]; then
	first_maj=`echo "$pkg_maj\n$sym_maj" | $CMD_SORT | $CMD_HEAD -1`
	[ "$first_maj" = "$pkg_maj" ] && return 0
	return 1
    fi

    # The major numbers must be equal
    pkg_min=`$CMD_EXPR "$1" : '....\(.\)'`
    sym_min=`$CMD_EXPR "$2" : '....\(.\)'`
    [ -z "$pkg_min" ] && [ -n "$sym_min" ] && return 0
    [ -n "$pkg_min" ] && [ -z "$sym_min" ] && return 1
    if [ -n "$pkg_min" ] && [ -n "$sym_min" ]; then
        first_min=`echo "$pkg_min\n$sym_min" | $CMD_SORT | $CMD_HEAD -1`
	[ "$first_min" = "$pkg_min" ] && return 0
	return 1
    fi

    return 1
}

# Verify that the package can be installed
# $1 = package name
# $2 = file name
# Return: 0 = confirmed (install)
confirmPackage() {
    ES_ADDON_PLATFORM_MATCH=""
    PLATFORMLIST=`$CMD_PKGPARAM -f "$2" ES_ADDON_PLATFORM`
    if [ "$PLATFORMLIST" != "" ]; then
        OLDIFS=$IFS
        IFS=:
        for each_platform in $PLATFORMLIST
        do
            $CMD_ECHO "$PRTOUT" | $CMD_GREP "$each_platform" > /dev/null 2>&1
            if [ $? -eq 0 ]; then
                ES_ADDON_PLATFORM_MATCH="$each_platform"
                break
            fi
        done
        IFS="$OLDIFS"
    fi

    PLATFORM_FILE="${VAROPTDIR}/platform/platform.prop"
    PLATFORM_DIR=`$CMD_DIRNAME $PLATFORM_FILE`
    [ ! -d $PLATFORM_DIR ] && $CMD_MKDIR -p $PLATFORM_DIR

    FAMILY=`$CMD_PKGPARAM -f $2 ES_ADDON_FAMILY`

    # Check if the platform file exists, and check to see if it matches
    # the package we are working with.
    if [ -n "$ES_ADDON_PLATFORM_MATCH" ] && [ -n "$FAMILY" ] && \
	[ -n "$PLATFORM_FILE" ] && [ -f "$PLATFORM_FILE" ]; then
	file_plat=`$CMD_GREP '^name' "$PLATFORM_FILE" | $CMD_CUT -f2 -d=`
	file_family=`$CMD_GREP '^family' "$PLATFORM_FILE" | $CMD_CUT -f2 -d=`
	[ "$file_plat" = "$ES_ADDON_PLATFORM_MATCH" ] && [ "$file_family" = "$FAMILY" ] && return 0
	return 1
    fi

    #  Either the platform file does not exist, or it does not
    #  match, so ask the user what he wants to do!
    $CMD_ECHO "  \c"
    echolog 'Please verify the platform for package: $2' "$1"
    echolog ''
    $CMD_ECHO "    \c"
    echolog 'Found: $2' "$PRTOUT"

    # Test to make sure the platform list was specified in the pkginfo file.
    if [ -n "$ES_ADDON_PLATFORM_MATCH" ]; then
	$CMD_ECHO "    \c"
	if [ -n "$FAMILY" ] ; then
	    echolog 'Supports: $2 - $3' "$ES_ADDON_PLATFORM_MATCH" "$FAMILY"
	else
	    echolog 'Supports: $2' "$ES_ADDON_PLATFORM_MATCH"
	fi
    fi

    name=`$CMD_PKGPARAM -f $2 NAME`
    $CMD_ECHO "    \c"
    echolog 'Name: $2' "$name"
    desc=`$CMD_PKGPARAM -f $2 DESC`
    $CMD_ECHO "    \c"
    echolog 'Description: $2' "$desc"
    echolog ''

    $CMD_ECHO "  \c"
    ask_user "Do you want to install this package now?"
    if [ $answer -eq 0 ] ; then
	$CMD_ECHO "    \c"
	echolog 'Skipping package $2' "$pkgname"
	echolog ""
	return 1
    fi

    # Save the platform/family information for others to use later
    if [ -n "$ES_ADDON_PLATFORM_MATCH" ] && [ -n "$FAMILY" ] ; then
	echo "name=$ES_ADDON_PLATFORM_MATCH" > "$PLATFORM_FILE"
	echo "family=$FAMILY" >> "$PLATFORM_FILE"
    fi

    return 0
}

# Checks whether an addon package can be installed
# $1 - full pathname of a pkginfo file
# Return: 1 if it is installable; 0 otherwise
isAddonPackageInstallable() {
    pkginfo="$1"

    if [ "$RELOCATED_ROOT" = "" -o "$RELOCATED_ROOT" = "/" ]; then
	PRTOUT=`$CMD_UNAME -i`
	if [ -f /usr/platform/"$PRTOUT"/sbin/prtdiag ]; then
	    PRTOUT=`/usr/platform/"$PRTOUT"/sbin/prtdiag | $CMD_HEAD -1 | $CMD_CUT -f2 -d:`
	fi
    else
	PRTOUT="$RELOC_PLATFORM"
    fi

    SYMVER=`$CMD_PKGPARAM $RELOC_PARAM SUNWescom SUNW_PRODVERS`
    if [ -n "$SYMVER" ]; then
	echo "$SYMVER" | $CMD_GREP "_" > /dev/null 2>&1 # Check to see if it has an underscore
	[ $? -eq 0 ] && SYMVER=`$CMD_EXPR "$SYMVER" : '\(.*\)_'` # Remove all after the underscore
    fi

    #  Make sure the CATEGORY of this package includes symon
    category_value=`$CMD_PKGPARAM -f "$pkginfo" CATEGORY`
    if [ "$category_value" != "" ]; then
	$CMD_ECHO $category_value | $CMD_GREP -i symon > /dev/null
	[ $? -ne 0 ] && return 0
    fi

    # Skip if the package is not on the selected layers
    addon_comp=`$CMD_PKGPARAM -f "$pkginfo" ES_ADDON_COMPONENT`
    if [ "$addon_comp" != "" ]; then
	case $addon_comp in
	    console) TEST=`$CMD_ECHO $INSTALL_LAYERS | $CMD_GREP "LAYER.CONSOLE"`
		     [ "$TEST" = "" ] && return 0
		     ;;
	    server)  TEST=`$CMD_ECHO $INSTALL_LAYERS | $CMD_GREP "LAYER.SERVER"`
		     [ "$TEST" = "" ] && return 0
		     ;;
	    agent)   TEST=`$CMD_ECHO $INSTALL_LAYERS | $CMD_GREP "LAYER.AGENT"`
		     [ "$TEST" = "" ] && return 0
		     ;;
	esac
    fi

    # If this value is blank or matches the current platform, display
    # addon for user selection, otherwise skip it.
    PLATFORMLIST=`$CMD_PKGPARAM -f "$pkginfo" ES_ADDON_PLATFORM`
    if [ "$PLATFORMLIST" != "" ]; then
	tmp_platform_match=""
	OLDIFS=$IFS 
	IFS=: 
	for each_platform in $PLATFORMLIST
	do
	    $CMD_ECHO "$PRTOUT" | $CMD_GREP "$each_platform" > /dev/null 2>&1
	    if [ $? -eq 0 ]; then
		tmp_platform_match="$each_platform"
		break
	    fi
	done
	IFS="$OLDIFS"
	[ "$tmp_platform_match" = "" ] && return 0
    fi

    # If any of the packages are not present on the host, skip installation of this package.
    PKGDEP=`$CMD_PKGPARAM -f "$pkginfo" ES_ADDON_PACKAGE_DEPENDENCY`
    for pkg in $PKGDEP
    do
	packageExists "$pkg"
	[ $? -ne 1 ] && return 0
    done

    # If any of the directories are not present on the host, skip installation of this package.
    PKGDIRDEP=`$CMD_PKGPARAM -f "$pkginfo" ES_ADDON_DIRECTORY_DEPENDENCY`
    skip=0
    for dir in $PKGDIRDEP
    do
	[ $skip -eq 1 ] && break # File not found
	[ ! -d "${RELOCATED_ROOT}/$dir" ] && skip=1 && break # Dir not found

	# If any of the files are not present on the host, skip installation of this package.
	PKGFILEDEP=`$CMD_PKGPARAM -f "$pkginfo" ES_ADDON_FILE_DEPENDENCY`
	for file in $PKGFILEDEP
	do
	    [ ! -f "${RELOCATED_ROOT}/${dir}/$file" ] && skip=1 && break
	done
    done
    [ $skip -eq 1 ] && return 0

    PKGVER=`$CMD_PKGPARAM -f "$pkginfo" ES_ADDON_VERSION`
    if [ "$PKGVER" != "" ]; then
	if [ "$PKGVER" != "$SYMVER" ]; then # If the versions match then we are OK
	    compareVersions "$PKGVER" "$SYMVER"
	    [ $? -ne 0 ] && return 0
	fi
    fi

    return 1
}

# Gets a list of installable addon packages
# Return: $PRODUCT_LIST_2X
getOldAddonPackages() {
    PRODUCT_LIST_2X=""

    # Check for existence of SunMC 2.X products
    tmp_dir="${SRC_DIR}/Addons/SunMC2.X/Solaris_${OSVERS}"
    tmp_common_dir="${SRC_DIR}/Addons/SunMC2.X/Common"
    if [ ! -d "$tmp_dir" ] && [ ! -d "$tmp_common_dir" ]; then
	return 0
    fi

    for product in `$CMD_LS $tmp_dir`
    do
	PACKAGE_LIST=""
	for package in `$CMD_LS "${tmp_dir}/$product"`
	do
	    [ "$package" = "locale" ] && continue # Skip locale directory

	    pkginfo="${tmp_dir}/${product}/${package}/pkginfo"
	    if [ ! -f "$pkginfo" ]; then
		pkginfo="${tmp_common_dir}/${product}/${package}/pkginfo"
		[ ! -f "$pkginfo" ] && continue
	    fi

	    isAddonPackageInstallable "$pkginfo"
	    if [ $? -eq 1 ]; then
		if [ "$PACKAGE_LIST" = "" ]; then
		    PACKAGE_LIST="$package"
		else
		    PACKAGE_LIST="${PACKAGE_LIST},$package"
		fi
	    fi
	done

	[ "$PACKAGE_LIST" != "" ] && PRODUCT_LIST_2X="$PRODUCT_LIST_2X ${product}:$PACKAGE_LIST"
    done

    return 1
}

# Check OS version
# $1 - supplied OSVERS (for -R option)
checkOSVersion() {
    if [ -z "$1" ]; then
	OSVERS=`$CMD_UNAME -r`
    else
	OSVERS="$1"
    fi

    case $OSVERS in
	5.5.1*) OSVERS=2.5.1 ;;
	5.6*)   OSVERS=2.6   ;;
	5.7*)   OSVERS=7     ;;
	5.8*)   OSVERS=8     ;;
	5.9*)   OSVERS=9     ;;
	*)      echolog ''
		echolog 'Unsupported OS version: $2' "$OSVERS"
	        quit         ;;
    esac
}

# Install products
installProduct() {
    selectLayers
    if [ "$INSTALL_LAYERS" != "" ]; then
        #
        #  Perform an early check to ensure that the make executable is present
        #  in the /usr/ccs/bin directory.  If not then inform user an exit.
        #
	TEST=`$CMD_ECHO $INSTALL_LAYERS | $CMD_GREP "LAYER.SERVER"`
	if [ "$TEST" != "" ]; then

		  tmp_usr_ccs_bin=/usr/ccs/bin

          tmp_make="$RELOCATED_ROOT${tmp_usr_ccs_bin}/make"
          tmp_ld="$RELOCATED_ROOT${tmp_usr_ccs_bin}/ld"
          tmp_ar="$RELOCATED_ROOT${tmp_usr_ccs_bin}/ar"

          if [ ! -f "$tmp_make" ] ; then
             echolog ""
             echolog "The Sun Management Center server installation requires that the make executable"
             echolog "be present on the host machine.  In particular it looks for this file in the"
             echolog "/usr/ccs/bin directory.  Please install the SUNWsprot package before executing"
             echolog "the es-inst script again."
             echolog ""
             exit 1
          fi

          if [ ! -f "$tmp_ld" ] ; then
             echolog ""
             echolog "The Sun Management Center server installation requires that the ld executable"
             echolog "be present on the host machine.  In particular it looks for this file in the"
             echolog "/usr/ccs/bin directory.  Please install the SUNWtoo package before executing"
             echolog "the es-inst script again."
             echolog ""
             exit 1
          fi

          if [ ! -f "$tmp_ar" ] ; then
             echolog ""
             echolog "The Sun Management Center server installation requires that the ar executable"
             echolog "be present on the host machine.  In particular it looks for this file in the"
             echolog "/usr/ccs/bin directory.  Please install the SUNWbtool package before executing"
             echolog "the es-inst script again."
             echolog ""
             exit 1
          fi

        fi

	selectPacks
	selectComponents
	if [ "$MASTER_INSTALL_COMPONENTS" != "" ]; then
	    showSummary
	    [ "$INSTALL_TYPE" = "PE" -o "$INSTALL_TYPE" = "DE" ] && CheckIfJavaInstallRequired
	    checkDiskSpace
	    if [ "$INSTALL_TYPE" = "PE" -o "$INSTALL_TYPE" = "DE" ]; then
	        installJava
		TEST=`$CMD_ECHO $INSTALL_LAYERS | $CMD_GREP "LAYER.SERVER"`
		if [ "$TEST" != "" ]; then
		    db_cmd=`$CMD_ECHO "$0" | $CMD_SED -e "s%es-inst%es-db%"`
		    if [ -f "$db_cmd" ]; then
			. "$db_cmd"
			#[ "$ORACLE_TARGET_DIR" = "" ] && ORACLE_TARGET_DIR="$ORIGINAL_TARGET_DIR"
			installDB "${SRC_DIR}/db" "$TARGET_DIR_WITHOUT_RELOC" "$TEMP_DBFILE_DIR" "$RELOCATED_ROOT"
			[ $? -eq 0 ] && quit # Oracle installation failed
		    fi
		fi
	    fi
            
	    installPackages

	    if [ "$INSTALL_TYPE" = "PE" -o "$INSTALL_TYPE" = "DE" ]; then

	       [ "$INSTALL_TYPE" = "PE" ] && CFGINFOFILE_SETTING="PRODUCTION"
	       [ "$INSTALL_TYPE" = "DE" ] && CFGINFOFILE_SETTING="DEVELOPMENT"

               put_var_in_cfgfile "$CFGINFOFILE" "SYSTEM" "$CFGINFOFILE_SETTING" "="
               put_var_in_cfgfile "$CFGINFOFILE" "VERSION" "3.0" "="
	       #$CMD_ECHO "$CFGINFOFILE_SETTING" >> "$CFGINFOFILE"
	       #$CMD_ECHO "VERSION=3.0" >> "$CFGINFOFILE"
               if [ $cfgfile_present -eq 0 ] ; then
		   $CMD_MV -f "$CFGINFOFILE" "$TARGET_DIR/SUNWsymon/sbin/.cfginfo"
               fi
	    fi
	else
	    echolog ''
	    $CMD_ECHO "    \c"
	    echolog 'No components to install.'
	    echolog ''
	fi
    fi
}

# Uninstall
# $1 - localized component name
uninstall() {
    echolog "A previous version of Sun Management Center exists."
    if [ "$RELOCATED_ROOT" != "" ]; then
	echolog 'Please uninstall it locally in \\\"$2\\\".' "$RELOCATED_ROOT"
	quit
    fi

    # ask user if they wish to proceed
    #

	echolog ''
    ask_user "Would you like to uninstall it now to proceed with the upgrade?"
    if [ $answer -ne 1 ]; then
      quit
    fi

	[ ! -d "${VAROPTDIR}/2x" ] && $CMD_MKDIR -p "${VAROPTDIR}/2x"

    keepData="false"
    if [ -d ${VAROPTDIR}/db -o -d ${VAROPTDIR}/cfg ] ; then
        preserve_data_answered=0
        while [ $preserve_data_answered -eq 0 ]; do
            echo ""
            ask_user "Would you like to migrate your previous data?"
            do_it=$answer
    
            if [ $do_it -eq 1 ] ; then
                echo ""
                preserve_data_answered=1
                keepData="true"
            else
                echo ""
                echolog 'This will remove all files under $2. If you have any custom' "${VAROPTDIR}"
                echolog 'scripts under this directory, please move them to an alternate location'
                echolog 'before proceeding.'
                echo ""
    
                ask_user "Do you wish to continue with the removal of the existing data"
                remove_it=$answer
    
                if [ $remove_it -eq 0 ] ; then
                    preserve_data_answered=0
                else
                    preserve_data_answered=1
			    	keepData="false"
			    	
                    if [ -h ${VAROPTDIR} ]; then
                        /usr/bin/rm -rf ${VAROPTDIR} >/dev/null 2>&1
                    fi

                    if [ -d ${VAROPTDIR} ]; then
                        /usr/bin/rm -rf ${VAROPTDIR} >/dev/null 2>&1
                    fi

                    echo ""
                fi
            fi
        done
	fi

    #
    # Perform the db-export if the raima db subdir exists
    # and relevent data files are present
    #
    # Ensure that the raima db directories are present and at least 
    # one topo data file (entities.dat) is also present
    #

    if [ -d ${VAROPTDIR}/db -a -d ${VAROPTDIR}/db/topology -a -d ${VAROPTDIR}/db/eventmgr -a -f ${VAROPTDIR}/db/topology/entities.dat ] ; then
        if [ "$keepData" = "true" ] ; then
  	        # Raima-to-Oracle export
	        db_export_cmd=`$CMD_ECHO "$0" | $CMD_SED -e "s%es-inst%export2x/db-export.sh%"`
	        if [ ! -f "$db_export_cmd" ]; then
	    	    echolog 'Cannot find $2' "$db_export_cmd"
	    	    quit
	        fi
	        "$db_export_cmd"
	        echolog ''
        fi
	fi


    # Uninstall
    tmp_basedir=`$CMD_PKGPARAM SUNWescom BASEDIR`
    uninst_cmd="$tmp_basedir/SUNWsymon/sbin/es-uninst"
    "$uninst_cmd"


	if [ "$keepData" = "true" ] ; then
        # If cfg dir exists, move it to 2x/cfg
        [ -d "${VAROPTDIR}/cfg" ] && $CMD_MV -f "${VAROPTDIR}/cfg" "${VAROPTDIR}/2x" > /dev/null 2>&1
	fi

    # es-uninst might have removed the ESROOT directory.
    # Check for install directory once more.
    INSTALL_DIR="$TARGET_DIR"
    [ -d "$INSTALL_DIR/SUNWsymon" ] && INSTALL_DIR="$INSTALL_DIR/SUNWsymon"

    # Check to make sure we can write to $INSTALL_DIR and also issue
    # chown commands there.
    TEST_FILE="$INSTALL_DIR/test_file$$"
    $CMD_TOUCH "$TEST_FILE" >/dev/null 2>&1
    touch_bad=$?
    $CMD_CHOWN root "$TEST_FILE" >/dev/null 2>&1
    chown_bad=$?

    if [ $touch_bad -eq 1 ] || [ ! -f "$TEST_FILE" ] || [ $chown_bad -eq 1 ]; then
	echolog ""
	$CMD_ECHO "  \c"
	echolog "There appear to be some permission problems with the installation"
	$CMD_ECHO "  \c"
	echolog 'directory: $2' "$INSTALL_DIR"
	$CMD_LS -l "$TEST_FILE" >> "$LOGFILE"
	echolog ""
	$CMD_ECHO "  \c"
	echolog "In order to install Sun Management Center, root must be able to write to the"
	$CMD_ECHO "  \c"
	echolog "installation directory and own the files in that directory."
	$CMD_ECHO "  \c"
	echolog "Please check your configuration and try again."
	$CMD_RM -f "$TEST_FILE" >/dev/null 2>&1
	quit
    else
	$CMD_RM -f "$TEST_FILE" >/dev/null 2>&1
    fi
}

# ProcessOldSunMCData
# $1 - localized component name
processOldSunMCData() {
    echolog "A previous version of Sun Management Center Data exists."

    # init some working vars

    if [ "$RELOCATED_ROOT" != "" ]
    then
       tmp_var1="$RELOCATED_ROOT${VAROPTDIR}/db"
       tmp_var2="$RELOCATED_ROOT${VAROPTDIR}/cfg"
       tmp_var3="$RELOCATED_ROOT${VAROPTDIR}/2x"
    else
       tmp_var1="${VAROPTDIR}/db"
       tmp_var2="${VAROPTDIR}/cfg"
       tmp_var3="${VAROPTDIR}/2x"
    fi

    # ask user if they wish to proceed
    #

    preserve_data_answered=0
    while [ $preserve_data_answered -eq 0 ]; do
        echo ""
        ask_user "Would you like to migrate your previous data?"
        do_it=$answer

        if [ $do_it -eq 1 ] ; then
            echo ""
            preserve_data_answered=1
        else
            echo ""
            echolog 'This will remove all files under $2. If you have any custom' "${VAROPTDIR}"
            echolog 'scripts under this directory, please move them to an alternate location'
            echolog 'before proceeding.'
            echo ""

            ask_user "Do you wish to continue with the removal of the existing data"
            remove_it=$answer

            if [ $remove_it -eq 0 ] ; then
                preserve_data_answered=0
            else
                preserve_data_answered=1
				
                if [ -h "${VAROPTDIR}" ]; then
                    /usr/bin/rm -rf "${VAROPTDIR}" >/dev/null 2>&1
                fi

                if [ -d "${VAROPTDIR}" ]; then
                    /usr/bin/rm -rf "${VAROPTDIR}" >/dev/null 2>&1
                fi

                echo ""
                return

             fi
        fi

    done

    [ ! -d "$tmp_var3" ] && $CMD_MKDIR -p "$tmp_var3"

    #
    # Perform the db-export if the raima db subdir exists
    # and relevent data files are present
    #
    # Ensure that the raima db directories are present and at least 
    # one topo data file (entities.dat) is also present
    #

    if [ -d $tmp_var1 -a -d $tmp_var1/topology -a -d $tmp_var1/eventmgr -a -f $tmp_var1/topology/entities.dat ]
    then
       # Raima-to-Oracle export
	   db_export_cmd=`$CMD_ECHO "$0" | $CMD_SED -e "s%es-inst%export2x/db-export.sh%"`
	   if [ ! -f "$db_export_cmd" ]; then
           echolog 'Cannot find $2' "$db_export_cmd"
           quit
	   fi
	   "$db_export_cmd"
    fi

    # If cfg dir exists, move it to 2x/cfg
    [ -d "$tmp_var2" ] && $CMD_MV -f "$tmp_var2" "$tmp_var3" > /dev/null 2>&1

    # es-uninst might have removed the ESROOT directory.
    # Check for install directory once more.
    INSTALL_DIR="$TARGET_DIR"
    [ -d "$INSTALL_DIR/SUNWsymon" ] && INSTALL_DIR="$INSTALL_DIR/SUNWsymon"

    # Check to make sure we can write to $INSTALL_DIR and also issue
    # chown commands there.
    TEST_FILE="$INSTALL_DIR/test_file$$"
    $CMD_TOUCH "$TEST_FILE" >/dev/null 2>&1
    touch_bad=$?
    $CMD_CHOWN root "$TEST_FILE" >/dev/null 2>&1
    chown_bad=$?

    if [ $touch_bad -eq 1 ] || [ ! -f "$TEST_FILE" ] || [ $chown_bad -eq 1 ]; then
	echolog ""
	$CMD_ECHO "  \c"
	echolog "There appear to be some permission problems with the installation"
	$CMD_ECHO "  \c"
	echolog 'directory: $2' "$INSTALL_DIR"
	$CMD_LS -l "$TEST_FILE" >> "$LOGFILE"
	echolog ""
	$CMD_ECHO "  \c"
	echolog "In order to install Sun Management Center, root must be able to write to the"
	$CMD_ECHO "  \c"
	echolog "installation directory and own the files in that directory."
	$CMD_ECHO "  \c"
	echolog "Please check your configuration and try again."
	$CMD_RM -f "$TEST_FILE" >/dev/null 2>&1
	quit
    else
	$CMD_RM -f "$TEST_FILE" >/dev/null 2>&1
    fi
}

# Copy locale files
# $1 - Directory above locale
copyLocaleFiles() {
    DIR="${1}/locale/"
    if [ -d "$DIR" ]; then
	$CMD_CP -rf $DIR $VAROPTDIR > /dev/null 2>&1
	if [ $? -ne 0 ]; then
	    echolog ''
	    echolog 'Error copying the locale files to: $2' "$VAROPTDIR"
	    echolog 'Setup script will use English.'
	    echolog ''
	fi
    fi
}

# Run
setupSunMC() {
    productExists "PE"
    if [ $? -eq 0 ]; then # PE does not exists
	productExists "DE"
	[ $? -eq 0 ] && return # DE does not exists
    fi

    echolog ''
    if [ "$RELOCATED_ROOT" = "" -o "$RELOCATED_ROOT" = "/" ]; then
	ask_user 'Do you want to run setup now?'
	[ $answer -eq 0 -o $answer -eq 2 ] && quit;

	if [ "$ESROOT" = "" ]; then
	    set_basedir
	    if [ "$ESROOT" = "" ]; then
		echolog ''
	        echolog 'ESROOT is not set.'
		quit
	    fi
         fi

	echolog ''
	if [ $VERBOSE = "ON" ]; then
 	  $ESROOT/sbin/es-setup -v
        else
          $ESROOT/sbin/es-setup
        fi
    else
	echolog 'Please run setup locally in \\\"$2\\\".' "$RELOCATED_ROOT"
	echolog ''
    fi
}

# Clean up
cleanUp() {
    if [ "$ADMIN" != "" ]; then
	$CMD_ECHO "Admin File Contents:" >> "$LOGFILE"
	$CMD_CAT "$ADMIN" >> "$LOGFILE"
	$CMD_RM -f "$ADMIN" > /dev/null 2>&1
	$CMD_RM -f "$DEFADMIN" > /dev/null 2>&1
    fi

    [ "$TEMP_INFO" != "" ] && $CMD_RM -f "$TEMP_INFO" > /dev/null 2>&1
    [ "$TEMP_PROPERTY" != "" ] && $CMD_RM -f "$TEMP_PROPERTY" > /dev/null 2>&1
    [ "$AUTOCONFIG_RUNFILE" != "" ] && $CMD_RM -f "$AUTOCONFIG_RUNFILE" > /dev/null 2>&1
}

# Gets the internationalized version of installation.properties, if any
# $1 = the directory containing the property file
# Return: <$1>/installation[_<locale>].properties
getI18NPropertyFile() {
    tmp_l10n="$L10N_CODE"
    [ "$tmp_l10n" = "C" ] && tmp_l10n="en"
    tmp="${1}/installation_${tmp_l10n}.properties"
    if [ -f "$tmp" ]; then
	echo "$tmp"
	return
    else
	tmp="${1}/installation.properties"
	echo "$tmp"
	return
    fi
}

get_locale_dir () 
{
     locale_directory=""
     osversion=`/usr/bin/uname -r`
     case $osversion in
	  5.5.1*) osversion=2.5.1 ;;
	  5.6*)   osversion=2.6   ;;
	  5.7*)   osversion=7     ;;
	  5.8*)   osversion=8     ;;
	  5.9*)   osversion=9     ;;
	  *)      echolog ''
	       echolog 'Unsupported OS version: $2' "$osversion"
	       exit 1           ;;
     esac
     curr_wd=`/usr/bin/pwd`
     if [ "$SRC_DIR" != "" ] ; then
         #
         #Either called from CD (copied to tmp area.), or es-inst
         #called with -S option from installed place.
         #In either case, its OK to set locale directory to
         #$SRC_DIR/PE/Solaris$osversion/locale. In case of addon
         #only installation where SRC_DIR will be present but not PE,
         #we will set TEXTDOMAINDIR to /var/opt/SUNWsymon/locale after
         #finding that the locale_directory is not present.
         #

         cd "$SRC_DIR" > /dev/null 2>&1
         if [ $? -eq 0 ] ; then
            locale_directory="`/usr/bin/pwd`/PE/Solaris_$osversion/locale"
	    cd $curr_wd
            return
         else 
             echolog 'Invalid source directory $2' "$SRC_DIR"
             exit 1
         fi
     fi

     #
     #Either called from disk image or installed place. If called from
     #installed place then we should use already installed message objects.
     #Else we should use the locale directory from disk image.
     #

     #
     #scriptdirname is set at the begining of the script.
     #
     cd $scriptdirname
     l_dir=`/usr/bin/pwd`
     echo "$l_dir" | /usr/bin/grep "SUNWsymon/sbin"  > /dev/null 2>&1
     if [ $? -eq 0 ] ; then
        #
        #installed place
        #
        locale_directory="/var/opt/SUNWsymon/locale"
	cd $curr_wd
	return 
     fi

     #
     #Called from disk image and we should be in sbin directory.
     #
     
     cd ../image
     locale_directory="`/usr/bin/pwd`/PE/Solaris_$osversion/locale"
     cd $curr_wd
     return 

}

#
# Add symbolic links from higher version to Solaris 8.
#
add_os_links() {
    if [ "${OSVERS}" = "9" ] ; then
        LINKTO="sparc-sun-solaris2.8"
        LINKFROM="sparc-sun-solaris2.${OSVERS}"
        #osdirs=`find "$BASEDIR" -name "$LINKTO" -type d -print`
        osdirs="${TARGET_DIR_WITH_RELOC}/SUNWsymon/base/bin ${TARGET_DIR_WITH_RELOC}/SUNWsymon/base/lib ${TARGET_DIR_WITH_RELOC}/SUNWsymon/base/sbin ${TARGET_DIR_WITH_RELOC}/SUNWsymon/util/bin ${TARGET_DIR_WITH_RELOC}/SUNWsymon/util/lib ${TARGET_DIR_WITH_RELOC}/SUNWsymon/util/sbin"
        curdir=`pwd`
        for each_dir in $osdirs
        do
            if [ ! -d "$each_dir" ] ; then
                /usr/bin/mkdir -p "$each_dir"
            fi
            cd "$each_dir"
            if [ -h "$LINKFROM" ] ; then
                continue
            fi
            if [ -d "$LINKFROM" ] ; then
                continue
            fi
            #echo "linking $LINKTO $LINKFROM in $each_dir"
            /usr/bin/ln -s "$LINKTO" "$LINKFROM"
        done
        cd $curdir
    fi
}


#########################################################################
#           Main starts here                                            #   
#########################################################################

# Commands
CMD_AWK=/usr/bin/nawk
CMD_BASENAME=/usr/bin/basename
CMD_CAT=/usr/bin/cat
CMD_CHMOD=/usr/bin/chmod
CMD_CHOWN=/usr/bin/chown
CMD_CLEAR=/usr/bin/clear
CMD_CP=/usr/bin/cp
CMD_CUT=/usr/bin/cut
CMD_DATE=/usr/bin/date
CMD_DF=/usr/bin/df
CMD_DIRNAME=/usr/bin/dirname
CMD_ECHO=/usr/bin/echo
CMD_EXPR=/usr/bin/expr
CMD_GREP=/usr/bin/egrep
CMD_HEAD=/usr/bin/head
CMD_LS=/usr/bin/ls
CMD_MKDIR=/usr/bin/mkdir
CMD_MV=/usr/bin/mv
CMD_PKGADD=/usr/sbin/pkgadd
CMD_PKGINFO=/usr/bin/pkginfo
CMD_PKGPARAM=/usr/bin/pkgparam
CMD_PWD=/usr/bin/pwd
CMD_RM=/usr/bin/rm
CMD_SED=/usr/bin/sed
CMD_SORT=/usr/bin/sort
CMD_SLEEP=/usr/bin/sleep
CMD_TAIL=/usr/bin/tail
CMD_TEE=/usr/bin/tee
CMD_TOUCH=/usr/bin/touch
CMD_TR=/usr/bin/tr
CMD_UNAME=/usr/bin/uname

# Global variables
CFGINFOFILE_SETTING=""
EOL=$
INSTALLATION_SUMMARY=""
INSTALL_COMPONENTS=""
INSTALL_COMPONENTS_LOG=""
INSTALL_PACKAGES=""
INSTALLATION_INFO=""
INSTALLATION_PROPERTIES=""
LOGFILE=""
ADMIN=""
DEFADMIN=""
INSTALL_TYPE=""
SRC_DIR=""
ORIGINAL_TARGET_DIR=""
TARGET_DIR=""
ORACLE_TARGET_DIR=""
ROOT=""
RELOCATED_ROOT=""
RELOC_PARAM=""
RELOC_PLATFORM=""
ETC=""
INSTALL_JAVA=0
AUTOCONFIG_SAVEFILE=""
AUTOCONFIG_RUNFILE=""
VAR_ADDON_PRODUCT=""
VAR_CURRENT_CD=-1
VERBOSE=OFF
CURRENT_INSTALL_TYPE=""
COMPONENTS_NOT_TO_BE_INSTALLED=""
AGENT_ONLY=0
BUNDLED_JAVA_USED=0
IGNOREDEPENDFILE=0

#Added the following line for Bug 4394070
PROGNAME=$0;

# Load external commands
prog_base=`$CMD_BASENAME $0`
common_cmd=`$CMD_ECHO $0 | $CMD_SED s/$prog_base/es-common.sh/`
# For console, server or SDK only
java_cmd=`$CMD_ECHO $0 | $CMD_SED s/$prog_base/install-java.sh/`
# For console or server only
patch_cmd=`$CMD_ECHO $0 | $CMD_SED s/$prog_base/install-patch.sh/`

postinstallcommand=`$CMD_ECHO $0 | $CMD_SED s/$prog_base/sunmc-postinstall.sh/`

. $common_cmd
. $java_cmd
. $patch_cmd

if [ -f "$postinstallcommand" ] ; then
    . $postinstallcommand
fi

# Get command line parameters
has_S=0
has_T=0
while getopts L:R:S:T:C:A:vaw ARG
do
    temp_dir=`getPathName $OPTARG`
    case $ARG in
	L)  
	    LANG="$OPTARG"
            export LANG
	    ;;
	R)  [ "$OPTARG" = "/" ] && continue
	    RELOCATED_ROOT="$temp_dir"
	    if [ ! -d "$RELOCATED_ROOT" ]; then
		$CMD_ECHO ''
		$CMD_ECHO "Invalid directory: $RELOCATED_ROOT"
		quit
	    fi
	    RELOC_PARAM="-R $RELOCATED_ROOT"
	    ;;
	S)  SRC_DIR="$temp_dir"
	    if [ ! -d "$SRC_DIR" ]; then
		$CMD_ECHO ''
		$CMD_ECHO "Invalid directory: $SRC_DIR"
                SRC_DIR=""
		#quit
            else
		has_S=1
	    fi
	    ;;
	T)  ORIGINAL_TARGET_DIR="$temp_dir"
	    TARGET_DIR="$temp_dir"
	    has_T=1
	    ;;
	C)  AUTOCONFIG_SAVEFILE="$temp_dir"
	    ;;
	A)  AUTOCONFIG_RUNFILE="$temp_dir"
	    ;;
	a)  AGENT_ONLY=1
	    ;;
	v)  VERBOSE=ON
	    ;;
	w)  IGNOREDEPENDFILE=1
	    ;;
    esac
done

# Common settings
set_commondirs "$RELOCATED_ROOT"

check_for_l10n
LOCALE_CODE="$L10N_CODE"
[ "$LOCALE_CODE" = "en" ] && LOCALE_CODE="C"

get_locale_dir
setup_textdomain
check_root
check_logfile "install"

#CFGINFOFILE="/tmp/.cfginfo"
#[ ! -d "/tmp" ] && $CMD_MKDIR -p "/tmp"
#[ -f "$CFGINFOFILE" ] && $CMD_RM -f "$CFGINFOFILE"
#$CMD_TOUCH "$CFGINFOFILE"

ROOT="/"
ETC="/etc"
if [ "$RELOCATED_ROOT" != "" ]; then
    # Check RELOCATED_ROOT
    #ROOT=`getPathName $ROOT`
    #ETC=`getPathName $ETC`
    #if [ ! -d "$ROOT" -o ! -d "$ETC" ]; then
	#echolog ''
	#echolog 'Invalid directory: $2' "$RELOCATED_ROOT"
	#quit
    #fi

    [ "$TARGET_DIR" != "" ] && TARGET_DIR=`getRelocPathName $TARGET_DIR`
fi

if [ -n "$AUTOCONFIG_SAVEFILE" -o -n "$AUTOCONFIG_RUNFILE" ]; then
    if [ -n "$AUTOCONFIG_SAVEFILE" ]; then # Save automated installation
	AUTOCONFIG_RUNFILE=""
	[ -f "$AUTOCONFIG_SAVEFILE" ] && $CMD_RM "$AUTOCONFIG_SAVEFILE"
    else # Run automated installation
	if [ ! -f "$AUTOCONFIG_RUNFILE" ]; then
	    echolog ''
	    echolog 'Invalid file: $2' "$AUTOCONFIG_RUNFILE"
	    quit
	else
	    FILENAME=`$CMD_BASENAME $AUTOCONFIG_RUNFILE`
	    $CMD_CP $AUTOCONFIG_RUNFILE $VAROPTDIR/install/$FILENAME
	    AUTOCONFIG_RUNFILE="${VAROPTDIR}/install/$FILENAME"
	fi
    fi

#    if [ $has_S -ne 1 -o $has_T -ne 1 ]; then
#	echolog ''
#	echolog 'Both -S and -T options must be specified'
#	quit
#    fi
fi

# Prints the introductory screen
#$CMD_CLEAR
#SCREEN_LINE="====================================================================="
#echolog ''
#echolog '$2' "$SCREEN_LINE"
#$CMD_ECHO "             \c"
#echolog '             Welcome to Sun Management Center Installer              '
#echolog '$2' "$SCREEN_LINE"
#$CMD_ECHO "  \c"
#echolog '  Sun  Management  Center  software  is  an open, extensible system  '
#$CMD_ECHO "  \c"
#echolog '  monitoring and management solution that uses  Java \\\(TM\\\)  software'
#$CMD_ECHO "  \c"
#echolog '  protocol and Simple Network Management Protocol \\\(SNMP\\\) to provide'
#$CMD_ECHO "  \c"
#echolog '  an integrated and comprehensive enterprise wide management of Sun'
#$CMD_ECHO "  \c"
#echolog '  products and their subsystems, components, and peripheral devices.'
#echolog ''
#echolog 'This script will help you to install the Sun Management Center software.'
#echolog ''

# Prints the introductory screen
$CMD_CLEAR
echolog ''
echolog 'This script will help you to install the Sun Management Center software.'
echolog ''
oldSunMCExists
if [ $? -eq 1 ] ; then
    uninstall # SunMC 2.1 exists
else
    #
    # added call to process 2.1 data when software
    # has already been uninstalled 
    #
    oldSunMCDataOnlyExists
    [ $? -eq 1 ] && processOldSunMCData # SunMC 2.1 data only exists
fi

RELOC_PLATFORM=`$CMD_UNAME -i`
if [ -f /usr/platform/"$RELOC_PLATFORM"/sbin/prtdiag ]; then
    RELOC_PLATFORM=`/usr/platform/"$RELOC_PLATFORM"/sbin/prtdiag | $CMD_HEAD -1 | $CMD_CUT -f2 -d:`
fi



# Check OSVERS
if [ "$RELOCATED_ROOT" = "" ]; then
    checkOSVersion
else
    OSVERS=`$CMD_UNAME -r`
    echolog ''
    echolog 'For installing Sun Management Center on $2, we need to know the' "$RELOCATED_ROOT" 
    echolog 'operating system on $2. This should be the output of the command' "$RELOCATED_ROOT"
    echolog '/usr/bin/uname -r. The command must be executed on a machine booted with'
    echolog 'disk mounted on $2' "$RELOCATED_ROOT"
    echolog ''
    get_input_from_user 'Please enter the OS version for \\\"$2\\\" \\\[$3\\\]: ' "$RELOCATED_ROOT" "$OSVERS"
    [ "$answer" != "" ] && OSVERS="$answer"
    case $OSVERS in
	5.5.1*) OSVERS=5.5.1 ;;
	2.5*)   OSVERS=5.5.1 ;;
	5)      OSVERS=5.5.1 ;;
	5.6*)   OSVERS=5.6   ;;
	2.6*)   OSVERS=5.6   ;;
	6)      OSVERS=5.6   ;;
	5.7*)   OSVERS=5.7   ;;
	2.7*)   OSVERS=5.7   ;;
	7)      OSVERS=5.7   ;;
	5.8*)   OSVERS=5.8   ;;
	2.8*)   OSVERS=5.8   ;;
	8)      OSVERS=5.8   ;;
	5.9*)   OSVERS=5.9   ;;
	2.9*)   OSVERS=5.9   ;;
	9)      OSVERS=5.9   ;;
	*)      echolog ''
		echolog 'Unsupported OS version: $2' "$OSVERS"
	        quit         ;;
    esac
    checkOSVersion "$OSVERS"
    echolog ''
    echolog 'For installing Sun Management Center on $2, we need to know the' "$RELOCATED_ROOT" 
    echolog 'machine type of machine whose root filesystem is or will be mounted on $2.' "$RELOCATED_ROOT"
    echolog 'This should be the output of the following command'
    echolog '    /usr/platform/PLATFORM/sbin/prtdiag \\\| /usr/bin/head -1 \\\| /usr/bin/cut -f2 -d:'
    echolog '    where PLATFORM is the output of /usr/bin/uname -i'
    echolog 'Please note that the above commands must be executed on the target machine.'
    echolog ''

    #RELOC_PLATFORM=`$CMD_UNAME -i`
    get_input_from_user 'Please enter the platform for \\\"$2\\\" \\\[$3\\\]: ' "$RELOCATED_ROOT" "$RELOC_PLATFORM"
    [ "$answer" != "" ] && RELOC_PLATFORM="$answer"
    echolog ''
fi

PRTOUT="$RELOC_PLATFORM"
export PRTOUT
#
#ensure umask to 022
#

umask 022

/usr/bin/mkdir -p "$RELOCATED_ROOT/var/opt/SUNWsymon/install/"
/usr/bin/locale > "$RELOCATED_ROOT"/var/opt/SUNWsymon/install/env_file
echo "OS_VERSION=$OSVERS" >> "$RELOCATED_ROOT"/var/opt/SUNWsymon/install/env_file
echo "OS_DIR=Solaris_$OSVERS" >> "$RELOCATED_ROOT"/var/opt/SUNWsymon/install/env_file


getSourceDirectory

#
#Please dont remove this variable. It is used by patch installation
#
ORIGINAL_SRC_DIR=$SRC_DIR

if [ "$RELOCATED_ROOT" != "" -a "$RELOCATED_ROOT" != "/" ] ; then
    getTargetDirectoryWithReloc
else
    getTargetDirectory
fi

TARGET_DIR_WITHOUT_RELOC="$ORIGINAL_TARGET_DIR"
TARGET_DIR_WITH_RELOC="$TARGET_DIR"

cfgfile_present=0
if [ -f "$TARGET_DIR_WITH_RELOC/SUNWsymon/sbin/.cfginfo" ] ; then
    CFGINFOFILE="$TARGET_DIR_WITH_RELOC/SUNWsymon/sbin/.cfginfo"
    cfgfile_present=1
else
    CFGINFOFILE="/tmp/.cfginfo"
    [ ! -d "/tmp" ] && $CMD_MKDIR -p "/tmp"
    [ -f "$CFGINFOFILE" ] && $CMD_RM -f "$CFGINFOFILE"
    $CMD_TOUCH "$CFGINFOFILE"
    cfgfile_present=0
fi

add_os_links

setAdminFiles
installSunMC
installAddons

#
#Code to install the patches
#

sunmc_postinstall "$SRC_DIR"

setupSunMC

echolog ''
echolog 'Log file: $2' "$LOGFILE"
echolog ''
echolog "$SCREEN_LINE"
echo    "                        \c"
echolog "End of Installation"
echolog "$SCREEN_LINE"
echolog ''
cleanUp
