#! /bin/sh
#
# ident	"@(#)es-start	1.79	03/03/05 SMI"
#
# Copyright 2003 Sun Microsystems, Inc.  All rights reserved.
# Use is subject to license terms.
#

print_usage() {
    echo ""
    eval echo `/usr/bin/gettext 'Usage: $PROGNAME -acefghilmpstwxrAS [-y instanceName] [ -- args... ]'`
    /usr/bin/gettext "   where the options represent:\n"
    /usr/bin/gettext "     a : starts Sun Management Center Agent\n"
    /usr/bin/gettext "     c : starts Sun Management Center Console\n"
    /usr/bin/gettext "         Optionally, maximum heap size could be customized,\n"
    /usr/bin/gettext "         e.g., ./es-start -c -- -Xmx100m specifies 100 MBytes\n"
    /usr/bin/gettext "         as max heap size; Default size is 64m.\n"
    /usr/bin/gettext "     e : starts Sun Management Center Event manager\n"
    /usr/bin/gettext "     f : starts Sun Management Center Configuration manager\n"
    /usr/bin/gettext "     g : starts Sun Management Center Grouping services\n"
    /usr/bin/gettext "     h : prints this usage report\n"
    /usr/bin/gettext "     i : start agent in interactive mode, valid only for one of\n"
    /usr/bin/gettext "           the a, t, f, p, and e options\n"
    /usr/bin/gettext "     l : starts Platform Agent for SSP\n"
    /usr/bin/gettext "     m : starts Sun Management Center Metadata Repository\n"
    /usr/bin/gettext "     q : start components in quiet mode, effective only for one of\n"
    /usr/bin/gettext "       the A, a, t, f, p, and e options\n"
    /usr/bin/gettext "     p : starts Sun Management Center Topology manager\n"
    /usr/bin/gettext "     r : starts Hardware Diagnostic Suite Service\n"
    /usr/bin/gettext "     s : starts Sun Management Center Server\n"
    /usr/bin/gettext "         Optionally, maximum heap size could be customized,\n"
    /usr/bin/gettext "         e.g., ./es-start -s -- -Xmx100m specifies 100 MBytes\n"
    /usr/bin/gettext "         as max heap size; Default size is 64m.\n"
    /usr/bin/gettext "     t : starts Sun Management Center Trap Handler\n"
    /usr/bin/gettext "     x : starts CST Service\n"
    /usr/bin/gettext "     y : starts new instance of platform agent\n"
    /usr/bin/gettext "     w : starts web server\n"
    /usr/bin/gettext "     A : starts All Sun Management Center components except the console,\n"
    fmt=`/usr/bin/gettext "Equivalent to %s -aefgmprstxw"`
    /usr/bin/printf "            $fmt\n" $PROGNAME
    /usr/bin/gettext "     S : starts Sun Management Center Server and all the server subcomponents\n"
    fmt=`/usr/bin/gettext "Equivalent to %s -efgmprstxw"`
    /usr/bin/printf "            $fmt\n" $PROGNAME
    /usr/bin/gettext "     args are passed to console or server when started.\n"
    /usr/bin/gettext "           (for example: es-start -c -- -p 2099)\n\n"
    echo ""
}

component_missing() {
    echo ""
    eval echo `/usr/bin/gettext '$1 Component not installed'`
    echo ""
    [ $continue_flag -eq 0 ] && exit 1
    #exit 1
}

start_all_platform_instances ()
{
    if [ ! -f "${VARDIR}/platform-instances.dat" ] ; then
        return 0
    fi
    echologverbose "Starting all platform instances"
    list_of_instances=""
    list_of_instances=`$XGET ${VARDIR}/platform-instances.dat ControlBlock.instances`
    echologverbose 'Instances are $2' "$list_of_instances"
    if [ -z "$list_of_instances" ] ; then
        return 0
    fi
    list_of_instances=`echo $list_of_instances | /usr/bin/sed -e "s/,/ /g"`

    for each_instance in $list_of_instances
    do
        echolog 'Starting platform instances $2' "$each_instance"
        if [ $quiet_flag -eq 1 ] ; then
            es-start.sh $each_instance -q &
        else
            es-start.sh $each_instance -q &
        fi

    done

}

