#!/bin/sh
# Copyright 10/24/02 Sun Microsystems, Inc. All Rights Reserved.
#pragma ident  "@(#)es-setup	1.109 02/10/24 Sun Microsystems"

smcdb_configuration() {
    TMPCRONTAB=/tmp/crontab$$

    if [ -f ${TMPCRONTAB} ]; then
        rm -f ${TMPCRONTAB} 
    fi

    # is the crontab file populated for this user?

	/usr/bin/crontab -l 1>/dev/null 2>/dev/null

	if [ $? -eq 0 ]; then
        # if removal of every entry in the crontab file that references the
        # generate-analyze-table script would make the crontab file empty, then remove
        # the crontab file outright
        if [ -z "`/usr/bin/crontab -l | /usr/bin/grep -v generate-analyze-table`" ]; then
            /usr/bin/crontab -r
        else
            # remove any crontab entry added by Sun MC, and leave behind any entries made
            # from elsewhere
            /usr/bin/crontab -l | /usr/bin/grep -v generate-analyze-table > $TMPCRONTAB
        fi
    fi
    # add the Sun MC crontab entry.  It should not produce any standard output or standard error
    # and should not generate an e-mail message
    cat >> $TMPCRONTAB << EOF
0 0 * * 0-6 cd $BASEDIR/sbin;$BASEDIR/sbin/generate-analyze-table.sh 1>/dev/null 2>/dev/null
EOF
    cat $TMPCRONTAB | /usr/bin/crontab

    # remove temporary crontab file
	/usr/bin/rm -f $TMPCRONTAB

    #
    # Make sure an oratab file exists on this system
    #
    ORATABLOC=/var/opt/oracle
    ORATAB=${ORATABLOC}/oratab
    TMPORATB=/var/tmp/oratab$$


    if [ ! -s ${ORATAB} ];then
      if [ ! -d ${ORATABLOC} ];then
        /usr/bin/mkdir -p ${ORATABLOC}
      fi

      /usr/bin/cat <<!>> ${ORATAB}
#
!

    fi

    #
    # If an entry exists in $ORATAB for this $SID and this $ORACLE_HOME,
    # don't do anything.
    #

    FOUND=`/usr/bin/grep "^${ORACLE_SID}:${ORACLE_HOME}:" ${ORATAB}`
    if [ -z "${FOUND}" ];then

        #
        # If there is an old entry with no sid and same oracle home,
        # that entry will be marked as a comment.
        #

        FOUND_OLD=`/usr/bin/grep "^*:${ORACLE_HOME}:" ${ORATAB}`
        if [ -n "${FOUND_OLD}" ];then
            /usr/bin/sed -e "s?^.*:$ORACLE_HOME:?# *:$ORACLE_HOME:?" $ORATAB > $TMPORATB
            /usr/bin/cat $TMPORATB > $ORATAB
            /usr/bin/rm -f $TMPORATB 2>/dev/null
        fi
        /usr/bin/cat <<!>> ${ORATAB}
${ORACLE_SID}:$ORACLE_HOME:N
!

    #else

      #echo "Entry already exists in the ${ORATAB} file for this"|tee -a $LOG
      #echo "ORACLE_SID and ORACLE_HOME.  No changes made to file."|tee -a $LOG
    fi



    #
    # reset the configuration files for SunMC DB
    #
    listener_file="$ORACLE_HOME/network/admin/listener.ora"
    tns_file="$ORACLE_HOME/network/admin/tnsnames.ora"
    init_file="$ORACLE_HOME/admin/SunMC30/pfile/initSunMC30.ora"
    trace_file="$ORACLE_HOME/admin/SunMC30/udump/trace.txt"

    /usr/bin/chmod +w $listener_file $tns_file $init_file
    sid_name="SMCDBPROC"
    #
    # configure the trace.txt which controls generation of stored procedure
    # tracing from the trace package (see package-body-trace.sql)
    #
    if [ -f "$trace_file" ]; then
        /usr/bin/chmod +w $trace_file
    fi
    echo "off" > $trace_file

    if [ -f "$listener_file" -a -f "$tns_file" -a -f "$init_file" ]; then

        #
        # configure the listener file
        #
        echo "# LISTENER.ORA Configuration File:$listener_file
# Generated by SunMC DB Installation.

SID_LIST_SMCDBLISTENER =
  (SID_LIST =
    (SID_DESC =
      (SID_NAME = PLS$sid_name)
      (ORACLE_HOME = $ORACLE_HOME)
      (PROGRAM = extproc)
    )
    (SID_DESC =
      (GLOBAL_DBNAME = SunMC30)
      (ORACLE_HOME = $ORACLE_HOME)
      (SID_NAME = SunMC30)
    )
  )

SMCDBLISTENER =
  (DESCRIPTION_LIST =
    (DESCRIPTION =
      (ADDRESS_LIST =
        (ADDRESS = (PROTOCOL = TCP)(HOST = $SMCDB_HOST)(PORT = $SMCDB_PORT))
      )
      (ADDRESS_LIST =
        (ADDRESS = (PROTOCOL = IPC)(KEY = $sid_name))
      )
    )
  )" > $listener_file
        [ $? -ne 0 ] && return "1"

        #
        # configure tns service file
        #
        echo "# TNSNAMES.ORA Configuration File:$tns_file
# Generated by SunMC DB Installation.

SUNMC30 =
  (DESCRIPTION =
    (ADDRESS_LIST =
      (ADDRESS = (PROTOCOL = TCP)(HOST = $SMCDB_HOST)(PORT = $SMCDB_PORT))
    )
    (CONNECT_DATA =
      (SERVICE_NAME = SunMC30)
    )
  )

EXTPROC_CONNECTION_DATA =
  (DESCRIPTION =
    (ADDRESS_LIST =
      (ADDRESS = (PROTOCOL = IPC)(KEY = $sid_name))
    )
    (CONNECT_DATA =
      (SID = PLS$sid_name)
      (PRESENTATION = RO)
    )
  )" > $tns_file

        [ $? -ne 0 ] && return "1"

        #
        # configure database initialization file
        #
        echo "\n#
#
# initSunMC30.ora for SunMC Oracle
#
# Copyright (c) 06/22/00 Sun Microsystems, Inc. All Rights Reserved.
#

db_name = \"SunMC30\"
instance_name = SunMC30

service_names = SunMC30

control_files = (\"/var/opt/SUNWsymon/SunMCDB02/oradata/SunMC30/control01.ctl\", \"/var/opt/SUNWsymon/SunMCDB02/oradata/SunMC30/control02.ctl\", \"/var/opt/SUNWsymon/SunMCDB02/oradata/SunMC30/control03.ctl\")

open_cursors = 100
max_enabled_roles = 30
# db_file_multiblock_read_count = 16

db_block_buffers = 2048

shared_pool_size = 8M
shared_pool_reserved_size = 1M

large_pool_size = 48M
java_pool_size = 0

log_checkpoint_interval = 10000
log_checkpoint_timeout = 1800

processes = 115

log_buffer = 163840

# audit_trail = false  # if you want auditing
# timed_statistics = false  # if you want timed statistics
# max_dump_file_size = 10000  # limit trace file size to 5M each


# define directories to store trace and alert files
background_dump_dest = $ORACLE_HOME/admin/SunMC30/bdump
core_dump_dest = $ORACLE_HOME/admin/SunMC30/cdump
user_dump_dest = $ORACLE_HOME/admin/SunMC30/udump
utl_file_dir=$ORACLE_HOME/admin/SunMC30/udump

db_block_size = 8192

remote_login_passwordfile = exclusive

os_authent_prefix = \"\"

# The following parameters are needed for the Advanced Replication Option
#job_queue_processes = 4
#job_queue_interval = 60
#distributed_transactions = 10
#open_links = 4

# Uncomment the following line when your listener is configured for SSL
# (listener.ora and sqlnet.ora)
#

compatible = \"8.1.0\"
sort_area_size = 65536
sort_area_retained_size = 65536



# indicate explicit local listener for SunMC DB
#
#
# using multi-thread mode
#
#
#
MTS_DISPATCHERS = \"(PROTOCOL=TCP)\"
LOCAL_LISTENER = \"(ADDRESS_LIST = (ADDRESS = (PROTOCOL = IPC)(KEY = $sid_name))(ADDRESS=(PROTOCOL=TCP)(PORT=$SMCDB_PORT)(HOST=$SMCDB_HOST)))\"" > $init_file

        [ $? -ne 0 ] && return "1"
    else
        return "1"
    fi

    /usr/bin/chmod -w $listener_file $tns_file $init_file
    return "0"

}


smcdb_check_port_number() {
#
#  $1 = port number
#

    port_used="0"
    service_used="0"

    /usr/bin/netstat -an -P tcp | /usr/bin/grep "\*\.$1 " >/dev/null 2>&1

	if [ $? -eq 0 ]; then
		port_used="1"
		return 1
	fi

    /usr/bin/grep -w "$1/tcp" /etc/services >/dev/null 2>&1

	if [ $? -eq 0 ]; then
		service_used="1"
		return 1
	fi

    return 0
}



remove_dat_files() {

    if [ -d $VARDIR ] ; then
		/usr/bin/rm -f $VARDIR/discovery*.dat
		/usr/bin/rm -f $VARDIR/discovery*.x
		/usr/bin/rm -rf $VARDIR/discovery
		/usr/bin/rm -f $VARDIR/topology+view*.dat
		/usr/bin/rm -f $VARDIR/topology-modules-d.dat
		/usr/bin/rm -f $VARDIR/cfgserver-modules-d.dat
		/usr/bin/rm -f $VARDIR/event-modules-d.dat
		/usr/bin/rm -f $VARDIR/metadata-modules-d.dat
		/usr/bin/rm -f $VARDIR/trap-modules-d.dat
	fi
}


smcdb_check_memory_available() {
    #
    # get the phisical memory size of this machine
    #
    MEMSIZE=`/usr/sbin/prtconf | grep "Memory size: " | cut -f3 -d' '`

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

    MEMSIZE=`/usr/bin/expr $MEMSIZE \* 1024 \* 1024 / 2`
}

# Given a HEX or Octal number, returns decimal equivalant.
# 
make_radix_10()
{
  radix=`/usr/bin/echo $1 | /usr/bin/cut -c 1-2 2> /dev/null  `
  [ "$radix" = "0x" -o "$radix" = "0X" ] && temp_no=`/usr/bin/echo $1 | /usr/bin/sed "s/$radix//"` && /usr/bin/echo "{ibase=16;x=$temp_no;x}" | /usr/bin/bc && return 0
  radix=`/usr/bin/echo $1 | /usr/bin/cut -c 1-1 2> /dev/null `
  [ "$radix" = "0" -o "$radix" = "O" ] && temp_no=`/usr/bin/echo $1 | /usr/bin/sed "s/$radix//"` && /usr/bin/echo "{ibase=8;x=$temp_no;x}" | /usr/bin/bc && return 0
  /usr/bin/echo $1 
  return 0
}


smcdb_check_system() {
#
# set sharing memory for oracle in /etc/system
#
    echologverbose "Checking /etc/system file..."
    echologverbose ""


    SYSTEM_TEMP=/tmp/system.tmp1.$$
    TEMP_FILE=/tmp/system.tmp2.$$


    # Bugfix - 4620473 This is MUST to remove old copies / links - else is a security hole
    # 
    /usr/bin/rm -f $SYSTEM_TEMP
    /usr/bin/rm -f $TEMP_FILE
    /usr/bin/rm -f /tmp/system # In case left by old run of unpatched setup script 


    /usr/bin/cp /etc/system $SYSTEM_TEMP


    DETECT_SUNMC_MODS=`grep "This file has been modified by Sun MC 3.0 setup" $SYSTEM_TEMP`

    #if [ -n "$DETECT_SUNMC_MODS" ]; then
    #   echologverbose "/etc/system file matches Sun MC 3.0 requirements."
    #   return "0"
    #fi


    SHMMAX=`/usr/bin/grep "set shmsys:shminfo_shmmax" "$SYSTEM_TEMP" | /usr/bin/grep -v "[ ]*\*" | /usr/bin/cut -f2 -d'='`
    SHMMIN=`/usr/bin/grep "set shmsys:shminfo_shmmin" "$SYSTEM_TEMP" | /usr/bin/grep -v "[ ]*\*" | /usr/bin/cut -f2 -d'='`
    SHMMNI=`/usr/bin/grep "set shmsys:shminfo_shmmni" "$SYSTEM_TEMP" | /usr/bin/grep -v "[ ]*\*" | /usr/bin/cut -f2 -d'='`
    SHMSEG=`/usr/bin/grep "set shmsys:shminfo_shmseg" "$SYSTEM_TEMP" | /usr/bin/grep -v "[ ]*\*" | /usr/bin/cut -f2 -d'='`
    SEMMNI=`/usr/bin/grep "set semsys:seminfo_semmni" "$SYSTEM_TEMP" | /usr/bin/grep -v "[ ]*\*" | /usr/bin/cut -f2 -d'='`
    SEMMSL=`/usr/bin/grep "set semsys:seminfo_semmsl" "$SYSTEM_TEMP" | /usr/bin/grep -v "[ ]*\*" | /usr/bin/cut -f2 -d'='`
    SEMMNS=`/usr/bin/grep "set semsys:seminfo_semmns" "$SYSTEM_TEMP" | /usr/bin/grep -v "[ ]*\*" | /usr/bin/cut -f2 -d'='`
    SEMOPM=`/usr/bin/grep "set semsys:seminfo_semopm" "$SYSTEM_TEMP" | /usr/bin/grep -v "[ ]*\*" | /usr/bin/cut -f2 -d'='`
    SEMVMX=`/usr/bin/grep "set semsys:seminfo_semvmx" "$SYSTEM_TEMP" | /usr/bin/grep -v "[ ]*\*" | /usr/bin/cut -f2 -d'='`


    # In case of GNOME, /etc/system contains hex entries
    #
    SHMMAX=`make_radix_10 $SHMMAX ` 
    SHMMIN=`make_radix_10 $SHMMIN `
    SHMMNI=`make_radix_10 $SHMMNI `
    SHMSEG=`make_radix_10 $SHMSEG `
    SEMMNI=`make_radix_10 $SEMMNI `
    SEMMSL=`make_radix_10 $SEMMSL `
    SEMMNS=`make_radix_10 $SEMMNS `
    SEMOPM=`make_radix_10 $SEMOPM `
    SEMVMX=`make_radix_10 $SEMVMX `

    SHMMIN_REQ="1"

    if [ -n "$SHMMNI" -a -z "$DETECT_SUNMC_MODS" -o -n "$SHMMNI" -a "$SHMMNI" -lt 100 ]; then
        SHMMNI_REQ=`echo "{x=100 + $SHMMNI; x }" | /usr/bin/bc`
    else
        SHMMNI_REQ="100"
    fi

    SHMSEG_REQ="10"

    if [ -n "$SEMMNI" -a -z "$DETECT_SUNMC_MODS" -o -n "$SEMMNI" -a "$SEMMNI" -lt 100 ]; then
        SEMMNI_REQ=`echo "{x=100 + $SEMMNI; x }" | /usr/bin/bc`
    else
        SEMMNI_REQ="100"
    fi

    if [ -n "$SEMMSL" -a -z "$DETECT_SUNMC_MODS" -o -n "$SEMMSL" -a "$SEMMSL" -lt 115 ]; then
        SEMMSL_REQ=`echo "{x=115 + $SEMMSL; x }" | /usr/bin/bc`
    else
        SEMMSL_REQ="115"
    fi

    if [ -n "$SEMMNS" -a -z "$DETECT_SUNMC_MODS" -o -n "$SEMMNS" -a "$SEMMNS" -lt 200 ]; then
        SEMMNS_REQ=`echo "{x=200 + $SEMMNS; x }" | /usr/bin/bc`
    else
        SEMMNS_REQ="200"
    fi

    SEMOPM_REQ="100"
    SEMVMX_REQ="32767"

    SYSTEM_NEEDCHANGE="0"

    if [ $? -ne 0 ]; then
        return "1"
    fi

    # Note that the first comment line is grepped above.
    # See variable DETECT_SUNMC_MODS.
    if [ -z "$DETECT_SUNMC_MODS" ]; then
        echo "****************************************************" >> $SYSTEM_TEMP
        echo "* This file has been modified by Sun MC 3.0 setup. *" >> $SYSTEM_TEMP
        echo "*     DO NOT REMOVE THESE TWO COMMENTS LINES.      *" >> $SYSTEM_TEMP
        echo "****************************************************" >> $SYSTEM_TEMP
        SYSTEM_NEEDCHANGE="1"
    fi


    if [ -z "$SHMMAX" ]; then
        SYSTEM_NEEDCHANGE="1"
        echo "set shmsys:shminfo_shmmax=$MEMSIZE" >> $SYSTEM_TEMP
    else
        tmpvar=`echo "{x=0; if ( $SHMMAX < $MEMSIZE ) x=1; x}" | /usr/bin/bc`
        if [ $tmpvar -eq 1 ]; then
            repl_cmd=`echo "s/\\(set shmsys:shminfo_shmmax\\).*/\\1=$MEMSIZE/"`
            /usr/bin/cat $SYSTEM_TEMP | /usr/bin/sed -e "$repl_cmd" > $TEMP_FILE
            if [ $? -eq 0 ]; then
                SYSTEM_NEEDCHANGE="1"
                /usr/bin/mv $TEMP_FILE $SYSTEM_TEMP
            fi
        fi
    fi



    if [ "$SHMMIN" -lt "$SHMMIN_REQ" -o -z "$SHMMIN" ]; then
        if [ -z "$SHMMIN" ]; then
            SYSTEM_NEEDCHANGE="1"
            echo "set shmsys:shminfo_shmmin=$SHMMIN_REQ" >> $SYSTEM_TEMP
        else
            repl_cmd=`echo "s/\\(set shmsys:shminfo_shmmin\\).*/\\1=$SHMMIN_REQ/"`
            /usr/bin/cat $SYSTEM_TEMP | /usr/bin/sed -e "$repl_cmd" > $TEMP_FILE
            if [ $? -eq 0 ]; then
                SYSTEM_NEEDCHANGE="1"
                /usr/bin/mv $TEMP_FILE $SYSTEM_TEMP
            fi
        fi
    fi

    if [ "$SHMMNI" -lt "$SHMMNI_REQ" -o -z "$SHMMNI"  ]; then
        if [ -z "$SHMMNI" ]; then
            SYSTEM_NEEDCHANGE="1"
            echo "set shmsys:shminfo_shmmni=$SHMMNI_REQ" >> $SYSTEM_TEMP
        else
            repl_cmd=`echo "s/\\(set shmsys:shminfo_shmmni\\).*/\\1=$SHMMNI_REQ/"`
            /usr/bin/cat $SYSTEM_TEMP | /usr/bin/sed -e "$repl_cmd" > $TEMP_FILE
            if [ $? -eq 0 ]; then
                SYSTEM_NEEDCHANGE="1"
                /usr/bin/mv $TEMP_FILE $SYSTEM_TEMP
            fi
        fi
    fi

    if [ "$SHMSEG" -lt "$SHMSEG_REQ" -o -z "$SHMSEG"  ]; then
        if [ -z "$SHMSEG" ]; then
            SYSTEM_NEEDCHANGE="1"
            echo "set shmsys:shminfo_shmseg=$SHMSEG_REQ" >> $SYSTEM_TEMP
        else
            repl_cmd=`echo "s/\\(set shmsys:shminfo_shmseg\\).*/\\1=$SHMSEG_REQ/"`
            /usr/bin/cat $SYSTEM_TEMP | /usr/bin/sed -e "$repl_cmd" > $TEMP_FILE
            if [ $? -eq 0 ]; then
                SYSTEM_NEEDCHANGE="1"
                /usr/bin/mv $TEMP_FILE $SYSTEM_TEMP
            fi
        fi
    fi

    if [ "$SEMMNI" -lt "$SEMMNI_REQ" -o -z "$SEMMNI" ]; then
        if [ -z "$SEMMNI" ]; then
            SYSTEM_NEEDCHANGE="1"
            echo "set semsys:seminfo_semmni=$SEMMNI_REQ" >> $SYSTEM_TEMP
        else
            repl_cmd=`echo "s/\\(set semsys:seminfo_semmni\\).*/\\1=$SEMMNI_REQ/"`
            /usr/bin/cat $SYSTEM_TEMP | /usr/bin/sed -e "$repl_cmd" > $TEMP_FILE
            if [ $? -eq 0 ]; then
                SYSTEM_NEEDCHANGE="1"
                /usr/bin/mv $TEMP_FILE $SYSTEM_TEMP
            fi
        fi
    fi

    if [ "$SEMMSL" -lt "$SEMMSL_REQ" -o -z "$SEMMSL" ]; then
        if [ -z "$SEMMSL" ]; then
            SYSTEM_NEEDCHANGE="1"
            echo "set semsys:seminfo_semmsl=$SEMMSL_REQ" >> $SYSTEM_TEMP
        else
            repl_cmd=`echo "s/\\(set semsys:seminfo_semmsl\\).*/\\1=$SEMMSL_REQ/"`
            /usr/bin/cat $SYSTEM_TEMP | /usr/bin/sed -e "$repl_cmd" > $TEMP_FILE
            if [ $? -eq 0 ]; then
                SYSTEM_NEEDCHANGE="1"
                /usr/bin/mv $TEMP_FILE $SYSTEM_TEMP
            fi
        fi
    fi

    if [ "$SEMMNS" -lt "$SEMMNS_REQ" -o -z "$SEMMNS" ]; then
        if [ -z "$SEMMNS" ]; then
            SYSTEM_NEEDCHANGE="1"
            echo "set semsys:seminfo_semmns=$SEMMNS_REQ" >> $SYSTEM_TEMP
        else
            repl_cmd=`echo "s/\\(set semsys:seminfo_semmns\\).*/\\1=$SEMMNS_REQ/"`
            /usr/bin/cat $SYSTEM_TEMP | /usr/bin/sed -e "$repl_cmd" > $TEMP_FILE
            if [ $? -eq 0 ]; then
                SYSTEM_NEEDCHANGE="1"
                /usr/bin/mv $TEMP_FILE $SYSTEM_TEMP
            fi
        fi
    fi

    if [ "$SEMOPM" -lt "$SEMOPM_REQ" -o -z "$SEMOPM" ]; then
        if [ -z "$SEMOPM" ]; then
            SYSTEM_NEEDCHANGE="1"
            echo "set semsys:seminfo_semopm=$SEMOPM_REQ" >> $SYSTEM_TEMP
        else
            repl_cmd=`echo "s/\\(set semsys:seminfo_semopm\\).*/\\1=$SEMOPM_REQ/"`
            /usr/bin/cat $SYSTEM_TEMP | /usr/bin/sed -e "$repl_cmd" > $TEMP_FILE
            if [ $? -eq 0 ]; then
                SYSTEM_NEEDCHANGE="1"
                /usr/bin/mv $TEMP_FILE $SYSTEM_TEMP
            fi
        fi
    fi

    if [ "$SEMVMX" -lt "$SEMVMX_REQ" -o -z "$SEMVMX" ]; then
        if [ -z "$SEMVMX" ]; then
            SYSTEM_NEEDCHANGE="1"
            echo "set semsys:seminfo_semvmx=$SEMVMX_REQ" >> $SYSTEM_TEMP
        else
            repl_cmd=`echo "s/\\(set semsys:seminfo_semvmx\\).*/\\1=$SEMVMX_REQ/"`
            /usr/bin/cat $SYSTEM_TEMP | /usr/bin/sed -e "$repl_cmd" > $TEMP_FILE
            if [ $? -eq 0 ]; then
                SYSTEM_NEEDCHANGE="1"
                /usr/bin/mv $TEMP_FILE $SYSTEM_TEMP
            fi
        fi
    fi

}

get_ex_args() {
	/usr/bin/ex - /tmp/symon_temp.$$ <<SYMONEOF >/dev/null 2>&1
a
.
w
q
SYMONEOF
    if [ $? -eq 0 ] ; then
	SM_EX_ARGS="-"
    else
	SM_EX_ARGS=""
    fi
    export SM_EX_ARGS

    if [ -f /tmp/symon_temp.$$ ] ; then
	/usr/sbin/unlink /tmp/symon_temp.$$
    fi
}

run_setup() {
#
# $1 = script name
#  $2 = component name
#
    if [ $do_default -eq 1 ]; then
        if [ $VERBOSE = "ON" ] ; then
	    $BASEDIR/sbin/$1 -d -v
        else
	    $BASEDIR/sbin/$1 -d
        fi
    else
        if [ $VERBOSE = "ON" ] ; then
	    $BASEDIR/sbin/$1 -v -s "$security_seed"
        else
	    $BASEDIR/sbin/$1 -s "$security_seed"
        fi
    fi

    if [ $? -ne 0 ] ; then
	echolog ""
	echolog 'Setup of $2 component failed.' $2
	echolog "Exiting setup."
	echolog ""
	exit 1
    fi
}

setup_component() {
#
#  $1 = setup script name
#  $2 = component name
#

    # hack to preserve i18n for console setup
    if [ $1 = sm_setup_console.sh ]; then
        #ask_user 'Would you like to setup the Core Sun Management Center $2 Component now?' $2
        SETUP_LIST="$SETUP_LIST Console"
        return
    fi

    if [ $1 = sm_setup_service.sh ]; then
        #ask_user 'Would you like to setup the Advanced Server/Console Sun Management Center $2 Component now?' $2
        SETUP_LIST="$SETUP_LIST Services"
        return
    fi

    if [ ! -f "$BASEDIR/sbin/$1" ]; then
	echolog 'The Sun Management Center Setup File \\\($2\\\)$3' "$BASEDIR" "/sbin/$1"
	echolog '    is missing. Cannot Setup Sun Management Center $2.' "$2"
	echolog ""
	exit 1
    fi

    #ask_user 'Would you like to setup the Core Sun Management Center $2 Component now?' $2
    SETUP_LIST="$SETUP_LIST $2"
    echologverbose ""
    echologverbose 'Initiating setup for Sun Management Center $2 Component.' $2
    run_setup $1 $2

}

setup_device_type()
{
    if [ ! -d $VARDIR ] ; then
        /usr/bin/mkdir -p $VARDIR
    fi

    if [  -f $VARDIR/deviceinfo.conf ] ; then
        return 0
    fi

    device_platform=`/usr/bin/uname -p`
    os_release=`/usr/bin/uname -r | sed 's/^5/2/' | cut -d\. -f1,2`
    archos_dir=`echo $device_platform-sun-solaris$os_release`

    LD_LIBRARY_PATH="$LD_LIBRARY_PATH:$BASEDIR/base/lib/$archos_dir"
    export LD_LIBRARY_PATH

    gsioutput=""
    gsioutput=`$BASEDIR/base/bin/$archos_dir/deviceinfo 2> /dev/null`

    if [ -z "$gsioutput" ] ; then
        return 0
    fi

    did=`echo $gsioutput | /usr/bin/nawk ' { print $1 } ' `
    oid=`echo $gsioutput | /usr/bin/nawk ' { print $2 } ' `
    ftype=`echo $gsioutput | /usr/bin/nawk ' { print $3 } ' `

    if [ -z "$did" -o -z "$oid" -o -z "$ftype" ] ; then
         echolog "Please read the release notes to configure the device type."
         return 0
    fi

    echo "DID             $did" > $VARDIR/deviceinfo.conf
    echo "OID             $oid" >> $VARDIR/deviceinfo.conf
    echo "FAMILYTYPE      $ftype" >> $VARDIR/deviceinfo.conf
    return 0
}

stop_all_sunmc_components () {

    SETUP_COMPONENTS=`get_value_from_cfgfile  "$BASEDIR/sbin/.cfginfo" "SETUP_COMPONENTS" "="`
    if [ $? -eq 0 ] ; then
	echolog "Stopping all Sun Management Center processes.  This may take a few moments...\n"
	if [ $no_server -eq 0 ] ; then

	    $BASEDIR/sbin/es-stop -A 

	    #
	    #stop any scheduled discovery request
	    #

	    kill_processes "discover"
            return
	fi
	if [ $no_agent -eq 0 ] ; then

	    $BASEDIR/sbin/es-stop -A > /dev/null
            return
        fi
    fi
}

setup_addon_product () 
{
    if [ -z "$1" ] ; then
        return
    fi
    already_setup_components=""
    already_setup_components=`get_value_from_cfgfile "$BASEDIR/sbin/.cfginfo" SETUP_COMPONENTS "="`
    if [ -z "$already_setup_components" ] ; then
	echolog "Core Sun Management Center setup is not done."
	echolog "Please setup core Sun Management Center first."
        return
    fi
    addon_setup_script="$BASEDIR/addons/$1/sbin/es-setup.sh"
    $addon_setup_script "$already_setup_components"
}
setup_all_addon_products  ()
{
    BAD_ADDON=""
    if [ -d "$BASEDIR/addons" ] ; then
	already_setup_components=`get_value_from_cfgfile "$BASEDIR/sbin/.cfginfo" SETUP_COMPONENTS "="`
        if [ -z "$already_setup_components" ] ; then
            echolog "Core Sun Management Center setup is not done."
            echolog "Please setup core Sun Management Center first."
            return
        fi
	SETUP_LIST=`echo "$already_setup_components" | /usr/bin/tr [A-Z] [a-z]`
	addon_list=`ls $BASEDIR/addons/*/sbin/es-setup.sh 2> /dev/null`
	for each_addon in $addon_list
	do
	    addon_name=`/usr/bin/dirname $each_addon`
	    addon_name=`/usr/bin/dirname $addon_name`
	    addon_name=`/usr/bin/basename $addon_name`
	    # echolog 'Initiating setup for Sun Management Center $2 Addon.' $addon_name
	    $each_addon $SETUP_LIST
	    return_from_addon=$?
	    if [ $return_from_addon -ne 0 ] ; then
		if [ $return_from_addon -eq 2 ] ; then
		     exit 1
		fi
		echolog ""
		BAD_ADDON="$BAD_ADDON $addon_name"
		ask_user 'A problem occured with $2 setup.  Do you want to continue?' $addon_name
		if [ $answer -eq 0 ] ; then
		    echolog ""
		    echolog "Setup of Sun Management Center addons incomplete. Exiting"
		    echolog ""
		    exit 1
		fi
	    fi
	done
    fi

    echolog ""
    if [ -z "$BAD_ADDON" ] ; then
	echologverbose "Sun Management Center setup complete."
    else
	echolog 'Problems occured with setup of the following addons: $2' "$BAD_ADDON"
    fi
}

######################################################
#
#  Main entry point
#
prog_base=`/usr/bin/basename $0`
common_cmd=`echo $0 | /usr/bin/sed s/$prog_base/es-common.sh/`
. $common_cmd

PROGNAME=$0;
VERBOSE=OFF
do_default=0
ADDON_SETUP=0
ADDON_PRODNAME=""
ADDON_ONLY_SETUP=0
force_setup_flag=0
do_java_setup=0
arg_count=0       
while getopts Fdvajp: OPT
do
    case $OPT in
	F) 
	    force_setup_flag=1
            arg_count=`/usr/bin/expr "$arg_count" + 1`
	    ;;	
	d)
	    do_default=1
            arg_count=`/usr/bin/expr "$arg_count" + 1`
	    ;;
	v)
	    VERBOSE=ON
	    ;;
	a)
	    ADDON_ONLY_SETUP=1
            arg_count=`/usr/bin/expr "$arg_count" + 1`
	    ;;
        j)  
            do_java_setup=1
            arg_count=`/usr/bin/expr "$arg_count" + 1`
            ;;
	p)
	    ADDON_SETUP=1
            ADDON_PRODNAME="$OPTARG"
            arg_count=`/usr/bin/expr "$arg_count" + 1`
	    ;;
	*)
	    exit 1
	    ;;
    esac
