#!/bin/ksh
# Copyright 10/04/05 Sun Microsystems, Inc. All Rights Reserved.
#
# pragma ident  "@(#)db-stop	1.38 05/10/04   Sun Microsystems"
#

#
#
#  Utility functions to validate startup
#
#
#
#

# params:  

function getHostName
{
  currhost=$(hostname)

  retcode=$?

  if [ $retcode != 0 ]
  then
     echolog 'getHostName: failed to obtain current host from hostname comma\n'
     return $retcode
  fi
  
  echologverbose 'current host : $2\n' "$currho"

  return 0
}

# params:  

function getUserName
{
  username=$(id | cut -f1 -d" " | cut -f2 -d"(" | cut -f1 -d")")

  retcode=$?

  if [ $retcode != 0 ]
  then
     echolog "getUserName: failed to obtain user name from id comma\n"
     return $retcode
  fi
  
  echologverbose 'current user name : $2\n' "$userna"

  return 0
}


# params:  

function getUserGroup
{
  usergroup=$(id | cut -f2 -d" " | cut -f2 -d"(" | cut -f1 -d")")

  retcode=$?

  if [ $retcode != 0 ]
  then
     echolog "getUserGroup: failed to obtain user group from id comma\n"
     return $retcode
  fi

  echologverbose 'current user group : $2\n' "$usergro"

  return 0
}




# params:  pathtemp, filename

function isFileInPath
{

/usr/bin/gettext "path_app : ${path_app}\n"
echo
echo


found_app="false"


for dir in $1
do
#   echo "current subdir : " $dir

  if [ -f ${dir}/$2 ]
  then
    /usr/bin/gettext "found $2 on path in ${dir}\n"
    echo
    echo
    echo
    found_app="true"
    break 1
  fi
done




if [ $found_app != "true" ]
then
  /usr/bin/gettext 'can not find $2 on path, exiting.......................\n'
  echo
  echo
  echo
  return 1
fi
  return 0
}

# params:  sid

function verifyDatabaseUp
{
  proccnt=$(/usr/bin/ps -eaf | grep -i ora_.*$1 | grep -v "grep" | wc -l)

  retcode=$?

  if [ $retcode != 0 ]
  then
     echolog "verifyDatabaseUp: failed to obtain instance process info from ps\n"
     return $retcode
  fi
  
  if [ $proccnt -gt 0 ]
  then
     echologverbose "verifyDatabaseUp: instance is executing\n"
     return 0
  else
     echologverbose "verifyDatabaseUp: instance is not executing\n"
     return 1
  fi

  return 0
  
}



# params:  sid

function verifyDatabaseDown
{
  proccnt=$(/usr/bin/ps -eaf | grep -i ora_.*$1 | grep -v "grep" | wc -l)
  
  retcode=$?

  if [ $retcode != 0 ]
  then
     echolog "verifyDatabaseDown: failed to obtain instance process info from ps\n"
     return $retcode
  fi

  if [ $proccnt -gt 0 ]
  then
     echologverbose "verifyDatabaseDown: instance is executing\n"
     return 1
  else
     echologverbose "verifyDatabaseDown: instance is not executing\n"
     return 0
  fi

  return 0
  
}



# params:  lsnalias

function verifyListenerUp
{
  proccnt=$(/usr/bin/ps -u smcorau -o fname | grep -i tnslsnr | grep -v "grep" | wc -l)

  retcode=$?

  if [ $retcode != 0 ]
  then
     echolog "verifyListenerUp: failed to obtain listener process info from ps\n"
     return $retcode
  fi
  
  if [ $proccnt -gt 0 ]
  then
     echologverbose 'verifyListenerUp: listener $2 is executing\n' "$1"
     return 0
  else
     echologverbose 'verifyListenerUp: listener $2 is not executing\n' "$1"
     return 1
  fi

  return 0
  
}



# params:  lsnalias