start_proc() {
#
#  $1 = flag value
#  $2 - component name
#

    [ $1 -eq 0 ] && return

    if [ $2 = "all_platform_instance" ] ; then
        start_all_platform_instances
        return
    fi

    if [ $2 = "service" ] ; then
       
        # Fetch service registration port from javaserver.properties file
        #
        service_port="5600"
        if [ -f ${VAROPTDIR}/cfg/javaserver.properties ] ; then
           service_port=`/usr/bin/cat ${VAROPTDIR}/cfg/javaserver.properties | /usr/bin/grep "ServiceRegistryPort" | /usr/bin/cut -f2 -d=`
           [ -z "$service_port" ] && service_port="5600"
        fi
	i=1
	while [ $i -lt 20 ] 
	do	
	    sleep 2
	    /usr/bin/gettext " Waiting for service registration...\n"
	    is_port_open $service_port
	    [ $? -eq 1 ] && break
	    i=`expr $i + 1`
	done     
	is_port_open $service_port
	if [ $? -eq 0 ] ; then
	   echo "Can not find java server!!!"
	   return 1 
	fi
	$BASEDIR/classes/base/service/bin/es-service.sh $ARGUMENT
	return 0 
    fi

    if [ $2 = "metadata" ] ; then
        ESMODULES=$ESDIR/mdr/modules:mdr/modules
        export ESMODULES
        set_paths
    else
        if [ -n "$ESMODULES" ]
        then
            unset ESMODULES
            set_paths
        fi
    fi

    if [ $2 = "agent" -o $2 = "metadata" ] ; then
	[ -n "$LANG" ] && temp_lang=$LANG
        [ -n "$LC_ALL" ] && temp_lc=$LC_ALL
        LANG=C
        export LANG
        LC_ALL=C
        export LC_ALL
    fi

    if [ $2 = "event" -o $2 = "topology" ] ; then
	#
	# Path to SunMC Database Library
	#
	LD_LIBRARY_PATH=$LD_LIBRARY_PATH:$BASEDIR/SunMCDB01/app/oracle/product/8.1.6/lib
	export LD_LIBRARY_PATH
    fi

    if [ $2 = "hdservice" ]; then
        HWDS_CMD=`/usr/bin/pkginfo -r SUNWed`/SUNWsymon/addons/AdvancedMonitoring/sbin/start-diag
        if [ -x "$HWDS_CMD" ]; then    
            # Starting the Hardware Diagnostic Suite Service
            $HWDS_CMD > /dev/null
        else
            eval echo `/usr/bin/gettext 'ERROR: Could not start Hardware Diagnostic Suite Service.'`
        fi
        return
    fi

    if [ $interactive_flag -eq 1 ] ; then
	if [ $quiet_flag -eq 1 ] ; then
	    es-start.sh $2 -i -q
        else
	    es-start.sh $2 -i
	fi
	exit $?
    else
	if [ $quiet_flag -eq 1 ] ; then
	    es-start.sh $2 -q > /dev/null 2>&1  &
        else
	    es-start.sh $2 -q > /dev/null 2>&1  &
	fi
    fi
    if [ $2 = "agent" -o $2 = "metadata" ] ; then
        if [ -n "$temp_lang" ]; then
           LANG=$temp_lang
        else
           unset LANG
        fi
        export LANG
 
        if [ -n "$temp_lc" ]; then
           LC_ALL=$temp_lc
        else
           unset LC_ALL
        fi
        export LC_ALL
    fi
}

check_setup() {
#
#  $1 = filename to check
#

    if [ ! -f "$1" ] ; then
	echo ""
	/usr/bin/gettext "ERROR: Sun Management Center has not been set up.\nRun es-setup to set up Sun Management Center."
	echo ""
	exit 1
    fi

}
check_setup_but_dont_exit ()
{

    if [ ! -f "$1" ] ; then
        echo ""
        echolog 'It seems component $2 has not been setup, so skipping it.' "$2"
        echo ""
        return 1
    fi
    return 0
}