done

if [ $arg_count -gt 1 ]; then
   echolog 'Invalid Combination:$2' "$@"
   exit 1
fi

#
#make sure umask 022
#

umask 022

SMCORAU=smcorau
SMCORAG=smcorag

set_inst_base_dir
set_basedir

/usr/bin/mkdir -p "$VAROPTDIR"
/usr/bin/chmod 755 "$VAROPTDIR"

check_root
check_logfile setup
set_paths
check_osver

#
#Setup addon product only if requested.
#
if [ $ADDON_SETUP -eq 1 ] ; then
    setup_addon_product "$ADDON_PRODNAME"
    exit 0
fi
if [ $ADDON_ONLY_SETUP -eq 1 ] ; then
    setup_all_addon_products 
    exit 0
fi

set_db_env

echologverbose 'Using log file: $2.' $LOGFILE
export LOGFILE

echolog ""
echolog "   -----------------------------------"
echolog "   Sun Management Center Setup Program"
echolog "   -----------------------------------"
echolog ""
echolog "This program does setup of Sun Management Center components that are installed on your system."
echolog ""

echolog "Checking for Sun Management Center components installed on your system."
echolog ""

set_inst_flags

if [ $no_agent -eq 1 ] && [ $no_server -eq 1 ] && [ $no_console -eq 1 ] ; then
	echolog ""
	echolog "You Do not have any Sun Management Center components installed."
	echolog "You may want to run setup after installing"
	echolog "    the Sun Management Center components."
	echolog ""
	echolog "Exiting setup."
	echolog ""
	exit 0
