#!/usr/bin/sh
# Copyright 09/20/02 Sun Microsystems, Inc. All Rights Reserved.
# "@(#)em_db_start	1.19 02/09/20 Sun Microsystems"
#

###############################################################################
# em_db_start
###############################################################################
#
# Start the database server.
#
###############################################################################

###############################################################################
# errmsg  
###############################################################################
#
# Echo the arguments.
#
###############################################################################

errmsg()
{
    if [ "$1" = "-n" ] ; then
        shift
        gettext em_db_start "$*"               # no newline
    else
        gettext em_db_start "$*\\n"
    fi
}


###############################################################################
# Environment
###############################################################################
#
# Set up the environment.
#
###############################################################################

trap 'errmsg "\\nem_db_start interrupted."; exit 1' 1 2 3 15

EX_STAT=1
 
EM_DB_HOME=${INFORMIXDIR:-/opt/SUNWemrdb}
INFORMIXDIR=$EM_DB_HOME

HOSTNAME=`$EM_HOME/bin/get_local_host | /bin/sed -e 's/-/_/g'`
EM_DB_SERVER=${INFORMIXSERVER:-${HOSTNAME}_emdb}
INFORMIXSERVER=$EM_DB_SERVER

EM_DB_NAME=${EM_DB_NAME:-emdb}
EM_MISDB_NAME=${EM_MISDB_NAME:-emmisdb}
ONCONFIG=onconfig.em

export EM_DB_HOME EM_DB_NAME EM_MISDB_NAME INFORMIXDIR INFORMIXSERVER ONCONFIG

PATH=$EM_DB_HOME/bin:/bin:/usr/bin:/usr/ucb:$PATH:
LD_LIBRARY_PATH=$EM_DB_HOME/lib:$EM_DB_HOME/lib/esql:/usr/openwin/lib:$LD_LIBRARY_PATH
SHLIB_PATH=$LD_LIBRARY_PATH

export PATH LD_LIBRARY_PATH SHLIB_PATH

###############################################################################
# progress
###############################################################################
#
# Mark the passage of time to convince the user we haven't really hung at all.
#
###############################################################################

progress()
{
    # marker for monitor
    cp /dev/null /tmp/emha_progress > /dev/null 2>&1

    if [ "${quiet:-FALSE}" = TRUE -o "${debug:-FALSE}" = TRUE ] ; then
	return
    fi

    progress_counter=`expr ${progress_counter:-1} + 1`   
    position=`expr $progress_counter % 4`

    if [ $position -eq 0 ] ; then
	echo '|\b\c'
    elif [ $position -eq 1 ] ; then
	echo '/\b\c'
    elif [ $position -eq 2 ] ; then
	echo '-\b\c'
    else
	echo '\\\b\c'
    fi
}


###############################################################################
# main
###############################################################################
#
# The main part of this script.
#
###############################################################################

#
# sqlhosts seems particularly vulnerable to system crashes.  If it looks
# corrupt, use the backup.  If there's no backup, or it's corrupt, try to
# create one.
#

if [ ! -s $EM_DB_HOME/etc/sqlhosts ] ; then
    if [ -s $EM_DB_HOME/etc/sqlhosts.bak ] ; then
	errmsg -n "$EM_DB_HOME"
	errmsg -n "/etc/sqlhosts is corrupted.  Using backup file "
	errmsg " $EM_DB_HOME/etc/sqlhosts.bak."
	cp $EM_DB_HOME/etc/sqlhosts.bak $EM_DB_HOME/etc/sqlhosts
    else
	errmsg -n "$EM_DB_HOME"
	errmsg "/etc/sqlhosts is corrupted.  Attempting to reconstruct it...."
	echo "$EM_DB_SERVER ontlitcp " `$EM_HOME/bin/get_local_host` "sqlexec" > \
	    $EM_DB_HOME/etc/sqlhosts
    fi
fi

if [ -x $EM_DB_HOME/bin/oninit ]; then 
    progress
    oninit -y > ${debug_device:-/dev/null} 2>&1
    EX_STAT=$?

    #
    # Wait for the database to come completely up.
    #

    num_retry_attempts=0
    while true; do
	progress
	dbstatus=`onstat -k`
	echo $dbstatus | grep -- '-- On-Line --' > ${debug_device:-/dev/null} 2>&1
	
	if [ $? -eq 0 ] ; then
        sleep 2
	    break					# done initializing
	fi

	echo $dbstatus | grep 'not initialized' > ${debug_device:-/dev/null} 2>&1

	if [ $? -eq 0 ] ; then
	    errmsg 'Database failed to initialize.'
	    EX_STAT=1					# server died
	fi

    echo $dbstatus | grep 'Fast Recovery' > ${debug_device:-/dev/null} 2>&1

    [ $? -eq 0 ] && continue


        num_retry_attempts=`expr $num_retry_attempts + 1`
        if [ $num_retry_attempts -ge 4 ] ; then
            errmsg 'Four attempts to start the database failed.'
            errmsg -n "Please see the file "
	    errmsg -n "$EM_RUNTIME"
	    errmsg "/debug/database.log for more details."
            exit $EX_STAT
        fi

	sleep 5
    done
else
    errmsg ""
    errmsg "ERROR: Unable to locate database server manager binary (oninit) "
    errmsg -n "in directory "
    errmsg -n "$EM_DB_HOME/bin. "
    errmsg "Database not started. "
    errmsg ""
fi

exit $EX_STAT