start_web_server ()
{
    option=$1
    /usr/bin/pkginfo -qi SUNWesweb  > /dev/null 2>&1
    if [ $? -ne 0 ] ; then
        [ "$option" = "w" ] && echolog "Sun Management Center Web Server is not installed, Can not start web server\n"
        return 0
    fi
    webserver_start_command=$BASEDIR/netscape/https-localhost/start
    if [ ! -x $webserver_start_command ] ; then
        [ "$option" = "w" ] && echolog "Sun Management Center Web Server is not completely installed, Can not start web server\n"
        return 0
    fi
    $webserver_start_command > /dev/null 2>&1
    sleep 1
    return 0

}
set_setup_components () {

    SETUP_COMPONENTS=`get_value_from_cfgfile  "$BASEDIR/sbin/.cfginfo" "SETUP_COMPONENTS" "="`
    if [ $? -ne 0 ] ; then
	/usr/bin/gettext "Sun Management Center is not setup. Please setup first and then run es-start."
        echo ''
	exit
    fi
    echo "$SETUP_COMPONENTS" | /usr/bin/grep -i -w agent > /dev/null 2>&1
    if [ $? -eq 0 ] ; then
	agent_setup=1
    fi
    echo "$SETUP_COMPONENTS" | /usr/bin/grep -i -w server > /dev/null 2>&1
    if [ $? -eq 0 ] ; then
	server_setup=1
    fi
    echo "$SETUP_COMPONENTS" | /usr/bin/grep -i -w console > /dev/null 2>&1
    if [ $? -eq 0 ] ; then
	console_setup=1
    fi
    echo "$SETUP_COMPONENTS" | /usr/bin/grep -i -w services > /dev/null 2>&1
    if [ $? -eq 0 ] ; then
	service_setup=1
    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

set_basedir
set_db_env

#
# Set the ulimit hard limit to 1024 if OS is 2.9. This is to make
# discovery 2.8 binary work on solaris 9. On 2.8 the default hard
# limit is 1023 and on 2.9 it is 65536. Discovery uses snmpget which
# uses select system call which is passed harlimit. The select does 
# not expect large number so discovery fails.
# I am not seting it only for 2.9 because on 2.5.1, 2.6, 2.7 and 2.8
# it is by default 1024
#
ulimit -H -n 1024

/usr/bin/pkginfo -qi SUNWesagt
if [ $? -eq 0 ]; then
    set_xput
fi

ESROOT=$BASEDIR; export ESROOT

cd "$BASEDIR"
PWD="$BASEDIR"; export PWD
PROGNAME=$0; export PROGNAME
 
if [ $# -eq 0 ] ; then
    echo ""
    /usr/bin/gettext "Please specify a component to be started."
    print_usage
    exit 1
fi

case $1 in
    -metadata | -console | -server | -agent  | -service | -cfgserver | -trap | \
    -topology | -event | -cstservice | -serverall | -all | -event | -help)
	echo ""
	eval echo `/usr/bin/gettext 'Invalid option\\\($1\\\).  Options have changed to single letters:'`
	/usr/bin/gettext "Use the following single letter arguments instead:\n"
	echo "   -agent         -a"
	echo "   -all           -A"
	echo "   -cfgserver     -f"
	echo "   -console       -c"
	echo "   -event         -e"
	echo "   -service       -g"
	echo "   -cstservice    -x"
	echo "   -help          -h"
	echo "   -server        -s"
	echo "   -serverall     -S"
	echo "   -topology      -p"
	echo "   -trap          -t"
	echo "   -metadata      -m"
	echo ""
	exit 1
	;;
esac
    
set_inst_flags

interactive_flag=0
quiet_flag=0
multi_flag=0
do_console=0
do_server=0
do_trap=0
do_agent=0
do_service=0
do_topo=0
do_cfg=0
do_event=0
do_metadata=0
do_cstservice=0
do_platform_instance=0
do_all_platform_instances=0
num_comp=0
do_platform=0
do_hwds=0
continue_flag=0
VERBOSE=OFF

server_setup=0
agent_setup=0
console_setup=0
service_setup=0

set_setup_components 

while getopts y:cgsmatpfexwAShilqvrY OPT
do
    case $OPT in
	c)
	    if [ $no_console -eq 1 ] ; then
		echo ""
		/usr/bin/gettext "Console package not installed\n"
		echo ""
		exit 1
	    fi
            if [ $console_setup -eq 0 ] ; then
		echolog 'Sun Management Center $2 is not setup.' "Console"
		exit 1
            fi
	    do_console=1
	    ;;
	s)
	    [ $no_server -eq 1 ] && component_missing Server
	    check_setup $VARDIR/server-global-keys.x
	    check_root
	    do_server=1
	    num_comp=`/usr/bin/expr $num_comp + 1`
	    ;;
	g)
	    /usr/bin/pkginfo -qi SUNWessvc
	    if [ $? -eq 0 ]; then
		check_root
		if [ $server_setup -eq 0 ] ; then
		    echolog 'Sun Management Center $2 is not setup.' "Server"
		    exit 1
		fi
		do_service=1
		num_comp=`/usr/bin/expr $num_comp + 1`
	    fi
	    ;;
	m)
	    [ $no_metadata -eq 1 ] && component_missing Metadata
	    check_setup $VARDIR/metadata-usmusertbl-d.dat 
	    check_root
	    do_metadata=1
	    num_comp=`/usr/bin/expr $num_comp + 1`
	    ;;
	a)
	    [ $no_agent -eq 1 ] && component_missing Agent
	    check_setup $VARDIR/agent-usmusertbl-d.dat
	    check_root
	    do_agent=1
	    num_comp=`/usr/bin/expr $num_comp + 1`
	    ;;
	t)
	    [ $no_server -eq 1 ] && component_missing Server
	    check_setup $VARDIR/trap-usmusertbl-d.dat
	    check_root
	    do_trap=1
	    num_comp=`/usr/bin/expr $num_comp + 1`
	    ;;
	p)
	    [ $no_server -eq 1 ] && component_missing Server
	    check_setup $VARDIR/topology-usmusertbl-d.dat
	    check_root
	    do_topo=1
	    num_comp=`/usr/bin/expr $num_comp + 1`
	    ;;
	f)
	    [ $no_server -eq 1 ] && component_missing Server
	    check_setup $VARDIR/cfgserver-usmusertbl-d.dat
	    check_root
	    do_cfg=1
	    num_comp=`/usr/bin/expr $num_comp + 1`
	    ;;
	e)
	    [ $no_server -eq 1 ] && component_missing Server
	    check_setup $VARDIR/event-usmusertbl-d.dat
	    check_root
	    do_event=1
	    num_comp=`/usr/bin/expr $num_comp + 1`
	    ;;
	x)
	    [ $no_server -eq 1 ] && component_missing Server
	    check_setup $VARDIR/cstservice-usmusertbl-d.dat
	    check_root
            /usr/bin/pkginfo -qi SUNWcstv
            cstv=$?
            /usr/bin/pkginfo -qi SUNWcstve
            cstve=$?
            [ $cstv -eq 0 ] && do_cstservice=1
            [ $cstve -eq 0 ] && do_cstservice=1
            if [ $cstv -eq 1 ] && [ $cstve -eq 1 ] ; then
		echo ""
		eval echo `/usr/bin/gettext 'SUNWcstv or SUNWcstve package should be installed before start cstservice agent.'`
		echo ""
		exit 1
	    fi
	    num_comp=`/usr/bin/expr $num_comp + 1`
	    ;;
	w)
	    if [ $server_setup -eq 0 ] ; then
		echolog 'Sun Management Center $2 is not setup.' "Server"
		exit 1
	    fi
            start_web_server w
	    num_comp=`/usr/bin/expr $num_comp + 1`
            ;;
    	r)
	    [ $no_server -eq 1 ] && component_missing Server
	    check_root
            /usr/bin/pkginfo -qi SUNWed
            if [ $? -eq 0 ]; then
		if [ $server_setup -eq 0 ] ; then
		    echolog 'Sun Management Center $2 is not setup.' "Server"
		    exit 1
		fi
            	do_hwds=1
            	num_comp=`/usr/bin/expr $num_comp + 1`
       	    else
            	echo ""
            	eval echo `/usr/bin/gettext 'SUNWed package should be installed before start Hardware Diagnostic Suite Service.'`
            	echo ""            
            fi
            ;;
	A | S)
	    if [ $agent_setup -eq 0 ] ; then
		echolog 'Sun Management Center $2 is not setup.' "Agent and Server"
		exit 1
	    fi
	    if [ $interactive_flag -eq 1 ] ; then
		echo ""
		eval echo `/usr/bin/gettext 'Interactive option invalid with $OPT option'`
		echo ""
		exit 1
	    fi
	    check_root
	    #[ $no_server -eq 1 ] && component_missing Server
	    if [ $no_server -eq 1 ] ; then 
	        continue_flag=1	
		component_missing Server
	    else
                start_web_server A

                check_setup_but_dont_exit $VARDIR/trap-usmusertbl-d.dat "Trap Handler"
                if [ $? -eq 0 ] ; then
                    do_trap=1
                fi
                check_setup_but_dont_exit $VARDIR/cfgserver-usmusertbl-d.dat "Config Server"
                if [ $? -eq 0 ] ; then
                    do_cfg=1
                fi
                /usr/bin/pkginfo -qi SUNWcstv
                [ $? -eq 0 ] && do_cstservice=1
                /usr/bin/pkginfo -qi SUNWcstve
                [ $? -eq 0 ] && do_cstservice=1
                check_setup_but_dont_exit $VARDIR/event-usmusertbl-d.dat "Event Manager"
                if [ $? -eq 0 ] ; then
                    do_event=1
                fi
                check_setup_but_dont_exit $VARDIR/topology-usmusertbl-d.dat "Topology Manager"
                if [ $? -eq 0 ] ; then
                    do_topo=1
                fi
                check_setup_but_dont_exit $VARDIR/server-global-keys.x "Server"
                if [ $? -eq 0 ] ; then
                    do_server=1
                fi
	        /usr/bin/pkginfo -qi SUNWessvc
	        if [ $? -eq 0 ]; then
		    do_service=1
	        fi
            	/usr/bin/pkginfo -qi SUNWed
            	if [ $? -eq 0 ]; then
                    do_hwds=1
            	fi
	    fi	
            if [ $no_metadata -eq 1 ] ; then
                continue_flag=1
                component_missing "Metadata Repository"
            else
                check_setup_but_dont_exit $VARDIR/metadata-usmusertbl-d.dat "Metadata Repository"
                if [ $? -eq 0 ] ; then
                    do_metadata=1
                fi
            fi

	    if [ $OPT = "A" ] ; then
		#[ $no_agent -eq 1 ] && component_missing Agent
		if [ $no_agent -eq 1 ] ; then
		    continue_flag=1
	            component_missing Agent
		else
		    check_setup_but_dont_exit $VARDIR/agent-usmusertbl-d.dat "Sun Management Center Agent"
		    if [ $? -eq 0 ] ; then
			do_agent=1
		    fi
		    check_setup_but_dont_exit $VARDIR/platform-usmusertbl-d.dat "Sun Management Center Platform Agent"
		    if [ $? -eq 0 ] ; then
			CONF_FILE=$CONFDIR/symon.conf
			if [ -f "$CONF_FILE" ] ; then
			    /usr/bin/grep platform "$CONF_FILE" > /dev/null
			    [ $? -eq 0 ] && do_platform=1
			fi
		    fi
                    do_all_platform_instances=1
		fi
	    fi
	    multi_flag=1
	    ;;
	h)
	    print_usage
	    exit 0
	    ;;
	i)
	    if [ $multi_flag -eq 1 ] ; then
		echo ""
		/usr/bin/gettext "Interactive option invalid with -A or -S option"
		echo ""
		exit 1
	    fi
	    interactive_flag=1
	    ;;
	 l)
	    [ $no_agent -eq 1 ] && component_missing Agent
	    check_root
	    if [ $agent_setup -eq 0 ] ; then
		echolog 'Sun Management Center $2 is not setup.' "Agent"
		exit 1
	    fi
	    do_platform=1
	    num_comp=`/usr/bin/expr $num_comp + 1`
	    ;;
	 q)
            quiet_flag=1
	    ;;
     v)
	    VERBOSE=ON
	    ;;
     y)
	    check_root
	    platform_instance_name="$OPTARG"
            do_platform_instance=1
	    num_comp=`/usr/bin/expr $num_comp + 1`
	    ;;
	Y)
	    check_root
		do_all_platform_instances=1
	    num_comp=`/usr/bin/expr $num_comp + 1`
		;;		
	\?)
	    print_usage
	    exit 1
	    ;;
    esac
