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

#
#
#  Utility functions to validate startup
#
#
function getHostName
{
  currhost=$(hostname)

  retcode=$?

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

  echologverbose 'current host : $2\n' "$currhost"

  return 0
}

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 command\n"
     return $retcode
  fi

  echologverbose 'current user name : $2\n' "$username"

  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 command\n"
     return $retcode
  fi

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

  return 0
}




# params:  pathtemp, filename

function isFileInPath
{

echologverbose 'path_app : $2\n' "$path_app"

found_app="false"


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

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

if [ $found_app != "true" ]
then
  echolog 'can not find $2 on path, exiting.......................\n' "$2"
  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 execution\n' "$1"
     return 0
  else
     echologverbose 'verifyListenerUp: listener $2 is not execution\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 ps\n"
     return $retcode
  fi

  if [ $proccnt -gt 0 ]
  then
     echologverbose 'verifyListenerDown: listener $2 is execution\n' "$1"
     return 1
  else
     echologverbose 'verifyListenerDown: listener $2 is not execution\n' "$1"
     return 0
  fi

  return 0

}




# params:  dirname

function verifySetuidFilesystemPerm
{
  fsysname=$(/usr/sbin/df -k $1 | /usr/bin/sed -e '1d' | /usr/bin/awk '{print $6}')

  retcode=$?

  if [ $retcode != 0 ]
  then
     echolog "verifyFilesystemPerm: failed to obtain filesystem info from df\n"
     return $retcode
  fi



  fsysdev=$(/usr/sbin/df -k $1 | /usr/bin/sed -e '1d' | /usr/bin/awk '{print $1}')

  retcode=$?

  if [ $retcode != 0 ]
  then
     echolog "verifyFilesystemPerm: failed to obtain filesystem info from df\n"
     return $retcode
  fi

  fsysperm=$(/usr/sbin/mount | grep -i "$fsysname " | grep -i "$fsysdev " | cut -f4 -d" " | grep -i setuid | wc -l)

  retcode=$?

  if [ $retcode != 0 ]
  then
     echolog "verifyFilesystemPerm: failed to obtain filesystem info from mount comma\n"
     return $retcode
  fi

  if [ $fsysperm -ne 1 ]
  then
     echolog 'verifyFilesystemPerm: filesystem $2 is not mounted setuid\n' "$fsysname"
     echolog " must re-mount the filesystem with setuid enabled\n"
     return 1
  else
     echologverbose 'verifyFilesystemPerm: filesystem $2 is mounted setuid, OK\n' "$fsysname"
  fi

  return 0

}



# params: path, lsnalias

function verifyGoodListenerStatus
{

  srvcnt=$($1/lsnrctl status $2 | grep "service handler(s)" | wc -l)

  retcode=$?

  if [ $retcode != 0 ]
  then
     echolog 'verifyListenerStatus: failed to obtain listener status for alias: $2\n' "$2"
     return $retcode
  fi

  if [ $srvcnt -gt 0 ]
  then
     echologverbose 'verifyListenerStatus: listener status is good for alias: $2\n' "$2"
     return 0
  else
     echologverbose 'verifyListenerStatus: listener status is not good for alias: $2\n' "$2"
     return 1
  fi

  return 0

}





# params:  pathname, username, groupname