fi

echolog "You have the following Sun Management Center components installed"
echolog ""
[ $no_server -eq 0 ]   && echolog "             Sun Management Center Server"
[ $no_agent -eq 0 ]    && echolog "             Sun Management Center Agent"
[ $no_console -eq 0 ]  && echolog "             Sun Management Center Console"
echolog ""



# Configure java and exit in case of -j option
#
if [ "$do_java_setup" = "1" ]; then
   echolog ''
   if [ "$no_server" = "0" -o "$no_console" = "0" ] ; then
      checkJavaOK
      if [ $? -eq 0 ] ; then
         echolog 'Sun Management Center successfully configured for java: $2' "$JAVA_FULLVERSION"
         exit 0
      else
         echolog 'Could not configure Sun Management Center for java.'
         [ -n "$JAVA_FULLVERSION" ] && echolog 'Found installation of java: $2' "$JAVA_FULLVERSION"
         echolog 'Sun Management Center requires complete installation of $2 or above.' "$JAVA_MINIMUM_REQUIRED"
         echolog 'Please follow the following steps before attempting Sun Management Center setup.'
         echolog '1. Install supported JDK version.'
         echolog '2. set JAVA_HOME to directory where JDK is installed.'
         echolog '3. export JAVA_HOME'
         echolog '4. Run $2' "$BASEDIR/sbin/es-setup -j"
         echolog ''
         /usr/bin/rm -f $JAVA_HOME_FILE
         exit 1
      fi
   else
      echolog 'No Server or Console installation found.'
      echolog 'Sun Management Center needs java only if server or console layer is installed.'
      exit 1
   fi
