#!/bin/sh
#
# Copyright 05/16/01 Sun Microsystems, Inc. All Rights Reserved.
#
# pragma ident  "@(#)es-add-module	1.3 01/05/16 Sun Microsystems"
#

#
# Procedure that performs the database addition
#
# Input : modulename moduleversion pack
#
# e.g. - Module1    1.0       Basic Pack
# e.g. - Module2    3.14156   Advanced Agent Pack
# e.g. - Module3    1.0       Advanced Server/Console Pack
# e.g. - Module4    1.0       New Pack
#
addModule() {

        modulename=`echo $1 | awk '{print $1}'`
        versionname=`echo $1 | awk '{print $2}'`
        packname=`echo $1 | awk '{ printf("%s",$3);for ( x = 4 ; x<=NF ; x++) { printf(" %s",$x); } }'`

#	echo "modulename  = ****$modulename****"
#	echo "versionname = ****$versionname****"
#	echo "packname    = ****$packname****"

        if [ -n "$modulename" -a -n "$versionname" -a -n "$packname" ]; then

                sqlstring="call sp_module_create('$modulename','$versionname','$packname');"
#                echo "sqlstring = $sqlstring"
                result=`echo $sqlstring | $ORACLE_HOME/bin/sqlplus sunmc/sunmc`
#                echo "result = $result"

                success=`/usr/bin/echo $result | grep 'Call completed'`
                if [ "$success" ] ; then
                        echolog '$2 $3 $4 - SUCCESSFULLY added' "$modulename" "$versionname" "$packname"
                else
                       	echolog '$2 $3 $4 - FAILED added' "$modulename" "$versionname" "$packname"
                fi
	else 
                echolog 'Invalid format modulename=$2 , versionname=$3 , packname=$4' "$modulename" "$versionname" "$packname"
        fi
}

addModulesFromFile() {

    FILENAME=$1

    NUMLINES=`/usr/bin/wc $FILENAME | awk '{print $1}'`
    counter=$NUMLINES;
    command=`sort -u -r $FILENAME | head -$counter | tail -1 | sed -e 's/   / /g' -e 's/  */ /g' -e '/^ $/d'`
    while [ $counter -gt 0 ]
    do
	if [ -n "$command" ] ;
	then
        	addModule "$command"
	fi

        counter=`expr $counter - 1`

        #
        # retrieve the next line
        #
        command=`sort -u -r $FILENAME | head -$counter | tail -1 | sed -e 's/   / /g' -e 's/  */ /g' -e '/^ $/d'`
    done


}

#
# beginning execution point of the script
#
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

PROGNAME=$0

if [ $# -ne 1 -a $# -lt 3 ] ; then
        echolog "   "
        echolog "Usage : es-add-module inputfile"
        echo    "        es-add-module modulename moduleversion packname"
        echolog " "
	echolog "eg. inputfile"
        echolog " "
	echo "Module1    1.0       Basic Pack"
	echo "Module2    3.14156   Advanced Agent Pack"
	echo "Module3    1.0       Advanced Server/Console Pack"
	echo "Module4    1.0       New Pack"
	echolog " "
        exit 1
fi

if [ $# -eq 1 ] ; then 
    FILENAME=$1

    if [ ! -f "$FILENAME" ] ; then
        echolog "   "
        echolog 'File : $2 not found' "$FILENAME"
        echolog ""
        exit 2
    fi
fi

#
# set listener count and SunMC oracle process count
#
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 -a $scnt -ge 8 ]
then
    echolog "Database is running.  Add new modules ..."
    if [ $# -eq 1 ] ; then	
        addModulesFromFile $FILENAME
    else
        addModule "$*"
    fi
    exit 0
fi

#
# check if server layer has been setup
#
isserversetup=`/usr/bin/grep '^SETUP_COMPONENTS=' $BASEDIR/sbin/.cfginfo | /usr/bin/grep server`
if [ -z "$isserversetup" ] ; then
    echolog "Server layer not setup.  Cannot execute $PROGNAME"
    exit 3
fi

#
# start database
#
su - $SMCORAU -c $BASEDIR/sbin/db-start
retcode=$?
if [ $retcode -ne 0 ] ; then
    echolog "Unable to start database"
    exit 4
fi

echolog "Database starting ..."

#
# allow database three minutes
# to start 
#
counter=0
while [ $counter -lt 36 ] 
do
    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 -a $scnt -ge 8 ]
    then

        echolog "Database started ..."
        if [ $# -eq 1 ] ; then	
            addModulesFromFile $FILENAME
        else
	    addModule "$*"
        fi

	#
	# stop database
	#
        su - $SMCORAU -c $BASEDIR/sbin/db-stop
        retcode=$?
        if [ $retcode -ne 0 ] ; then
            echolog "Unable to stop database"
            exit 5
        fi

        echolog "Database stopped ..."
        exit 0
    fi 

    counter=`/usr/bin/expr $counter + 1`
    sleep 5
done

#
# database did not start correctly
#
echolog "Database was unable to start correctly"

exit 7