function verifyGoodOraclePermOwnership
{


if [ ! -d $1 ]
then
  echolog 'the specified path \\\($2\\\) does not exist\n' "$1"
  return 1
fi


tmpdir=$1

echologverbose "tmpdir \n" $tmpdir

startchar=$(echo $tmpdir | cut -c1)

if [ $startchar = "/" ]
then
  new="/"
else
  new=""
fi

#sed 's,\/, ,g' $tmpdir

#echo "tmpdir : " $tmpdir


for what in $(echo $tmpdir | sed 's,\/, ,g')
do

#  echo "loop: current what : " $what

  new=$new$what

  echologverbose "loop: current new \n" $new

  if [ -d $new ]
  then

     accessperms=$(ls -ld $new | awk '{print $1}')

     retcode=$?

     if [ $retcode != 0 ]
     then
        echolog 'verifyGoodOraclePermOwnership: failed to obtain accessperms from ls command for : $2\n' "$new"
        return $retcode
     fi


     listuser=$(ls -ld $new | awk '{print $3}')

     retcode=$?

     if [ $retcode != 0 ]
     then
        echolog 'verifyGoodOraclePermOwnership: failed to obtain username from ls command for : $2\n' "$new"
        return $retcode
     fi

     listgroup=$(ls -ld $new | awk '{print $4}')

     retcode=$?

     if [ $retcode != 0 ]
     then
        echolog 'verifyGoodOraclePermOwnership: failed to obtain groupname from ls command for : $2\n' "$new"
        return $retcode
     fi


#     /usr/bin/gettext "perms = $accessperms, listuser = $listuser, listgroup = $listgroup\n"

     #
     # verify at least 755 perms on each subdir
     # which is:  -rwxr-xr-x
     #

     for item in $(echo "2 3 4 5 7 8 10")
     do

        tmp=$(echo $accessperms | cut -c$item)

#        /usr/bin/gettext "tmp \n" $tmp

        if [ $tmp = "-" ]
        then
          echolog 'verifyGoodOraclePermOwnership: access perms for \\\($2\\\) is not at least 755\n' "$new"
          return 1
        fi

     done

     echologverbose "verifyGoodOraclePermOwnership: access perms ok\n"


     if [ $listuser != $2 ]
     then
        echolog 'verifyGoodOraclePermOwnership: owner \\\($2\\\) of $3 is not $4\n' "$listuser" "$new" "$2"
        return 1
     fi

     echologverbose "verifyGoodOraclePermOwnership: owner ok\n"


     if [ $listgroup != $3 ]
     then
        echolog 'verifyGoodOraclePermOwnership: group \\\($2\\\) of $3 is not $4\n' "$listgroup" "$new" "$3"
        return 1
     fi

     echologverbose "verifyGoodOraclePermOwnership: group ok\n"

     echo


  else

     echologverbose '**** directory : $2 does not exist\n' "$new"
     return 1
  fi

  # append directory seperator to end of string
  #

  new=$new"/"

done


#
#  Now check specific files for correct permissions and ownership
#  oracle,svrmgrl,sqlplus,lsnrctl,tnslsnr,etc
#

tmpdir=$new

for what in $(echo "svrmgrl sqlplus lsnrctl tnslsnr")
do

#  echo "loop: current what : " $what

  new=$tmpdir/$what

  echologverbose 'loop: current new $2\n' "$new"

  if [ -f $new ]
  then

     accessperms=$(ls -ld $new | awk '{print $1}')

     retcode=$?

     if [ $retcode != 0 ]
     then
        echolog 'verifyGoodOraclePermOwnership: failed to obtain accessperms from ls command for : $2\n' "$new"
        return $retcode
     fi


     listuser=$(ls -ld $new | awk '{print $3}')

     retcode=$?

     if [ $retcode != 0 ]
     then
        echolog 'verifyGoodOraclePermOwnership: failed to obtain username from ls command for : $2\n' "$new"
        return $retcode
     fi

     listgroup=$(ls -ld $new | awk '{print $4}')

     retcode=$?

     if [ $retcode != 0 ]
     then
        echolog 'verifyGoodOraclePermOwnership: failed to obtain groupname from ls command for : $2\n' "$new"
        return $retcode
     fi



     #
     # verify at least 755 perms on each file
     # which is:  -rwxr-xr-x
     #

     for item in $(echo "2 3 4 5 7 8 10")
     do

        tmp=$(echo $accessperms | cut -c$item)

#        /usr/bin/gettext "tmp \n" $tmp

        if [ $tmp = "-" ]
        then
          echolog 'verifyGoodOraclePermOwnership: access perms for \\\($2\\\) is not at least 755\n' "$new"
          return 1
        fi

     done

     echologverbose "verifyGoodOraclePermOwnership: access perms ok\n"


     if [ $listuser != $2 ]
     then
        echolog 'verifyGoodOraclePermOwnership: owner \\\($listuser\\\) of $3 is not $4\n' "$listuser" "$new" "$2"
        return 1
     fi

     echologverbose "verifyGoodOraclePermOwnership: owner ok\n"


     if [ $listgroup != $3 ]
     then
        echolog 'verifyGoodOraclePermOwnership: group \\\($2\\\) of $3 is not $4\n' "$listgroup" "$new" "$3"
        return 1
     fi

     echologverbose "verifyGoodOraclePermOwnership: group ok\n"

     echo


  else

     echolog '**** file : $2 does not exist\n' "$new"
     return 1
  fi


done



 return 0

}


# params:  usrname, grpname

