#!/bin/ksh
#File-name: analyzeALL.ksh
#--------------------------------------------------------------
# Author: Larry Roberts 7/23/2004
# E-mail: lroberts@tandbergtv.com
# W-page: http://www.tandbergtv.com
#--------------------------------------------------------------

#---------------------------------------------------------------------
#--- Info highlighted like this is the start of a function -----------
#---------------------------------------------------------------------
#
#----------------------------------------------
# Info highlighted like this is for doco only
#----------------------------------------------
#

export analyzeDir=/usr/local/n2bb/backup/scripts

if test -f /var/opt/oracle/oratab
then
    ORATAB=/var/opt/oracle/oratab
fi
if test -f /etc/oratab
then
    ORATAB=/etc/oratab
fi
export ORATAB

#-----------------------------------------------------------------
#------------------- Analyze Objects------------------------------
#-----------------------------------------------------------------
##
function analyzeObjects
{
## analyze a schema's tables and indexes

##

echo "Please wait - Analyzing schemas objects"

sqlplus /nolog <<EOM
   connect / as sysdba
   spool analyzeALL.log
   select 'STARTED AT '||to_char(sysdate,'MM/DD/YYYY HH24:MI:SS') "Start Date"
    from dual;
   PROMPT Creating the analyzeALL procedure for $ORACLE_SID PLEASE WAIT
   @${analyzeDir}/analyzeALL.sql
   PROMPT Executing the analyzeALL procedure for $ORACLE_SID PLEASE WAIT
   set serveroutput on
   execute analyzeALL;
   select 'COMPLETED AT '||to_char(sysdate,'MM/DD/YYYY HH24:MI:SS') "Start End"
    from dual;
   spool off
   exit
EOM

} #END analyzeObjects
#------------------------------------------------------------------------
#----------------------- Main Search of Oratab --------------------------
#------------------------------------------------------------------------
cat $ORATAB | while read LINE
do
    case $LINE in
        \#*)                ;;        #comment-line in oratab
        *)
#       Proceed only if third field is 'Y'.
        if [ "`echo $LINE | awk -F: '{print $3}' -`" = "Y" ] ; then
            ORACLE_SID=`echo $LINE | awk -F: '{print $1}' -`
            if [ "$ORACLE_SID" = '*' ] ; then
                ORACLE_SID=""
            fi
#           Called programs use same database ID
            export ORACLE_SID
            ORACLE_HOME=`echo $LINE | awk -F: '{print $2}' -`
#           Called scripts use same home directory
            export ORACLE_HOME
#           Put $ORACLE_HOME/bin into PATH and export.
            PATH=$ORACLE_HOME/bin:/bin:/usr/bin:/etc ; export PATH
            LD_LIBRARY_PATH=${ORACLE_HOME}/lib ; export LD_LIBRARY_PATH
            analyzeObjects
        fi
    esac
done

echo "--------------------------------------------------------------"
echo "If any error other than -ANALYZE completed SUCCESSFULLY- "
echo "appears then must correct the analyzeALL.sql script and rerun "
echo "Check the analyzeALL.log to make sure it has this message.    "
echo "--------------------------------------------------------------"

