#!/bin/sh

# Copyright 05/18/01 Sun Microsystems, Inc. All Rights Reserved.
#
# pragma ident  "@(#)es-db-patch	1.3 01/05/18 Sun Microsystems"
#

###########################################################
#
# Procedure that inserts stored procedure into database
#
# Input : absolute filename of stored procedure file 
#	  
# e.g. - db/eventmanager/procedures/hostIns.sql
#
# Parameters: $1 is the sub-directory where the patch file is stored,
#                with a trailing /
#             $2 is the file name of the patch file
#             $3 is a valid logfile.  If this script needs a logfile,
#             then it can append to the file named in $3
#	
# The design of this script dictates that it must be installed
# in $ESROOT/sbin, because it needs to run es-common.sh
#	
###########################################################
patchStoredProc() {

	nameProc="$1"
	if [ -n "$nameProc" ]; then

		sqlstring="@${nameProc};"
		#echo "sqlstring = $sqlstring"
		result=`echo $sqlstring | $ORACLE_HOME/bin/sqlplus sunmc/sunmc | tee -a $2`
		#echo "result = $result"

		success=`echo $result | grep 'No errors'`
		if [ "$success" ] ; then
			echologverbose "Successfully added : $nameProc"
		else
			echolog "FAILED to add : $nameProc"
		fi
	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

PROGNAME=$0
PATCHFILE=$1$2
NEWPATCHFILESUFFIX=`/usr/bin/date | /usr/bin/awk '{printf("%s-%d-%d-%s\n",$2,$3,$6,$4); }'`
NEWPATCHFILE="$PATCHFILE-$NEWPATCHFILESUFFIX"

#echo "Patchfile = $PATCHFILE"
#echo "NewPatchfilesuffix = $NEWPATCHFILESUFFIX"
#echo "NewPatchfile = $NEWPATCHFILE"
#echo "$ESROOT"

#
# move packing list to $VAROPTDIR
#
if [ ! -d "$1" ]
then
    mkdir $1
    echologverbose "Successfully created "$1
fi

#
# determine if the patch file exists
#
if [ ! -f "$1$2" ]
then
    cp $ESROOT/db/patch/$2 $1$2
    echologverbose "Successfully copied packing list of patches."
fi

#
# determine if there are files to patch
#
if [ ! -f "$1$2" ]
then
    echolog "File which should contain list of patches does not exist."
    exit 1
fi

#
# determine if the patch file is readable
#
if [ -s "$1$2" ]
then

    #
    # determine if database if running
    #
    pscount=`/usr/bin/ps -ef | /usr/bin/grep -c smcorau`
    if [ "$pscount" -lt 9 ]
    then
        echolog "Cannot update stored procedures as database is not available"
        exit 3
    fi

    #
    # determine if services that use the database are running
    #
    pscount=`/usr/ucb/ps -ww -a -g -x | egrep '(init topology|init event|com.sun.symon.base.mgmtservice.main.SunMCServices)'|fgrep -v egrep`
    if [ "$pscount" -gt 0 ]
    then
        echolog "es-stop -A needs to be run before installing the database patch.  Exiting."
        exit 3
    fi

    #
    # create new file of updated stored procedures
    #
    touch $NEWPATCHFILE
    if [ $? -ne 0 ]
    then
        echolog "Unable to create file $NEWPATCHFILE"
        exit 4
    fi

    for file in `/usr/bin/grep -v '^#' $PATCHFILE`
    do
        echolog "$file" >> $NEWPATCHFILE
        if [ -n "$file" ] ;
        then
            patchStoredProc "$ESROOT/$file" "$3"
        fi
    done

    cat /dev/null > $PATCHFILE
    if [ $? -ne 0 ]
    then
        echolog "Unable to remove file $Patchfile"
        exit 5
    fi

    x=`$ORACLE_HOME/bin/sqlplus -s sunmc/sunmc @validate-status|tee -a $3`
    echologverbose "Results of validation: "$x
    if [ "$x" != "There are 290 valid objects." ]; then
        echolog "Patch failed."
        rm -rf $1
        exit 5
    else
        echologverbose "Patch successful."
    fi

fi
exit 0