function verifyNoSharedMemUsage
{

  tmp=$(ipcs -m | grep "$1" | grep "$2" | awk '{print $2}')

  retcode=$?

  if [ $retcode != 0 ]
  then
     echolog 'verifySharedMemUsage: failed to obtain ipcs \\\(shared memory segment\\\) info user : $2, group : $3\n' "$1" "$2"
     return $retcode
  fi

  found=0

  for what in $tmp
  do

    found=1
    echologverbose 'verifySharedMemUsage: found ipcs \\\(shared memory segment\\\) info user : $2, group : $3, \\\(shid\\\) : $4\n' "$1" "$2" "$what"

  done

  if [ found -eq 1 ]
  then
     echolog 'verifySharedMemUsage: did find ipcs \\\(shared memory segment\\\) info user : $2, group : $3\n' "$1" "$2"
     return 1
  else
     echologverbose 'verifySharedMemUsage: did not find any ipcs \\\(shared memory segment\\\) info user : $2, group : $3\n' "$1" "$2"
     return 0
  fi


}



# params:  usrname, grpname

function verifyNoActiveSemaphoresUsage
{

  tmp=$(ipcs -s | grep "$1" | grep "$2" | awk '{print $2}')

  retcode=$?

  if [ $retcode != 0 ]
  then
     echolog 'verifyActiveSemaphoresUsage: failed to obtain ipcs \\\(active semaphores\\\) info user : $2, group : $3\n' "$1" "$2"
     return $retcode
  fi

  found=0

  for what in $tmp
  do

    found=1
    echologverbose 'verifyActiveSemaphoresUsage: found ipcs \\\(active semaphores\\\) info user : $2, group : $3, \\\(semid\\\) : $4\n' "$1" "$2" "$wh"

  done

  if [ found -eq 1 ]
  then
     echolog 'verifyActiveSemaphoresUsage: did find ipcs \\\(active semaphores\\\) info user : $2, group : \n' "$1"
     return 1
  else
     echologverbose 'verifyActiveSemaphoresUsage: did not find any ipcs \\\(active semaphores\\\) info user : $2, group : $3\n' "$1" "$2"
     return 0
  fi


}

# params: lsnalias

function startListener
{
  lsnrctl start $1

  retcode=$?

  if [ $retcode != 0 ]
  then
     echolog 'startListener: failed to start listener, alias: $2\n' "$1"
     return $retcode
  fi

  echologverbose 'startListener: listener started, alias: $2\n' "$1"

  return 0

}


# params: lsnalias

function stopListener
{

  . $BASEDIR/db/cfg/dbadmin.cfg

  lsnrctl << EOF
set current_listener $1
set password ${SUNMC_USER_PWD}
stop $1
EOF

  retcode=$?

  if [ $retcode != 0 ]
  then
     echolog 'stopListener: failed to stop listener, alias: $2\n' "$1"
     return $retcode
  fi

  echologverbose 'stopListener: listener stopped, alias: $2\n' "$1"

  return 0

}

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

BASEDIR=`/usr/bin/pkgparam SUNWescom BASEDIR`
SCRIPT_DIR=${BASEDIR}/SUNWsymon/sbin
. ${SCRIPT_DIR}/es-common.sh



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

set_basedir
set_db_env

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

system_OS=$(uname -a)

pathdelimiter=":"
sqlplus_app="sqlplus"
path_app=$(echo $PATH | sed 's/:/ /g')



/usr/bin/umask 022


if [ -f ${TMP_SMC_DB_LOGS}/db-start.log ]
then
 /usr/bin/rm -rf ${TMP_SMC_DB_LOGS}/db-start.log
fi

if [ -f ${TMP_SMC_DB_LOGS}/db-start-sql.log ]
then
 /usr/bin/rm -rf ${TMP_SMC_DB_LOGS}/db-start-sql.log
fi




getHostName >> ${TMP_SMC_DB_LOGS}/db-start.log
getUserName >> ${TMP_SMC_DB_LOGS}/db-start.log
getUserGroup >> ${TMP_SMC_DB_LOGS}/db-start.log


#
#  Do some environment checking here
#

verifySetuidFilesystemPerm $ORACLE_HOME >> ${TMP_SMC_DB_LOGS}/db-start.log

if [ $? -ne 0 ]
 then
   echolog 'execution of verifySetuidFilesystemPerm $2 fail\n' "$ORACLE_HOME"
   echo
   echo
   echolog "exiting........................\n"

   #
   #  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-start.log >> $LOGFILE