function verifyListenerDown
{
  proccnt=$(/usr/bin/ps -u smcorau -o fname | grep -i tnslsnr | grep -v "grep" | wc -l)

  retcode=$?

  if [ $retcode != 0 ]
  then
     echolog "verifyListenerDown: failed to obtain listener process info from \n"
     return $retcode
  fi
  
  if [ $proccnt -gt 0 ]
  then
     echologverbose 'verifyListenerDown: listener $2 is executing\n' "$1"
     return 1
  else
     echologverbose 'verifyListenerDown: listener $2 is not executing\n' "$1"
     return 0
  fi

  return 0
  
}


###############################################
#
#   Main entry point
#
#

BASEDIR=`/usr/bin/pkgparam SUNWescom BASEDIR`
SCRIPT_DIR=${BASEDIR}/SUNWsymon/sbin

. ${SCRIPT_DIR}/es-common.sh



VERBOSE=OFF
LOGFILE=""
while getopts v OPT
do
    case $OPT in
        v)
            VERBOSE=ON
            ;;
        *)
            ;;
    esac
done

set_basedir
set_db_env

umask 022

CUSER=`get_user_id 2> /dev/null `
if [ "$CUSER" != "$SMCORAU" ] ; then
    echolog "You must logon as the user 'smcorau'\n"
    exit 1
fi

path_app=$(echo $PATH | sed 's/:/ /g')



if [ ! -f $ORACLE_HOME/bin/svrmgrl ]
then
  echolog
  echolog
  echolog 'the file $2 does not exist, exiting........................' "$ORACLE_HOME/bin/svrmgrl"
  echolog
  exit 1
fi


if [ ! -f $ORACLE_HOME/bin/lsnrctl ]
then
  echolog
  echolog
  echolog 'the file $2 does not exist, exiting........................' "$ORACLE_HOME/bin/lsnrctl"
  echolog
  exit 1
fi

 # if db is up shutdown

 verifyDatabaseDown $ORACLE_SID > ${TMP_SMC_DB_LOGS}/db-stop.log

 if [ $? -ne 0 ]
 then

   echologverbose
   echologverbose 'performing a \\\(shutdown immediate\\\)......'

   # Shuting down Oracle core dumps the shadow process of this session
   # Limit the core dump size
   ulimit -c 0

   $ORACLE_HOME/bin/svrmgrl <<EOF >> ${TMP_SMC_DB_LOGS}/db-stop.log
set stoponerror on
set echo off
set termout off
spool ${TMP_SMC_DB_LOGS}/db-stop-sql.log
connect sys/oracle as sysdba
shutdown immediate
quit
EOF

   if [ $? -ne 0 ]
   then
     echolog 'Stop database failed, see ${TMP_SMC_DB_LOGS}/db-stop.log\n'
     echolog
     echolog
     echolog "exiting.........................."

     #
     #  Append the contents of the sql log onto
     #  that of the current symon related log
     #

#     if [ -n $LOGFILE ] ; then
#       echo "=========================================================================" >> $LOGFILE
#       cat ${TMP_SMC_DB_LOGS}/db-stop.log >> $LOGFILE
#       echo "=========================================================================" >> $LOGFILE
#     fi

     exit 1

   fi

 fi


# if still up them shutdown abort

 verifyDatabaseDown $ORACLE_SID >> ${TMP_SMC_DB_LOGS}/db-stop.log

 if [ $? -ne 0 ]
 then

   echologverbose
   echologverbose "unable to perform normal shutdown, executing a shutdown abort"
   # Shuting down Oracle core dumps the shadow process of this session
   # Limit the core dump size
   ulimit -c 0

   $ORACLE_HOME/bin/svrmgrl  >> ${TMP_SMC_DB_LOGS}/db-stop.log << EOF
set stoponerror on
set echo off
set termout off
spool ${TMP_SMC_DB_LOGS}/db-stop-sql.log
connect sys/oracle as sysdba
shutdown abort
disconnect
quit
EOF

   if [ $? -ne 0 ]
   then
     echolog 'execution of $2 using adhoc shutdown-abort failed' "$ORACLE_HOME/bin/svrmgrl"
     echolog
     echolog
     echolog "exiting.........................."

     #
     #  Append the contents of the sql log onto
     #  that of the current symon related log
     #