fi

stop_all_sunmc_components

if [ -f $BASEDIR/sbin/.cfginfo ]; then
    SYSTEM=`/usr/bin/grep "SYSTEM" $BASEDIR/sbin/.cfginfo | /usr/bin/cut -f2 -d=`
else
    SYSTEM=""
fi
if [ "$SYSTEM" = "DEVELOPMENT" ] ; then
    if [ ! -f "$VARDIR/.sdklicense" ] ; then
          LICENSE=`/usr/bin/grep "LICENSE" $BASEDIR/sbin/.cfginfo | /usr/bin/cut -f2 -d=`
          [ -d "$VARDIR" ] && /usr/bin/mkdir -p "$VARDIR"
          echo "$LICENSE" > $VARDIR/.sdklicense
    fi
fi

#if [ ! -f "$VAROPTDIR/.java_home" ] ; then
#    if [ -f $BASEDIR/sbin/.cfginfo ]; then
#	JAVADIR=`/usr/bin/grep "JAVADIR" $BASEDIR/sbin/.cfginfo | /usr/bin/cut -f2 -d= > /dev/null 2>&1`
#    else
#	JAVADIR=""
#    fi
#    [ -d "$VAROPTDIR" ] && /usr/bin/mkdir -p "$VAROPTDIR"
#    [ -z "$JAVADIR" ] && JAVADIR="/usr/java1.2"
#    echo "$JAVADIR" > $VAROPTDIR/.java_home
#    /usr/bin/chmod 664 $VAROPTDIR/.java_home > /dev/null 2>&1
#fi

# Setup Java if required
#
if [ $no_server -eq 0 -o $no_console -eq 0 ] ; then
   checkJavaOK
   ret_status=$?
   if [ $ret_status -gt 0 ] ; then
      echolog ''
      [ -n "$JAVA_FULLVERSION" ] && echolog 'Found installation of java: $2' "$JAVA_FULLVERSION"
      echolog 'Sun Management Center requires complete installation of $2 or above.' "$JAVA_MINIMUM_REQUIRED"
      echolog 'Please follow the following steps before attempting Sun Management Center setup.'
      echolog '1. Install supported JDK version.'
      echolog '2. set JAVA_HOME to directory where JDK is installed.'
      echolog '3. export JAVA_HOME'
      echolog '4. Run $2' "$BASEDIR/sbin/es-setup "
      echolog ''
      /usr/bin/rm -f $JAVA_HOME_FILE
      exit 1
   else
      echolog ''
      echolog 'Sun Management Center successfully configured for java: $2' "$JAVA_FULLVERSION"
      echolog ''
   fi
   JAVA=$SYMON_JAVAHOME/bin/java
fi

#  For server and/or agent installation
#  migrate the cfg data, run db-import.sh
#  for server installs
#
RUN_DB_IMPORT="NO"
if [ $no_agent -eq 0 -o $no_server -eq 0 ]
then

   if [ -d /var/opt/SUNWsymon/2x -a ! -f /var/opt/SUNWsymon/2x/db-cfg-import.log ] || [ -d /var/opt/SUNWsymon/2x/db -a ! -f /var/opt/SUNWsymon/2x/db-import.log ]
   then
   echolog ''
   ask_user '    Do you want to import your previous data?'
   echolog ''
   if [ $answer -eq 1 ]
   then
      #  import cfg data if data exists
      if [ -d /var/opt/SUNWsymon/2x/cfg ]
      then
         cfg_cnt=`/usr/bin/ls -1 /var/opt/SUNWsymon/2x/cfg |/usr/bin/wc -l`
         if [ $cfg_cnt -gt 0 ]
         then
            import_2x_data_files
         fi
      fi
      
      #  run db-import.sh if only if the following conditions are true

      if [ $no_server -eq 0 ]
      then
         if [ -d /var/opt/SUNWsymon/2x/db -a ! -f /var/opt/SUNWsymon/2x/db-import.log ]
         then
            RUN_DB_IMPORT="YES"
         else
            RUN_DB_IMPORT="NO"
         fi
      else # not server install
         RUN_DB_IMPORT="NO"
      fi
   else
      RUN_DB_IMPORT="NO"
      echologverbose "Will not import previous data"
   fi
   fi

fi

# Bugfix for 
#
GENERATE_SECURITY_KEY=0
if [ $no_server -eq 0 ] ; then
   [ ! -f "${VARDIR}/server-global-keys.x" ] && GENERATE_SECURITY_KEY=1
   [ ! -f "${VARDIR}/event-usmusertbl-d.dat" ] && GENERATE_SECURITY_KEY=1
   [ ! -f "${VARDIR}/trap-usmusertbl-d.dat" ] && GENERATE_SECURITY_KEY=1
   [ ! -f "${VARDIR}/topology-usmusertbl-d.dat" ] && GENERATE_SECURITY_KEY=1
   [ ! -f "${VARDIR}/metadata-usmusertbl-d.dat" ] && GENERATE_SECURITY_KEY=1
fi

if [ $no_agent -eq 0 ] ; then
   [ ! -f "${VARDIR}/agent-usmusertbl-d.dat" ] && GENERATE_SECURITY_KEY=1
   [ ! -f "${VARDIR}/platform-usmusertbl-d.dat" ] && GENERATE_SECURITY_KEY=1
fi
[ "$force_setup_flag" = "1" ] && GENERATE_SECURITY_KEY=1
export GENERATE_SECURITY_KEY


# ------------------------------

# Relink database
if [ $no_server -eq 0 ] ; then

    umask 022

    #
    #  Ensure we cleanup any oracle related sql and log files
    #  used by the smcorau user
    #


    if [ -f /tmp/db-start.sql ]
    then
      rm -rf /tmp/db-start.sql
    fi

    if [ -f /tmp/db-start.log ]
    then
      rm -rf /tmp/db-start.log
    fi

    if [ -f /tmp/db-start-sql.log ]
    then
      rm -rf /tmp/db-start-sql.log
    fi

    if [ -f /tmp/db-start-sh.log ]
    then
      rm -rf /tmp/db-start-sh.log
    fi

    if [ -f /tmp/db-start-vinst.sql ]
    then
      rm -rf /tmp/db-start-vinst.sql
    fi

    if [ -f /tmp/db-start-vinst.log ]
    then
      rm -rf /tmp/db-start-vinst.log
    fi

    if [ -f /tmp/db-stop.log ]
    then
       rm -rf /tmp/db-stop.log
    fi

    if [ -f /tmp/db-stop-sql.log ]
    then
      rm -rf /tmp/db-stop-sql.log
    fi

    if [ -f /tmp/db-charset-utf8.log ]
    then
      rm -rf /tmp/db-charset-utf8.log
    fi

    if [ -f /tmp/db-charset-utf8.sql ]
    then
      rm -rf /tmp/db-charset-utf8.sql
    fi

    if [ -f /tmp/db-charset-utf8-sql.log ]
    then
      rm -rf /tmp/db-charset-utf8-sql.log
    fi




    if [ ! -d /tmp ]
    then
       echolog "the needed /tmp directory doesn't exist"
       echolog "please manually create it with at least 777 perms"
       exit 1
    fi

    if [ ! -d /var ]
    then
       echolog "the needed /var directory doesn't exist"
       echolog "please manually create it with at least 755 perms"
       exit 1
    fi

    if [ ! -d /var/tmp ]
    then
       echolog "the needed /var/tmp directory doesn't exist"
       echolog "please manually create it with 777 perms"
       exit 1
    fi


     #
     #  ensure that /tmp is at least 777 permissions
     #

     new="/tmp"

     accessperms=`ls -ld $new | awk '{print $1}'`

     retcode=$?

     if [ $retcode != 0 ]
     then
        echolog "es-setup - failed to obtain accessperms from ls command for : $new"
        exit 1
     fi


     #
     # verify at least 777 perms on each file
     # which is:  -rwxrwxrwx
     #

     for item in `echo "2 3 4 5 6 7 8 9 10"`
     do
        tmp=`echo $accessperms | cut -c$item`