#     echo "=========================================================================" >> $LOGFILE
#   fi



   exit 1
 fi

/usr/bin/gettext "the current sid \n" $ORACLE_SID >> ${TMP_SMC_DB_LOGS}/db-start-sh.log

/usr/bin/gettext "the current orahome \n" $ORACLE_HOME >> ${TMP_SMC_DB_LOGS}/db-start-sh.log

 #
 # If listener is down start it up
 #
 #


#echo "logfile = ]$LOGFILE["


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

 if [ $? -eq 0 ]
 then

   $ORACLE_HOME/bin/lsnrctl start $SUNMCLISTENER >> ${TMP_SMC_DB_LOGS}/db-start.log 2>&1

   if [ $? -ne 0 ]
   then
     echolog 'execution of $2 start $3 fail\n' "$ORACLE_HOME/bin/lsnrctl" "$SUNMCLISTENER"
     echo
     echo
     echolog "exiting........................\n"

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

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

     exit 1

   fi

   verifyListenerUp $SUNMCLISTENER >> ${TMP_SMC_DB_LOGS}/db-start.log

   if [ $? -ne 0 ]
   then
     echolog 'execution of verifyListenerUp $2 fail\n' "$SUNMCLISTENER"
     echo
     echo
     echolog "exiting........................\n"

     #
     #  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-start.log >> $LOGFILE
#       echo "=========================================================================" >> $LOGFILE
#     fi

     exit 1
   fi

 fi


 #
 # Ensure listener status is good
 #


 verifyGoodListenerStatus "$ORACLE_HOME/bin" $SUNMCLISTENER >> ${TMP_SMC_DB_LOGS}/db-start.log

 if [ $? -ne 0 ]
 then
   echolog 'execution of verifyGoodListenerStatus $2 fail\n' "$SUNMCLISTENER"
   echo
   echo
   echolog "exiting........................\n"

   #
   #  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-start.log >> $LOGFILE
#     echo "=========================================================================" >> $LOGFILE
#   fi

   exit 1
 fi



 #
 # if Database if down start it up
 #


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

 if [ $? -eq 0 ]
 then


   #
   #  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-start.log >> $LOGFILE
#     echo "=========================================================================" >> $LOGFILE
#   fi



   #
   # Start the database
   #


if [ -f ${TMP_SMC_DB_LOGS}/db-start.sql ]
then
 /usr/bin/rm -rf ${TMP_SMC_DB_LOGS}/db-start.sql
fi


if [ -f ${TMP_SMC_DB_LOGS}/db-start-sql.log ]
then
 /usr/bin/rm -rf ${TMP_SMC_DB_LOGS}/db-start-sql.log
fi



echo "set stoponerror on " >> ${TMP_SMC_DB_LOGS}/db-start.sql
echo "set echo off " >> ${TMP_SMC_DB_LOGS}/db-start.sql
echo "set termout off " >> ${TMP_SMC_DB_LOGS}/db-start.sql
echo "spool ${TMP_SMC_DB_LOGS}/db-start-sql.log " >> ${TMP_SMC_DB_LOGS}/db-start.sql
echo "connect sys/oracle as sysdba " >> ${TMP_SMC_DB_LOGS}/db-start.sql
echo "startup pfile=$ORACLE_HOME/dbs/init$ORACLE_SID.ora " >> ${TMP_SMC_DB_LOGS}/db-start.sql


$ORACLE_HOME/bin/svrmgrl command=@${TMP_SMC_DB_LOGS}/db-start.sql >> ${TMP_SMC_DB_LOGS}/db-start.log


   retcode=$?

   if [ $retcode -ne 0 ]
   then
      echolog 'execution of svrmgrl using ${TMP_SMC_DB_LOGS}/db-start.sql failed\n'

      #
      #  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-start.log >> $LOGFILE