done

shift

if [ "$1" = "--" ] ; then
    shift
fi
ARGUMENT="$*"
if [ $num_comp -eq 0 ] && [ $multi_flag -eq 0 ] \
    && [ $do_console -eq 0 ] && [ $do_server -eq 0 ] ; then
    echo ""
    /usr/bin/gettext "You have not specified any components to run.\n"
    echo ""
    exit 1
fi

if [ $num_comp -gt 1 ] && [ $interactive_flag -eq 1 ] ; then
    echo ""
    /usr/bin/gettext "Only one component may be started in interactive mode\n"
    echo ""
    exit 1
fi

if [ $num_comp -eq 0 ] && [ $do_console -eq 1 ] && \
    [ $interactive_flag -eq 1 ] ; then
    echo ""
    /usr/bin/gettext "You cannot start console in interactive mode\n"
    echo ""
    exit 1
fi

unset_env_variables
set_paths

if [ $num_comp -gt 0 -o $multi_flag -eq 1 ] ; then
    #
    # Patch all the platform instances. This will transffer contents from 
    # base-config.x to user-config.x. It will do this only if base-config.x
    # is present in /var/opt/SUNWsymon/cfg.
    #
    $BASEDIR/sbin/es-platform -p
fi

if [ $do_server -eq 1 -o $do_console -eq 1 -o $do_service -eq 1 ] ; then
   checkJavaOK
   if [ $? -ne 0 ] ; then
      echolog ''
      [ -n "$JAVA_FULLVERSION" ] && echolog 'Found incomplete installation of $2' "$JAVA_FULLVERSION"
      echolog 'Sun Management Center requires complete installation of $2 or above.' "$JAVA_MINIMUM_REQUIRED"
      echolog 'Please follow the following steps before starting Sun Management Center.'
      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    
   JAVA=$SYMON_JAVAHOME/bin/java