#        echo "tmp : " $tmp

        if [ $tmp = "-" ]
        then
          echolog "es-setup - changing access perms for $new to 777"

          chmod 777 $new

          retcode=$?

          if [ $retcode != 0 ]
          then
             echolog "es-setup - failed to change permissions from chmod command for : $new"
             echolog "es-setup - access perms for $new is not at least 777"
             echolog "please manually modify using chmod to be at least 777"
             echolog "then re-start es-setup"
             exit 1
          fi

          break 1
        fi

     done


     #
     #  ensure that /var is at least 755 permissions
     #

     new="/var"

     accessperms=`ls -ld $new | awk '{print $1}'`

     retcode=$?

     if [ $retcode != 0 ]
     then
        echolog "es-setup - failed to obtain accessperms from ls command for : $new"
        exit 1
     fi


     #
     # verify at least 755 perms on each file
     # which is:  -rwxr-xr-x
     #

     for item in `echo "2 3 4 5 7 8 10"`
     do
        tmp=`echo $accessperms | cut -c$item`

#        echo "tmp : " $tmp

        if [ $tmp = "-" ]
        then
          echolog "es-setup - changing access perms for $new to 755"

          chmod 755 $new

          retcode=$?

          if [ $retcode != 0 ]
          then
             echolog "es-setup - failed to change permissions from chmod command for : $new"
             echolog "es-setup - access perms for $new is not at least 755"
             echolog "please manually modify using chmod to be at least 755"
             echolog "then re-start es-setup"
             exit 1
          fi

          break 1
        fi

     done


     #
     # ensure that /var/tmp has 777 permissions
     #

     new="/var/tmp"

     accessperms=`ls -ld $new | awk '{print $1}'`

     retcode=$?

     if [ $retcode != 0 ]
     then
        echolog "es-setup - failed to obtain accessperms from ls command for : $new"
        exit 1
     fi


     #
     # verify at least 777 perms on each file
     # which is:  -rwxr-xr-x
     #

     for item in `echo "2 3 4 5 6 7 8 9 10"`
     do
        tmp=`echo $accessperms | cut -c$item`

#        echo "tmp : " $tmp

        if [ $tmp = "-" ]
        then
          echolog "es-setup - changing access perms for $new to 777"

          chmod 777 $new

          retcode=$?

          if [ $retcode != 0 ]
          then
             echolog "es-setup - failed to change permissions from chmod command for : $new"
             echolog "es-setup - access perms for $new is not at least 777"
             echolog "please manually modify using chmod to be at least 777"
             echolog "then re-start es-setup"
             exit 1
          fi

          break 1
        fi

     done

     #
     #  ensure sticky bit is set
     #

     tmp=`echo $accessperms | cut -c10`

     if [ $tmp != "t" ]
     then
        chmod u+t $new

        if [ $retcode != 0 ]
        then
           echolog "es-setup - failed to change permissions from chmod command for : $new"
           exit 1
        fi

     fi







    #
    #
    #  Setup Oracle Admin User and Group
    #
    #


     groupid=""

     verifyCreateOracleGroup $SMCORAG

     retcode=$?

     if [ $retcode != 0 ]
     then
        echolog "execution of verifyCreateOracleGroup failed, exiting............."
        exit 1
     fi


     verifyCreateOracleUser $SMCORAU $SMCORAG $groupid "/var/opt/SUNWsymon"

     retcode=$?

     if [ $retcode != 0 ]
     then
        echolog "execution of verifyCreateOracleUser failed, exiting............."
        exit 1
     fi


     if [ ! -d /var/opt/SUNWsymon/$SMCORAU ]; then
        echolog "the admin user $SMCORAU working directory /var/opt/SUNWsymon/$SMCORAU does not exist"
        echolog "please manually remove the existing admin user before proceeding"
        exit 1
     fi



     echo "ORACLE_BASE=$ORACLE_HOME" >> /var/opt/SUNWsymon/$SMCORAU/.profile
     echo "export ORACLE_BASE" >> /var/opt/SUNWsymon/$SMCORAU/.profile
     echo "ORACLE_HOME=$ORACLE_HOME" >> /var/opt/SUNWsymon/$SMCORAU/.profile
     echo "export ORACLE_HOME" >> /var/opt/SUNWsymon/$SMCORAU/.profile
     echo "ORACLE_SID=SunMC30" >> /var/opt/SUNWsymon/$SMCORAU/.profile
     echo "export ORACLE_SID" >> /var/opt/SUNWsymon/$SMCORAU/.profile
     echo "ORA_NLS33=$ORACLE_HOME/ocommon/nls/admin/data" >> /var/opt/SUNWsymon/$SMCORAU/.profile
     echo "export ORA_NLS33" >> /var/opt/SUNWsymon/$SMCORAU/.profile
     echo "PATH=$PATH:$ORACLE_HOME/bin" >> /var/opt/SUNWsymon/$SMCORAU/.profile
     echo "export PATH" >> /var/opt/SUNWsymon/$SMCORAU/.profile


    #
    # check if db (listener in particular) is currently executing and stop if so.
    #


    lcnt=0
    scnt=0
    lcnt=`/usr/ucb/ps -ww -gax | /usr/bin/grep -i "tnslsnr smcdblistener" | /usr/bin/grep -v "grep" | /usr/bin/wc -l`
    scnt=`/usr/ucb/ps -ww -gax | /usr/bin/grep -i "ora_.*$ORACLE_SID" | /usr/bin/grep -v "grep" | /usr/bin/wc -l`


    if [ $lcnt -gt 0 -o $scnt -gt 0 ]
    then

       #
       # stop database
       #

       prstring=`$ESROOT/base/bin/sparc-sun-solaris${OSVERS}/smcpwchk $SMCORAU 2>/dev/null`

       retcode=$?

       if [ $retcode = 2 ]
       then
          echolog 'the user $2 was not found by a getpwnam system call ,not in /etc/passwd, nis, nis+, etc..' "$SMCORAU"
          echolog 'user must manually deal with user issues before continuing'
          echolog 'cannot perform a db-stop, exiting.............................'
          exit 1
       fi

       grstring=`$ESROOT/base/bin/sparc-sun-solaris${OSVERS}/smcgrchk $SMCORAG 2>/dev/null`

       retcode=$?

       if [ $retcode = 2 ]
       then
          echolog 'the group $2 was not found by a getgrnam system call ,not in /etc/group, nis, nis+, etc..' "$SMCORAG"
          echolog 'user must manually deal with group issues before continuing'
          echolog 'cannot perform a db-stop, exiting.............................'
          exit 1
       fi

       if [ ! -f $BASEDIR/sbin/db-stop ]
       then
          echolog 'the $2 file does not exist, exiting..............' "$BASEDIR/sbin/db-stop"
          exit 1
       fi

       su - $SMCORAU -c $BASEDIR/sbin/db-stop

        #
        #  Confirm that the db and listener are down
        #

        lcnt=0
        scnt=0
        lcnt=`/usr/ucb/ps -ww -gax | /usr/bin/grep -i "tnslsnr smcdblistener" | /usr/bin/grep -v "grep" | /usr/bin/wc -l`
        scnt=`/usr/ucb/ps -ww -gax | /usr/bin/grep -i "ora_.*$ORACLE_SID" | /usr/bin/grep -v "grep" | /usr/bin/wc -l`

        if [ $lcnt -gt 0 -o $scnt -gt 0 ]
        then
           echolog "Attempt to stop the database and the listener were unsuccessfull, exiting.............."
           exit 1
        fi


    fi



    #
    # check if the memory is enough for requirement
    #
    echologverbose ""
    echologverbose "Checking memory available..."
    smcdb_check_memory_available




    # the flag to indicate if /etc/system file has been changed
    SYSTEM_CHANGED="0"

    # Check and change system info
    smcdb_check_system
    if [ $? -ne 0 ]; then
        echolog "Checking /etc/system file error. It can not be updated."
        exit 1
    fi


    if [ "$SYSTEM_NEEDCHANGE" -eq 1 ]; then
        echolog ""
        echolog "The /etc/system file needs to be changed for Sun Management Center DB requirements."
        ask_user '    Do you wish to update /etc/system file?'
        echolog ''
        if [ $answer -eq 1 ]
        then
            echologverbose "Backing up /etc/system to /etc/system.SunMCDB.backup"
            /usr/bin/cp /etc/system /etc/system.SunMCDB.backup
            /usr/bin/mv $SYSTEM_TEMP /etc/system
            /usr/bin/chgrp sys /etc/system
            SYSTEM_CHANGED="1"
        else
            #echolog "Setup of Sun Management Center may fail if /etc/system is not changed."
            echologverbose "/etc/system file has not been changed."
        fi
    fi

    /usr/bin/rm -f $SYSTEM_TEMP
    /usr/bin/rm -f $TEMP_FILE

    # The system need to be reboot when the /etc/system changed
    if [ $SYSTEM_CHANGED -eq 1 ]; then
        #
        # prompt for reboot if the system file has been changed
        #
        echolog ""
        echolog "In order for kernel variable changes to take effect, this "
        echolog "machine must be rebooted.  You must reboot this machine now"
        echolog "and then run the setup again."

        echolog ""
        exit 2
    fi



    #
    #  Create symbolic link from actual extracted location
    #  of the SUNWestbl package and /var/opt/SUNWsymon/SunMCDB02
    #  if they are not one in the same.
    #
    #

    # ensure proper umask, just in case
    umask 022


    pkgestbl_loc=`/usr/bin/pkginfo -r SUNWestbl`

    if [ "$pkgestbl_loc" != "/var/opt" ]
    then

       if [ -h /var/opt/SUNWsymon/SunMCDB02 ]; then
        /usr/bin/rm  /var/opt/SUNWsymon/SunMCDB02
       fi

       if [ -d /var/opt/SUNWsymon/SunMCDB02 ]; then
        /usr/bin/mv  /var/opt/SUNWsymon/SunMCDB02 /var/opt/SUNWsymon/SunMCDB02.bak
		echolog "the /var/opt/SUNWsymon/SunMCDB02 already existed, renaming to /var/opt/SUNWsymon/SunMCDB02.bak"
       fi


       /usr/bin/ln -s $pkgestbl_loc/SUNWsymon/SunMCDB02 /var/opt/SUNWsymon/SunMCDB02 >/dev/null 2>&1

	if [ $? -ne 0 ] ; then
	    echolog 'Cannot create link $2 to directory $3' "/var/opt/SUNWsymon/SunMCDB02" "$pkgestbl_loc/SUNWsymon/SunMCDB02"
	    echolog 'SunMCDB setup cannot continue.'
	    echolog 'Sun Management Center DB setup cannot continue.'
	    exit 1
	fi

    fi


    /usr/bin/chown -Rh root /var/opt/SUNWsymon/SunMCDB02
    if [ $? -ne 0 ] ; then
	echolog 'Error changing ownership of $2 to root.  Installation cannot continue.' "/var/opt/SUNWsymon/SunMCDB02"
	exit 1
    fi



    pkgesora_loc=`/usr/bin/pkginfo -r SUNWesora`

    if [ -d $pkgesora_loc/SUNWsymon/SunMCDB01 ]
    then

       /usr/bin/chown -Rh root $pkgesora_loc/SUNWsymon/SunMCDB01
       if [ $? -ne 0 ] ; then
         echolog 'Error changing ownership of $2 to root.  Installation cannot continue.' "$pkgesora_loc/SUNWsymon/SunMCDB01"
  	   exit 1
       fi
    fi

    #
    # generate listener and tns files
    #
    echologverbose ""
    echologverbose "Configuring Sun Management Center DB listener and service files..."