#        echo "=========================================================================" >> $LOGFILE
#      fi



     	exit 1
   fi


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

   if [ $errcnt != 0 ]
   then
     echolog 'execution of svrmgrl using $2 fail\n' "${TMP_SMC_DB_LOGS}/db-start.sql"
     echolog
     echolog
     echolog "exiting........................\n"

     for what in $(grep -i "ORA-[0-9]" ${TMP_SMC_DB_LOGS}/db-start-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
       if [ $errcode -eq 27146 ]; then
	   multiline_echolog 'This error message indicates that /etc/system settings\nmight be insufficient for shmsys:shminfo_shmmni, semsys:seminfo_semmni\nsemsys:seminfo_semmsl or semsys:seminfo_semmns or others.'
       fi
     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-start-sql.log >> $LOGFILE
#       echo "=========================================================================" >> $LOGFILE
#     fi

     exit 1

   fi



  fi
#
#
# Ensure database status is good
#

 verifyDatabaseUp $ORACLE_SID >> ${TMP_SMC_DB_LOGS}/db-start.log

 if [ $? -ne 0 ]
 then
   echolog 'execution of verifyDatabaseUp $2 fail\n' "$ORACLE_SID"
   echo
   echo
   echolog "exiting........................\n"

   #
   #  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-start.log >> $LOGFILE
#     echo "=========================================================================" >> $LOGFILE
#   fi

   exit 1
 fi




if [ -f ${TMP_SMC_DB_LOGS}/db-start-vinst.sql ]
then
 /usr/bin/rm -rf ${TMP_SMC_DB_LOGS}/db-start-vinst.sql
fi


if [ -f ${TMP_SMC_DB_LOGS}/db-start-vinst.log ]
then
 /usr/bin/rm -rf ${TMP_SMC_DB_LOGS}/db-start-vinst.log
fi



echo " " >> ${TMP_SMC_DB_LOGS}/db-start-vinst.sql
echo "set termout off " >> ${TMP_SMC_DB_LOGS}/db-start-vinst.sql
echo "set stoponerror on " >> ${TMP_SMC_DB_LOGS}/db-start-vinst.sql
echo "set serveroutput on " >> ${TMP_SMC_DB_LOGS}/db-start-vinst.sql
echo "set echo off " >> ${TMP_SMC_DB_LOGS}/db-start-vinst.sql
echo "spool ${TMP_SMC_DB_LOGS}/db-start-vinst.log " >> ${TMP_SMC_DB_LOGS}/db-start-vinst.sql
echo "connect sys/oracle as sysdba " >> ${TMP_SMC_DB_LOGS}/db-start-vinst.sql
echo " " >> ${TMP_SMC_DB_LOGS}/db-start-vinst.sql
echo " " >> ${TMP_SMC_DB_LOGS}/db-start-vinst.sql
echo "declare " >> ${TMP_SMC_DB_LOGS}/db-start-vinst.sql
echo "    statusvar varchar2(7); " >> ${TMP_SMC_DB_LOGS}/db-start-vinst.sql
echo " " >> ${TMP_SMC_DB_LOGS}/db-start-vinst.sql
echo "begin " >> ${TMP_SMC_DB_LOGS}/db-start-vinst.sql
echo " " >> ${TMP_SMC_DB_LOGS}/db-start-vinst.sql
echo "   select status into statusvar " >> ${TMP_SMC_DB_LOGS}/db-start-vinst.sql
echo "   from v\$instance; " >> ${TMP_SMC_DB_LOGS}/db-start-vinst.sql
echo " " >> ${TMP_SMC_DB_LOGS}/db-start-vinst.sql
echo "     dbms_output.put_line('The database instance status(' || statusvar || ') '); " >> ${TMP_SMC_DB_LOGS}/db-start-vinst.sql
echo "     dbms_output.put_line(chr(10)); " >> ${TMP_SMC_DB_LOGS}/db-start-vinst.sql
echo " " >> ${TMP_SMC_DB_LOGS}/db-start-vinst.sql
echo " " >> ${TMP_SMC_DB_LOGS}/db-start-vinst.sql
echo "  if statusvar is not null then " >> ${TMP_SMC_DB_LOGS}/db-start-vinst.sql
echo " " >> ${TMP_SMC_DB_LOGS}/db-start-vinst.sql
echo "     dbms_output.put_line('The database instance status(' || statusvar || ') '); " >> ${TMP_SMC_DB_LOGS}/db-start-vinst.sql
echo "     dbms_output.put_line(chr(10)); " >> ${TMP_SMC_DB_LOGS}/db-start-vinst.sql
echo " " >> ${TMP_SMC_DB_LOGS}/db-start-vinst.sql
echo "     if upper(statusvar) != upper('open') then " >> ${TMP_SMC_DB_LOGS}/db-start-vinst.sql
echo " " >> ${TMP_SMC_DB_LOGS}/db-start-vinst.sql
echo "       dbms_output.put_line('The database instance status(' || statusvar || ') is not OPEN'); " >> ${TMP_SMC_DB_LOGS}/db-start-vinst.sql
echo "       dbms_output.put_line(chr(10)); " >> ${TMP_SMC_DB_LOGS}/db-start-vinst.sql
echo "       raise_application_error(-20300,'The database instance status(' || statusvar || ') is not OPEN',TRUE); " >> ${TMP_SMC_DB_LOGS}/db-start-vinst.sql
echo " " >> ${TMP_SMC_DB_LOGS}/db-start-vinst.sql
echo "     end if; " >> ${TMP_SMC_DB_LOGS}/db-start-vinst.sql
echo " " >> ${TMP_SMC_DB_LOGS}/db-start-vinst.sql
echo " " >> ${TMP_SMC_DB_LOGS}/db-start-vinst.sql
echo " " >> ${TMP_SMC_DB_LOGS}/db-start-vinst.sql
echo "  else " >> ${TMP_SMC_DB_LOGS}/db-start-vinst.sql
echo " " >> ${TMP_SMC_DB_LOGS}/db-start-vinst.sql
echo "     dbms_output.put_line('The statusvar variable is null'); " >> ${TMP_SMC_DB_LOGS}/db-start-vinst.sql
echo "     dbms_output.put_line(chr(10)); " >> ${TMP_SMC_DB_LOGS}/db-start-vinst.sql
echo "     raise_application_error(-20301,'The statusvar variable is null',TRUE); " >> ${TMP_SMC_DB_LOGS}/db-start-vinst.sql
echo " " >> ${TMP_SMC_DB_LOGS}/db-start-vinst.sql
echo "  end if; " >> ${TMP_SMC_DB_LOGS}/db-start-vinst.sql
echo " " >> ${TMP_SMC_DB_LOGS}/db-start-vinst.sql
echo " " >> ${TMP_SMC_DB_LOGS}/db-start-vinst.sql
echo " " >> ${TMP_SMC_DB_LOGS}/db-start-vinst.sql
echo "exception when others then    " >> ${TMP_SMC_DB_LOGS}/db-start-vinst.sql
echo " " >> ${TMP_SMC_DB_LOGS}/db-start-vinst.sql
echo "  raise; " >> ${TMP_SMC_DB_LOGS}/db-start-vinst.sql
echo " " >> ${TMP_SMC_DB_LOGS}/db-start-vinst.sql
echo "end; " >> ${TMP_SMC_DB_LOGS}/db-start-vinst.sql
echo " " >> ${TMP_SMC_DB_LOGS}/db-start-vinst.sql
echo "/ " >> ${TMP_SMC_DB_LOGS}/db-start-vinst.sql
echo " " >> ${TMP_SMC_DB_LOGS}/db-start-vinst.sql
echo "disconnect " >> ${TMP_SMC_DB_LOGS}/db-start-vinst.sql
echo " " >> ${TMP_SMC_DB_LOGS}/db-start-vinst.sql
echo "quit " >> ${TMP_SMC_DB_LOGS}/db-start-vinst.sql
echo " " >> ${TMP_SMC_DB_LOGS}/db-start-vinst.sql
echo " " >> ${TMP_SMC_DB_LOGS}/db-start-vinst.sql


$ORACLE_HOME/bin/svrmgrl command=@${TMP_SMC_DB_LOGS}/db-start-vinst.sql >> ${TMP_SMC_DB_LOGS}/db-start.log

retcode=$?

if [ $retcode -ne 0 ]
then
   echolog 'execution of svrmgrl using ${TMP_SMC_DB_LOGS}/db-start-vinst.sql failed\n'

   #
   #  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-start.log >> $LOGFILE
#     echo "=========================================================================" >> $LOGFILE
#   fi

   exit 1
fi



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

if [ $errcnt != 0 ]
then
  echolog 'execution of svrmgrl using ${TMP_SMC_DB_LOGS}/db-start-vinst.sql failed'
  echolog
  echolog
  echolog "exiting........................\n"

  for what in $(grep -i "ORA-[0-9]" ${TMP_SMC_DB_LOGS}/db-start-vinst.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"
  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-start-vinst.log >> $LOGFILE
#    echo "=========================================================================" >> $LOGFILE
#  fi

  exit 1

fi










echologverbose "Listener and Database are up and running\n"


return 0