#     if [ -n $LOGFILE ] ; then
#       echo "=========================================================================" >> $LOGFILE
#       cat ${TMP_SMC_DB_LOGS}/db-stop.log >> $LOGFILE
#       echo "=========================================================================" >> $LOGFILE
#     fi

     exit 1

   fi
 fi

 verifyListenerDown $SUNMCLISTENER >> ${TMP_SMC_DB_LOGS}/db-stop.log

 if [ $? -ne 0 ]
 then

   . $BASEDIR/db/cfg/dbadmin.cfg

   #echo "listener is already executing, will re-start............"

   $ORACLE_HOME/bin/lsnrctl <<EOF >> ${TMP_SMC_DB_LOGS}/db-stop.log
set current_listener $SUNMCLISTENER
set password ${SUNMC_USER_PWD}
stop $SUNMCLISTENER
EOF

   if [ $? -ne 0 ]
   then
     echolog 'execution of $2 stop $3 failed' "$ORACLE_HOME/bin/lsnrctl" "$SUNMCLISTENER"
     echolog
     echolog
     echolog "exiting.........................."

     #
     #  Append the contents of the sql log onto
     #  that of the current symon related log
     #

#     if [ -n $LOGFILE ] ; then
#       echo "=========================================================================" >> $LOGFILE
#       cat ${TMP_SMC_DB_LOGS}/db-stop.log >> $LOGFILE
#       echo "=========================================================================" >> $LOGFILE
#     fi

     exit 1

   fi


 fi



#
#  If the sql log exist check for errors
#

if [ -f  ${TMP_SMC_DB_LOGS}/db-stop-sql.log ]
then

   errcnt=$(grep -i "ORA-[0-9]" ${TMP_SMC_DB_LOGS}/db-stop-sql.log | wc -l)

   if [ $errcnt != 0 ]
   then
     echolog 'execution of svrmgrl using $2 fail\n' "@$BASEDIR/sbin/db-stop.sql"
     echolog
     echolog
     echolog "exiting........................\n"

     for what in $(grep -i "ORA-[0-9]" ${TMP_SMC_DB_LOGS}/db-stop-sql.log | sed 's, ,#,g')
     do
       errfull=$(echo $what | sed 's,#, ,g')
       errcode=$(echo $what | cut -f1 -d':' | cut -f2 -d'-')
       echolog 'sqlerror code = $2\n' "$errcode"
       echolog 'sqlerrmsg full = $2\n' "$errfull"
       echo
     done

     #
     #  Append the contents of the sql log onto
     #  that of the current symon related log
     #

#     if [ -n $LOGFILE ] ; then
#       echo "=========================================================================" >> $LOGFILE
#       cat ${TMP_SMC_DB_LOGS}/db-stop-sql.log >> $LOGFILE
#       echo "=========================================================================" >> $LOGFILE
#     fi

     exit 1

   fi


   errcnt=$(grep -i "TNS-[0-9]" ${TMP_SMC_DB_LOGS}/db-stop-sql.log | wc -l)

   if [ $errcnt != 0 ]
   then
     echolog 'execution of svrmgrl using $2 fail\n' "@$BASEDIR/sbin/db-stop.sql"
     echolog
     echolog
     echolog "exiting........................\n"

     for what in $(grep -i "TNS-[0-9]" ${TMP_SMC_DB_LOGS}/db-stop-sql.log | sed 's, ,#,g')
     do
       errfull=$(echo $what | sed 's,#, ,g')
       errcode=$(echo $what | cut -f1 -d':' | cut -f2 -d'-')
       echolog 'sqlerror code = $2\n' "$errcode"
       echolog 'sqlerrmsg full = $2\n' "$errfull"
       echo
     done

     #
     #  Append the contents of the sql log onto
     #  that of the current symon related log
     #

#     if [ -n $LOGFILE ] ; then
#       echo "=========================================================================" >> $LOGFILE
#       cat ${TMP_SMC_DB_LOGS}/db-stop-sql.log >> $LOGFILE
#       echo "=========================================================================" >> $LOGFILE
#     fi

     exit 1

   fi

fi