#    SMCDB_HOST=`/usr/bin/more /etc/hosts |/usr/bin/grep loghost |/usr/bin/cut -f2`
#     SMCDB_HOST=`/usr/bin/hostname`
     SMCDB_HOST=`/usr/bin/uname -a | /bin/awk '{print $2}'`

    #
    # check and change listener port
    #
    SMCDB_PORT="2521"

    # check if the default port is used

    provided_SMCDB_PORT=$SMCDB_PORT

    smcdb_check_port_number "$provided_SMCDB_PORT"
    retcode=$?

    while [ $retcode -eq 1 ]
    do

    echolog 'The default listener port $2 for SunMC DB has already been used.' "$provided_SMCDB_PORT"
    echolog "Please input another port number for Sun Management Center DB listener."

      tmp_SMCDB_PORT=`/usr/bin/expr $provided_SMCDB_PORT + "1"`

      get_input_from_user 'Enter the listener port : [  $tmp_SMCDB_PORT ]'

      if [ -n "$answer" ]
      then
         #checking for the validity of the port number

         /usr/bin/expr $answer + 1  1> /dev/null  2>&1
         if [ $? -ne 0 ] ; then
			 echolog 'Port $2 is not a valid port number.' "$answer"
             continue
         fi

         provided_SMCDB_PORT=$answer

         smcdb_check_port_number "$provided_SMCDB_PORT"
         retcode=$?

	  else
		 # user has accepted the new default we had proposed. There
		 # is no need to test it
		 provided_SMCDB_PORT=$tmp_SMCDB_PORT
         smcdb_check_port_number "$provided_SMCDB_PORT"
         retcode=$?
      fi

    done

    SMCDB_PORT=$provided_SMCDB_PORT



    # configure the listener and tns files
    smcdb_configuration

    if [ $? -ne 0 ]; then
	echolog "Error in resetting the configuration files for Sun Management Center DB."
	exit 1
    fi



        #
        #  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.
        #

        if [ ! -f /usr/ccs/bin/make ] ; then
           echolog "The Sun Management Center server setup 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-setup script again."
           echolog ""
           exit 1
        fi

        if [ ! -f /usr/ccs/bin/ld ] ; then
           echolog ""
           echolog "The Sun Management Center server setup 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-setup script again."
           echolog ""
           exit 1
        fi

        if [ ! -f /usr/ccs/bin/ar ] ; then
           echolog ""
           echolog "The Sun Management Center server setup 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-setup script again."
           echolog ""
           exit 1
        fi


    echolog "Configuring the system for setup, please wait."


    #
    # Add some specific makes to ensure it gets done
    #


    if [ -f /tmp/make.log ]
    then
       rm -rf /tmp/make.log
    fi

    # save current dir
    curr_dir=`/usr/bin/pwd`

    # ensure we set the nmliblist file
    echo "$ORACLE_HOME/rdbms/lib/skgxns.o" > $ORACLE_HOME/rdbms/lib/nmliblist


    echo "============================= ins_rdbms.mk install ==================================================" >> /tmp/make.log
    echo " " >> /tmp/make.log
    echo " " >> /tmp/make.log
    echo " " >> /tmp/make.log

    cd $ORACLE_HOME/rdbms/lib
    unset OPT

    /usr/ccs/bin/make -f ins_rdbms.mk install >> /tmp/make.log 2>&1

    retcode=$?

    if [ $retcode -ne 0 ]
    then
      echolog "execution of make -f ins_rdbms.mk install failed"
      echolog "see /tmp/make.log file for details"
      exit 1
    fi

    echo " " >> /tmp/make.log
    echo " " >> /tmp/make.log
    echo " " >> /tmp/make.log
    echo "============================= ins_rdbms.mk utilities ================================================" >> /tmp/make.log
    echo " " >> /tmp/make.log
    echo " " >> /tmp/make.log
    echo " " >> /tmp/make.log

    cd $ORACLE_HOME/rdbms/lib
    unset OPT

    /usr/ccs/bin/make -f ins_rdbms.mk utilities >> /tmp/make.log 2>&1

    retcode=$?

    if [ $retcode -ne 0 ]
    then
      echolog "execution of make -f ins_rdbms.mk utilities failed"
      echolog "see /tmp/make.log file for details"
      exit 1
    fi


    echo " " >> /tmp/make.log
    echo " " >> /tmp/make.log
    echo " " >> /tmp/make.log
    echo "================================  ins_otrace.mk  ====================================================" >> /tmp/make.log
    echo " " >> /tmp/make.log
    echo " " >> /tmp/make.log
    echo " " >> /tmp/make.log

    cd $ORACLE_HOME/otrace/lib
    unset OPT

    /usr/ccs/bin/make -f ins_otrace.mk install >> /tmp/make.log 2>&1

    retcode=$?

    if [ $retcode -ne 0 ]
    then
      echolog "execution of make -f ins_otrace.mk install failed"
      echolog "see /tmp/make.log file for details"
      exit 1
    fi

    echo " " >> /tmp/make.log
    echo " " >> /tmp/make.log
    echo " " >> /tmp/make.log
    echo "================================  ins_plsql.mk  ====================================================" >> /tmp/make.log
    echo " " >> /tmp/make.log
    echo " " >> /tmp/make.log
    echo " " >> /tmp/make.log

    cd $ORACLE_HOME/plsql/lib
    unset OPT

    /usr/ccs/bin/make -f ins_plsql.mk install >> /tmp/make.log 2>&1

    retcode=$?

    if [ $retcode -ne 0 ]
    then
      echolog "execution of make -f ins_plsql.mk install failed"
      echolog "see /tmp/make.log file for details"
      exit 1
    fi


    echo " " >> /tmp/make.log
    echo " " >> /tmp/make.log
    echo " " >> /tmp/make.log
    echo "===================================  ins_net_server.mk  =================================================" >> /tmp/make.log
    echo " " >> /tmp/make.log
    echo " " >> /tmp/make.log
    echo " " >> /tmp/make.log

    cd $ORACLE_HOME/network/lib
    unset OPT

    /usr/ccs/bin/make -f ins_net_server.mk install >> /tmp/make.log 2>&1

    retcode=$?

    if [ $retcode -ne 0 ]
    then
      echolog "execution of make -f ins_net_server.mk install failed"
      echolog "see /tmp/make.log file for details"
      exit 1
    fi


    echo " " >> /tmp/make.log
    echo " " >> /tmp/make.log
    echo " " >> /tmp/make.log
    echo "===================================  ins_net_client.mk  (sharedlib) =======================================" >> /tmp/make.log
    echo " " >> /tmp/make.log
    echo " " >> /tmp/make.log
    echo " " >> /tmp/make.log

    cd $ORACLE_HOME/network/lib
    unset OPT

    /usr/ccs/bin/make -f ins_net_client.mk client_sharedlib >> /tmp/make.log 2>&1

    retcode=$?

    if [ $retcode -ne 0 ]
    then
      echolog "execution of make -f ins_net_client.mk client_sharedlib failed"
      echolog "see /tmp/make.log file for details"
      exit 1
    fi


    echo " " >> /tmp/make.log
    echo " " >> /tmp/make.log
    echo " " >> /tmp/make.log
    echo "===================================  ins_net_client.mk  =================================================" >> /tmp/make.log
    echo " " >> /tmp/make.log
    echo " " >> /tmp/make.log
    echo " " >> /tmp/make.log

    cd $ORACLE_HOME/network/lib
    unset OPT

    /usr/ccs/bin/make -f ins_net_client.mk install >> /tmp/make.log 2>&1

    retcode=$?

    if [ $retcode -ne 0 ]
    then
      echolog "execution of make -f ins_net_client.mk install failed"
      echolog "see /tmp/make.log file for details"

      exit 1
    fi

    echo " " >> /tmp/make.log
    echo " " >> /tmp/make.log
    echo " " >> /tmp/make.log
    echo "================================  ins_sqlplus.mk  ====================================================" >> /tmp/make.log
    echo " " >> /tmp/make.log
    echo " " >> /tmp/make.log
    echo " " >> /tmp/make.log

    cd $ORACLE_HOME/sqlplus/lib
    unset OPT

    /usr/ccs/bin/make -f ins_sqlplus.mk install >> /tmp/make.log 2>&1

    retcode=$?

    if [ $retcode -ne 0 ]
    then
      echolog "execution of make -f ins_sqlplus.mk install failed"
      echolog "see /tmp/make.log file for details"
      exit 1
    fi


    # return to current dir
    cd $curr_dir



    # set admin password after re-building orapwd util

    echologverbose "Create ora passord file"

    if [ -f $ORACLE_HOME/dbs/orapw$ORACLE_SID ]
    then
       rm -rf $ORACLE_HOME/dbs/orapw$ORACLE_SID
    fi

    $ORACLE_HOME/bin/orapwd file=$ORACLE_HOME/dbs/orapw$ORACLE_SID password=oracle entries=1
    if [ $? != 0 ]; then
	  echolog 'failed to create internal user password file : $2' "$ORACLE_HOME/dbs/orapw$ORACLE_SID"
	  exit 1
    fi



    if [ -f $ORACLE_HOME/bin/tnslsnr ]
    then
      chmod u+s $ORACLE_HOME/bin/tnslsnr

      retcode=$?

      if [ $retcode -ne 0 ]
      then
        echolog "es-setup - chmod u+s $ORACLE_HOME/bin/tnslsnr failed"
        echolog "exiting..............................."
        exit 1
      fi

    fi

    if [ -d $pkgesora_loc/SUNWsymon/SunMCDB01 ]
    then
      chown -hR $SMCORAU $pkgesora_loc/SUNWsymon/SunMCDB01

      retcode=$?

      if [ $retcode -ne 0 ]
      then
        echolog "es-setup - chown -hR $SMCORAU $pkgesora_loc/SUNWsymon/SunMCDB01 failed"
        echolog "exiting..............................."
        exit 1
      fi

      chgrp -hR $SMCORAG $pkgesora_loc/SUNWsymon/SunMCDB01

      retcode=$?

      if [ $retcode -ne 0 ]
      then
        echolog "es-setup - chgrp -hR $SMCORAG $pkgesora_loc/SUNWsymon/SunMCDB01 failed"
        echolog "exiting..............................."
        exit 1
      fi

    fi

    if [ -d $pkgestbl_loc/SUNWsymon/SunMCDB02 ]
    then
      chown -hR $SMCORAU $pkgestbl_loc/SUNWsymon/SunMCDB02

      retcode=$?

      if [ $retcode -ne 0 ]
      then
        echolog "es-setup - chown -hR $SMCORAU $pkgestbl_loc/SUNWsymon/SunMCDB02 failed"
        echolog "exiting..............................."
        exit 1
      fi


      chgrp -hR $SMCORAG $pkgestbl_loc/SUNWsymon/SunMCDB02

      retcode=$?

      if [ $retcode -ne 0 ]
      then
        echolog "es-setup - chgrp -hR $SMCORAG $pkgestbl_loc/SUNWsymon/SunMCDB02 failed"
        echolog "exiting..............................."
        exit 1
      fi

    fi