fi

#
#  Need to start the console first since it is the only one
#  that cannot run in interactive mode.
#

if [ $do_console -eq 1 ] ; then
    if [ -z "$DISPLAY" ] ; then
	echo ""
	/usr/bin/gettext "DISPLAY environment variable must be set\n"
	echo ""
	exit 1
    fi
    XHOST=/usr/openwin/bin/xhost
    if [ -x $XHOST ] ; then
	$XHOST >/dev/null 2>&1
	if [ $? -ne 0 ] ; then
	    echo ""
	    eval echo `/usr/bin/gettext 'Unable to open DISPLAY: $DISPLAY'`
	    echo ""
	    exit 1
	fi
    fi
fi

[ $interactive_flag -eq 1 ] && [ $do_console -eq 1 ] && es-console.sh $* &

if [ $do_service -eq 1 -o $do_event -eq 1 -o $do_topo -eq 1 ] ; then
    #
    # Start the SunMC database
    #

    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.log ]
    then
      rm -rf /tmp/db-start-vinst.log
    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 '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 'exiting.............................'
       exit 1
    fi


     if [ "$VERBOSE" = "ON" ]; then
       su - $SMCORAU -c $BASEDIR/sbin/db-start -v
     else
       su - $SMCORAU -c $BASEDIR/sbin/db-start
     fi

    if [ $? -ne 0 ]
    then
      echolog "failed to successfully perform Database Startup,exiting................"
      exit 1
    fi

    if [ -n $VAROPTDIR/patch/ ]; then
        cd $ESROOT/db/build
		/usr/bin/rm -f $VAROPTDIR/install/dbpatch*
        check_logfile dbpatch
        $ESROOT/sbin/es-db-patch $VAROPTDIR/patch/ packing-list.txt "$LOGFILE"
        retcode=$?
        if [ $retcode -ne 0 ]; then
            echolog "Database patch failed"
            exit 1
        fi

        cd $ESROOT/sbin
    fi