fi

if [ $do_default -eq 1 ]; then
    /usr/bin/pkginfo -qi SUNWessrv
    [ $? -eq 0 ] && run_setup sm_setup_server.sh Server
    /usr/bin/pkginfo -qi SUNWesmod
    [ $? -eq 0 ] && run_setup sm_setup_agent.sh Agent
    /usr/bin/pkginfo -qi SUNWesmod
    [ $? -eq 0 ] && run_setup sm_setup_metadata.sh Metadata
    exit 0
fi

echologverbose "This script will perform the setup for each of these components"
echologverbose ""

get_ex_args

#
# Ask for the seed here
#

# generate seeds only if user is not doing an upgrade

if [ "$UPGRADE_DONE" != "YES"  -a "$GENERATE_SECURITY_KEY" = "1" ]
then
    security_seed=""
    if [ $no_server -eq 0 -o $no_agent -eq 0 ] ; then
        echolog ""
        get_security_key $do_default
        security_seed="$seed"
    fi
fi

SETUP_LIST=""
[ $no_server -eq 0 ] && setup_component sm_setup_server.sh Server
[ $no_agent -eq 0 ] && setup_component sm_setup_agent.sh Agent
[ $no_metadata -eq 0 ] && setup_component sm_setup_metadata.sh Metadata
[ $no_console -eq 0 ] && setup_component sm_setup_console.sh Console
[ $no_service -eq 0 ] && setup_component sm_setup_service.sh Services

#
#If it is development environment then setup the DE.
#

check_development_env
if [ $? -eq 0 ] ; then
    $BASEDIR/sbin/es-setup-de.sh
fi

# copy one x file which will be removed by Advanced Monitoring Pack installation.
if [ -f "$BASEDIR/classes/base/console/cfg/applications-process-core-j.x" ] ; then
	/usr/bin/cp -f "$BASEDIR/classes/base/console/cfg/applications-process-core-j.x" "$VARDIR/applications-process-j.x"
	/usr/bin/chmod 644 "$VARDIR/applications-process-j.x"
fi

# create dummy x file which will be removed by Advanced Monitoring Pack installation.
if [ -f "$VARDIR/details-tab-viewlog-j.x" ] ; then
	/usr/bin/rm -f "$VARDIR/details-tab-viewlog-j.x"
fi
	/usr/bin/touch "$VARDIR/details-tab-viewlog-j.x"

# create dummy x files which will be removed by Advanced Services Pack installation.
/usr/bin/rm -f "$VARDIR/dataview-button-j.x"
/usr/bin/touch "$VARDIR/dataview-button-j.x"

/usr/bin/rm -f "$VARDIR/dataview-clipboard-j.x"
/usr/bin/touch "$VARDIR/dataview-clipboard-j.x"

/usr/bin/rm -f "$VARDIR/dataview-menu-j.x"
/usr/bin/touch "$VARDIR/dataview-menu-j.x"

/usr/bin/rm -f "$VARDIR/grouping-options-j.x"
/usr/bin/touch "$VARDIR/grouping-options-j.x"


echologverbose ""
echologverbose "Core Sun Management Center setup complete."

BAD_ADDON=""
if [ -d "$BASEDIR/addons" ] ; then
    addon_list=`ls $BASEDIR/addons/*/sbin/es-setup.sh 2> /dev/null`
    for each_addon in $addon_list
    do
	addon_name=`/usr/bin/dirname $each_addon`
	addon_name=`/usr/bin/dirname $addon_name`
	addon_name=`/usr/bin/basename $addon_name`
	# echolog 'Initiating setup for Sun Management Center $2 Addon.' $addon_name
	SETUP_LIST=`echo $SETUP_LIST | /usr/bin/tr [A-Z] [a-z]`
	$each_addon $SETUP_LIST
        return_from_addon=$?
	if [ $return_from_addon -ne 0 ] ; then
            if [ $return_from_addon -eq 2 ] ; then
                 exit 1
            fi
	    echolog ""
	    BAD_ADDON="$BAD_ADDON $addon_name"
	    ask_user 'A problem occured with $2 setup.  Do you want to continue?' $addon_name
	    if [ $answer -eq 0 ] ; then
		echolog ""
		echolog "Setup of Sun Management Center addons incomplete. Exiting"
		echolog ""
		exit 1
	    fi
	fi
    done
fi

echolog ""
if [ -z "$BAD_ADDON" ] ; then
    echolog "Sun Management Center setup complete."
else
    echolog 'Problems occured with setup of the following addons: $2' "$BAD_ADDON"
fi
#
#If Agent setup was done then prepare device info file.
#

echo $SETUP_LIST | /usr/bin/grep -iw Agent  > /dev/null 2>&1

if [ $? -eq 0 ] ; then
    setup_device_type
fi


#
# SunMC DB environment variables which will be used for setting
# the properties in dbase.cfg. These environment variables should
# be exported for being used by build-smc-oracle-recovery.ksh.
#
SMCDB_USER="sunmc"
export SMCDB_USER

SMCDB_PASSWORD="sunmc"
export SMCDB_PASSWORD

SMCDB_DATABASE="SunMC30"
export SMCDB_DATABASE



#
# The host name and port number for SunMC DB will be reset by
# the SunMC DB installation
#
export SMCDB_HOST

export SMCDB_PORT

SMCDB_DRIVER="oracle.jdbc.driver.OracleDriver"
export SMCDB_DRIVER

SMCDB_URL="jdbc:oracle:thin"
export SMCDB_URL

SMCDB_MINCONNECTIONS="5"
export SMCDB_MINCONNECTIONS

SMCDB_MAXCONNECTIONS="10"
export SMCDB_MAXCONNECTIONS

#
# Setup the oracle database if server type component found
#


if [ $no_server -eq 0 ]; then

    # get tbl package location again, just in case pkgestbl_loc var has been modified
    pkgestbl_loc=`/usr/bin/pkginfo -r SUNWestbl`

    numLines=`/usr/bin/df -k $pkgestbl_loc | wc | awk '{print $1}'`
    numLines=`expr $numLines - 1`
    available_db_loc=`/usr/bin/df -k $pkgestbl_loc | tail -$numLines | awk '{print $4}'`
    db_partition=`/usr/bin/df -k $pkgestbl_loc | tail -$numLines | awk '{print $1}'`

    # make sure at least 110 MB (112640 KB) is available for DB setup

    if  [ $available_db_loc -lt 112640 ]
    then
	    echolog ""
        echolog 'Insufficient disk space \\\($2 kb avaliable\\\) for database setup to proceed \\\(112640 kb needed\\\)' "$available_db_loc"
        echolog 'Please cleanup disk partition \\\($2\\\), then re-run $ESROOT/sbin/es-setup' "$db_partition"
	    echolog ""
        exit 1
    fi

	cd $BASEDIR/db/build
	if [ $? -ne 0 ] ; then
	    echolog 'Database setup failed : $2 does not exist' "$BASEDIR/db/build"
	    exit 1
	fi

        if [ $VERBOSE = "ON" ] ; then
	    ./mkaggregatefiles.ksh -v -l "$LOGFILE"
            retcode=$?
        else
	    ./mkaggregatefiles.ksh -l "$LOGFILE"
            retcode=$?
        fi
	if [ $retcode -ne 0 ]; then
	    echolog "Database setup failed : mkaggregatefiles.ksh failed"
	    exit 1
	fi



       prstring=`$ESROOT/base/bin/sparc-sun-solaris${OSVERS}/smcpwchk $SMCORAU 2>/dev/null`

       retcode=$?

       if [ $retcode = 2 ]
       then
          echolog 'the user $2 was not found by a getpwnam system call ,not in /etc/passwd, nis, nis+, etc..' "$SMCORAU"
          echolog 'user must manually deal with user issues before continuing'
          echolog 'cannot perform a db-start,db-charset-utf8 and db-stop, exiting.............................'
          exit 1
       fi

       grstring=`$ESROOT/base/bin/sparc-sun-solaris${OSVERS}/smcgrchk $SMCORAG 2>/dev/null`

       retcode=$?

       if [ $retcode = 2 ]
       then
          echolog 'the group $2 was not found by a getgrnam system call ,not in /etc/group, nis, nis+, etc..' "$SMCORAG"
          echolog 'user must manually deal with group issues before continuing'
          echolog 'cannot perform a db-start,db-charset-utf8 and db-stop, exiting.............................'
          exit 1
       fi



	#
	# Start the database server and build tables
	#


    if [ "VERBOSE" = "ON" ] ; then
        su - $SMCORAU -c $BASEDIR/sbin/db-start -v
        retcode=$?
    else
        su - $SMCORAU -c $BASEDIR/sbin/db-start
        retcode=$?
    fi
	if [ $retcode -ne 0 ]; then
	    echolog "Database setup failed : db-start failed"
	    exit 1
	fi


    #
    #  The script below will changes the charset from us7ascii  
    #  to utf8.  It expects to see the listener and database
    #  already executing, otherwise will report error and
    #  return a value of 1.
    #
    #  This logic is re-runnable without harm.
    #
    #

    su - $SMCORAU -c $BASEDIR/sbin/db-charset-utf8

    retcode=$?

	if [ $retcode -ne 0 ]; then
	    echolog "Change charset to UTF8 failed : db-charset-utf8 failed"
	    exit 1
	fi


	#
	# Build the SunMC oracle database
	#
	cd $BASEDIR/db/build

	PATH=$PATH:$ORACLE_HOME/bin
	export PATH

    #
    #  First test whether or not the sunmc.dat datafile already
    #  exists, if so, then we can ask the user whether or not
    #  to clean the previous installation.
    #

    if [ -f /var/opt/SUNWsymon/SunMCDB02/oradata/$SMCDB_DATABASE/sunmc.dat ]
    then
      #
      # allow user to choose whether or not to cleandb prior to object creation
      #
      preserve_data_answered=0
      while [ $preserve_data_answered -eq 0 ]; do
          echolog ""
          ask_user "Do you want to preserve your existing data"
          do_it=$answer
          if [ $do_it -eq 1 ]
          then
              echolog ""
	          echolog "Please wait, Sun Management Center database setup in progress. It may take 15 to 20 minutes"
              preserve_data_answered=1
              if [ $VERBOSE = "ON" ] ; then
                  ./build-smc-oracle-recovery.ksh -au $SMCDB_USER -ap $SMCDB_PASSWORD -wu sunmcu -wp sunmcu -sp manager -s $SMCDB_DATABASE -tsn sunmc -tsp /var/opt/SUNWsymon/SunMCDB02/oradata/$SMCDB_DATABASE -tsis 100 -olfp $VARDIR -ver -lf "$LOGFILE"
                  retcode=$?
              else
                  ./build-smc-oracle-recovery.ksh -au $SMCDB_USER -ap $SMCDB_PASSWORD -wu sunmcu -wp sunmcu -sp manager -s $SMCDB_DATABASE -tsn sunmc -tsp /var/opt/SUNWsymon/SunMCDB02/oradata/$SMCDB_DATABASE -tsis 100 -olfp $VARDIR -lf "$LOGFILE"
                  retcode=$?
              fi
              if [ $retcode -ne 0 ]; then
                  echolog "Database setup failed : build-smc-oracle-recovery.ksh failed"
                  exit 1
              fi
          else
             echo ""
             echolog 'This will remove all the existing data. If you wish to keep a copy of your'
		     echolog 'data, please manually run es-backup script 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
                echolog ""
	            echolog "Please wait, Sun Management Center database setup in progress. It may take 15 to 20 minutes"
                if [ $VERBOSE = "ON" ] ; then
                    ./build-smc-oracle-recovery.ksh -au $SMCDB_USER -ap $SMCDB_PASSWORD -wu sunmcu -wp sunmcu -sp manager -s $SMCDB_DATABASE -tsn sunmc -tsp /var/opt/SUNWsymon/SunMCDB02/oradata/$SMCDB_DATABASE -tsis 100 -olfp $VARDIR -cdb -ver -lf "$LOGFILE"
                    retcode=$?
                else
                    ./build-smc-oracle-recovery.ksh -au $SMCDB_USER -ap $SMCDB_PASSWORD -wu sunmcu -wp sunmcu -sp manager -s $SMCDB_DATABASE -tsn sunmc -tsp /var/opt/SUNWsymon/SunMCDB02/oradata/$SMCDB_DATABASE -tsis 100 -olfp $VARDIR -cdb -lf "$LOGFILE"
                    retcode=$?
                fi
                if [ $retcode -ne 0 ]; then
                    echolog "Database setup failed : build-smc-oracle-recovery.ksh failed"
                    exit 1
                fi

                # remove specific files
                remove_dat_files

             fi
          fi
        done
    else
	      echolog "Please wait, Sun Management Center database setup in progress. It may take 15 to 20 minutes"
          if [ $VERBOSE = "ON" ] ; then
              ./build-smc-oracle-recovery.ksh -au $SMCDB_USER -ap $SMCDB_PASSWORD -wu sunmcu -wp sunmcu -sp manager -s $SMCDB_DATABASE -tsn sunmc -tsp /var/opt/SUNWsymon/SunMCDB02/oradata/$SMCDB_DATABASE -tsis 100 -olfp $VARDIR -ver -lf "$LOGFILE"
              retcode=$?
          else
              ./build-smc-oracle-recovery.ksh -au $SMCDB_USER -ap $SMCDB_PASSWORD -wu sunmcu -wp sunmcu -sp manager -s $SMCDB_DATABASE -tsn sunmc -tsp /var/opt/SUNWsymon/SunMCDB02/oradata/$SMCDB_DATABASE -tsis 100 -olfp $VARDIR -lf "$LOGFILE"
              retcode=$?
          fi
          if [ $retcode -ne 0 ]; then
              echolog "Database setup failed : build-smc-oracle-recovery.ksh failed"
              exit 1
          fi
    fi


    # Add DE addon modules to database
    $BASEDIR/sbin/es-add-addon-modules

    #
    #  Run the db import for db only
    #

    if [ "$RUN_DB_IMPORT" = "YES" ]
    then
       if [ -f $ESROOT/sbin/db-import.sh ]
       then
          cd $ESROOT/sbin
          if [ $VERBOSE = "ON" ] ; then
              $ESROOT/sbin/db-import.sh -v
          else
              $ESROOT/sbin/db-import.sh
          fi
       else
         echolog 'cannot find the import script file $2' "$ESROOT/sbin/db-import.sh"
       fi
  
    fi


        #
        # execute script for suggested fix
        #
        if [ "$VERBOSE" = "ON" ] ; then
            $BASEDIR/sbin/es-sfix.sh
        else
            $BASEDIR/sbin/es-sfix.sh > /dev/null 2>&1
        fi


       prstring=`$ESROOT/base/bin/sparc-sun-solaris${OSVERS}/smcpwchk $SMCORAU 2>/dev/null`

       retcode=$?

       if [ $retcode = 2 ]
       then
          echolog 'the user $2 was not found by a getpwnam system call ,not in /etc/passwd, nis, nis+, etc..' "$SMCORAU"
          echolog 'user must manually deal with user issues before continuing'
          echolog 'cannot perform a db-stop, exiting.............................'
          exit 1
       fi

       grstring=`$ESROOT/base/bin/sparc-sun-solaris${OSVERS}/smcgrchk $SMCORAG 2>/dev/null`

       retcode=$?

       if [ $retcode = 2 ]
       then
          echolog 'the group $2 was not found by a getgrnam system call ,not in /etc/group, nis, nis+, etc..' "$SMCORAG"
          echolog 'user must manually deal with group issues before continuing'
          echolog 'cannot perform a db-stop, exiting.............................'
          exit 1
       fi


	#
	# Stop the database server
	#
        if [ $VERBOSE = "ON" ] ; then
#	    $BASEDIR/sbin/db-stop -v
            su - $SMCORAU -c $BASEDIR/sbin/db-stop -v
            retcode=$?
        else
#	    $BASEDIR/sbin/db-stop
            su - $SMCORAU -c $BASEDIR/sbin/db-stop
            retcode=$?
        fi
	if [ $retcode -ne 0 ]; then
	    echolog "Database setup failed : db-stop failed"
	    exit 1
	fi
fi



echologverbose ""
echologverbose ""
echologverbose 'Setup log stored in $2' $LOGFILE

echologverbose ""

#
#Setup has been successful so put this information into cfginfo file.
#

/usr/bin/touch  "$BASEDIR/sbin/.cfginfo"
put_var_in_cfgfile  "$BASEDIR/sbin/.cfginfo" "SETUP_COMPONENTS" "$SETUP_LIST" "="
if [ $? -ne 0 ] ; then
    echolog 'Error: It seems the file $2 is not writable, could not update the file.' "$BASEDIR/sbin/.cfginfo"
    exit 1
fi
/usr/bin/chmod 644 "$BASEDIR/sbin/.cfginfo"

###########################################################
# Start the product only if the Server component is
# setup because es-start -A has no significance
# for console only install/setup and for a agent only
# install/setup the context of agent isn't certain.

echo $SETUP_LIST | /usr/bin/grep -iw Server  > /dev/null 2>&1
if [ $? -eq 0 ]
then
        ask_user 'Do you want to start Sun Management Center agent and server components now'
        if [ $answer -eq 1 ]; then
#	    echolog "Please wait ..."
#		/usr/bin/sleep 60
            prog_base=`/usr/bin/pkgparam SUNWescom BASEDIR`
            start_cmd="$prog_base/SUNWsymon/sbin/es-start"
            $start_cmd -A
        fi
else
    echo $SETUP_LIST | /usr/bin/grep -iw Agent  > /dev/null 2>&1
    if [ $? -eq 0 ] ; then
        ask_user 'Do you want to start Sun Management Center agent now'
        if [ $answer -eq 1 ]; then
            $BASEDIR/sbin/es-start -a > /dev/null 2>&1

	    CONF_FILE=$CONFDIR/symon.conf
	    if [ -f "$CONF_FILE" ] ; then
		/usr/bin/grep platform "$CONF_FILE" > /dev/null
		if [ $? -eq 0 ] ; then
                    $BASEDIR/sbin/es-start -l > /dev/null 2>&1
                fi
	    fi

        fi
    fi

fi


exit 0