fi

if [ $do_server -eq 1 ] ; then
    if [ $interactive_flag -eq 1 ] ; then
	/usr/bin/gettext "Interactive server session:\n"
	es-server.sh -i $*
    else
	es-server.sh $* &
    fi
fi

start_proc $do_hwds hdservice
start_proc $do_trap  trap
start_proc $do_cfg   cfgserver
start_proc $do_event event
start_proc $do_agent agent
start_proc $do_topo  topology
start_proc $do_cstservice cstservice
start_proc $do_platform platform
start_proc $do_platform_instance $platform_instance_name
start_proc $do_all_platform_instances "all_platform_instance"

# Delay metadata startup to ensure there is no contention 
if [ $do_metadata -eq 1 ]; then
	sleep 30
fi
start_proc $do_metadata metadata
start_proc $do_service service


if [ $do_topo -eq 1 ] && [ -f "${ESDIR}/delayConsole" ]; then
    i=1
    while [ $i -le 10 ]
    do
        /usr/bin/gettext "."
        sleep 15
        i=`expr $i + 1`
    done
    rm -rf ${ESDIR}/delayConsole
    /usr/bin/gettext "\n"
fi

[ $interactive_flag -eq 0 ] && [ $do_console -eq 1 ] && es-console.sh $*

exit 0
