#! /bin/sh
# Copyright (c)1995 SuSE GmbH Nuernberg, Germany.
# 
# Author: SuSE Oracle Team <feedback@suse.de>
# Homepage: http://www.suse.com/oracle/
# 
### BEGIN INIT INFO
# Provides: oracle
# Required-Start: $network $syslog $remote_fs raw
# Should-Start: ocfs2 sshd
# Should-Stop: ocfs2
# Required-Stop: $network $syslog $remote_fs
# Default-Start: 3 5
# Default-Stop: 0 1 2 6
# Description: Start the Oracle database
### END INIT INFO

 
# Shell functions sourced from /etc/rc.status:
#      rc_check         check and set local and overall rc status
#      rc_status        check and set local and overall rc status
#      rc_status -v     ditto but be verbose in local rc status
#      rc_status -v -r  ditto and clear the local rc status
#      rc_failed        set local and overall rc status to failed
#      rc_reset         clear local rc status (overall remains)
#      rc_exit          exit appropriate to overall rc status
. /etc/rc.status


# catch mis-use right here at the start
if [  "$1" != "start"  -a  "$1" != "stop"  -a  "$1" != "status" -a "$1" != "restart" ]; then
    echo "Usage: $0 {start|stop|status|restart}"
    exit 1
fi


ORACLE_RAC="no"
USE_SYSCTL="no"
LOAD_O2CB=/etc/init.d/o2cb
LOAD_OCFS2=/etc/init.d/ocfs2
MOUNT=/bin/mount
UMOUNT=/bin/umount
MKDIR=/bin/mkdir
RMMOD=/sbin/rmmod

CHECKPROC="/sbin/checkproc"
test -x "$CHECKPROC" || CHECKPROC="test -x "


# Get settings, if file(s) exist(s). If not, we simply use defaults.
if test -f /etc/sysconfig/oracle; then
    # new location as of SL 8.0 is directory /etc/sysconfig/
    . /etc/sysconfig/oracle
else
  if test -f /etc/rc.config.d/oracle.rc.config; then
    # location is directory /etc/rc.config.d/
    . /etc/rc.config.d/oracle.rc.config
  else
    if test -f /etc/rc.config; then
    # old SuSE location was to have everything in one file
    . /etc/rc.config
    fi
  fi
fi


# Determine the base and follow a runlevel link name.
# DISABLED by default because it's very individual...
#base=${0##*/}
#link=${base#*[SK][0-9][0-9]}
# Force execution if not called by a runlevel directory.
#test $link = $base && START_ORACLE_DB="yes" && START_ORACLE_DB_LISTENER="yes" && SET_ORACLE_KERNEL_PARAMETERS="yes"

 
# First reset status of this service
rc_reset


# 
# Get and check environment (e.g. ORACLE_HOME)
# 
ora_environment()
{
  test -f /etc/profile.d/oracle.sh && . /etc/profile.d/oracle.sh
 # Check, If Oracle SW is installed, before spitting too many warnings
 if [ ! -f /etc/oratab -a ! -d "$ORACLE_HOME" ]; then
    echo "${warn}Oracle Database Software not yet installed!$norm"
    echo
 else
  if [ ! -z "$ORACLE_HOME" -a ! -d "$ORACLE_HOME" ]; then
    echo "${warn}ORACLE_HOME directory $ORACLE_HOME does not exist!$norm"
    echo "Unsetting ORACLE_HOME, will try to determine it from system..."
    unset ORACLE_HOME
  fi

  # Try /etc/oratab if it's not set in /etc/profile.d/oracle.sh
  test -z "$ORACLE_HOME" && test -f /etc/oratab &&                        \
    ORACLE_HOME=`awk -F: '/^[^#].*:.+:[YN]/ {if ($2!="") print $2; exit}' </etc/oratab` &&   \
    echo && echo "ORACLE_HOME not set, but I found this in /etc/oratab: $ORACLE_HOME" && echo

  if [ -z "$ORACLE_HOME" ]; then
    echo "${warn}ORACLE_HOME environment variable not set.$norm"
    echo "Check /etc/profile.d/oracle.sh and /etc/oratab"
  fi

  if [ ! -d "$ORACLE_HOME" ]; then
    echo "${warn}Cannot find ORACLE_HOME directory $ORACLE_HOME.$norm"
    echo "Environment settings are wrong! Check /etc/profile.d/oracle.sh"
  fi

  test -z "$ORACLE_OWNER" && ORACLE_OWNER="oracle"

  echo

  # Set Intelligent Agent Start/Stop 
  AGENT_VERSION="unknown"
  if [ ! -z "$ORACLE_HOME" -a ! -d "$ORACLE_HOME" ]; then
  if test -x "$AGENT_HOME/bin/emctl"; then
    # If Oracle Grid Control Agent is installed 
    AGENT_VERSION="10gA"
    # bz#308851 - 10gR3 Agent uses emagent
    if test -x "$AGENT_HOME/bin/emagent"; then
    	AGENT_PROG="$AGENT_HOME/bin/emagent"
    else
    	AGENT_PROG="$AGENT_HOME/bin/emctl"
    fi
    AGENT_START="$AGENT_HOME/bin/emctl start agent"
    AGENT_STOP="$AGENT_HOME/bin/emctl stop agent"
  elif test -x "$ORACLE_HOME/bin/agtctl"; then 
    # 10g
    AGENT_VERSION="10g"
    AGENT_HOME=$ORACLE_HOME
    AGENT_PROG="$ORACLE_HOME/bin/agtctl"                                            AGENT_START="$ORACLE_HOME/bin/agtctl startup"
    AGENT_STOP="$ORACLE_HOME/bin/agtctl stop"
  elif test -x "$ORACLE_HOME/bin/agentctl"; then
    # Oracle 9i
    AGENT_VERSION="9i"
    AGENT_HOME=$ORACLE_HOME
    AGENT_PROG="$ORACLE_HOME/bin/agentctl"
    AGENT_START="$ORACLE_HOME/bin/agentctl start"
    AGENT_STOP="$ORACLE_HOME/bin/agentctl stop"
  else
    # Oracle 8i
    AGENT_VERSION="8i"
    AGENT_HOME=$ORACLE_HOME
    AGENT_PROG="$ORACLE_HOME/bin/lsnrctl"
    AGENT_START="$ORACLE_HOME/bin/lsnrctl dbsnmp_start"
    AGENT_STOP="$ORACLE_HOME/bin/lsnrctl dbsnmp_stop"
    if [ `$ORACLE_HOME/bin/lsnrctl help | grep -q dbsnmp_start; echo $?` = "0"  -a  ${START_ORACLE_DB_AGENT:-no} = "yes" ]; then
     echo "${warn}Cannot start agent - Setting START_ORACLE_DB_AGENT = no $norm"
     START_ORACLE_DB_AGENT="cannot";
    fi
  fi
  fi
 fi

#  If Oracle Grid Control Agent is installed 
#  if test -x "$AGENT_HOME/bin/emctl"; then
#    AGENT_VERSION="10g"
#    AGENT_HOME=$AGENT_HOME
#    AGENT_PROG="$AGENT_HOME/bin/emctl"
#    AGENT_START="$AGENT_HOME/bin/emctl start agent"
#    AGENT_STOP="$AGENT_HOME/bin/emctl stop agent"
#  fi

# If Oracle Clusteware is installed, Set ORACLE_RAC Variable to "yes"
# ORA_CRS_HOME is defined in /etc/profile.d/oracle[c].sh

  if (test -x "$ORA_CRS_HOME/bin/crsctl" ); then
	ORACLE_RAC="yes"
  fi

#   If /etc/init.d/init.crs is configured (chkconfig) to start then
#   Don't starting CRS again
    if ( (chkconfig -c init.crs on) ); then
      START_ORACLE_RAC_CRS="cannot";
    fi

#   If OCFS2 is configured (chkconfig) to start then Don't start again
    if ( (chkconfig -c ocfs2 on) ); then
      START_ORACLE_RAC_OCFS2="cannot";
    fi

  test -d "${TNS_ADMIN}" || TNS_ADMIN="$ORACLE_HOME/network/admin"

  if [ "$1" = "start" ]; then
    echo -n " ${extd}SETTINGS $1 from /etc/sysconfig/oracle$norm"
    if [ ! -f /etc/sysconfig/oracle ]; then
      echo " - ${warn}!!! MISSING !!!$norm"
    else
      echo
    fi
    echo " - Set Kernel Parameters for Oracle:   ${SET_ORACLE_KERNEL_PARAMETERS:-no}"
   if [ "$ORACLE_RAC" = "yes" ]; then
    echo " - Start Oracle OCFS2:                 ${START_ORACLE_RAC_OCFS2:-no}"
    echo " - Start Oracle CRS:                   ${START_ORACLE_RAC_CRS:-no}"
    echo " - Start Oracle Enterprise Manager:    ${START_ORACLE_DB_EMANAGER:-no}"
   else
    echo " - Start Oracle Listener:              ${START_ORACLE_DB_LISTENER:-no}"
    echo " - Start Oracle Database:              ${START_ORACLE_DB:-no}"
    echo " - Start Oracle Webserver:             ${START_ORACLE_DB_APACHE:-no}"
    echo " - Start Oracle EManager:              ${START_ORACLE_DB_EMANAGER:-no}"
    echo " - Start Oracle iSQLPLUS Manager:      ${START_ORACLE_DB_ISQLPLUS:-no}"
    echo " - Start Oracle Agent:                 ${START_ORACLE_DB_AGENT:-no}"
    echo " - Start Oracle Internet Filesystem:   ${START_ORACLE_DB_IFS:-no}"
    echo " - Start Oracle Directory Manager:     ${START_ORACLE_DB_OID:-no}"
   fi
  fi
}


# Here we finally get to do the real work.
case "$1" in
  start)
    echo
    echo "#############################################################################"
    echo "#                 Begin of   O R A C L E   startup section                  #"
    echo "#############################################################################"
    echo
    ora_environment start

    # 
    # Check if we really have all the Oracle components we are told to start
    # 
  if [ "$ORACLE_RAC" = "yes" ]; then
    if [ ! -x $LOAD_OCFS2 -a ${START_ORACLE_RAC_OCFS2:-no} = "yes" ]; then
      echo "${warn}Can't find needed file: $LOAD_OCFS2 - Setting START_ORACLE_RAC_OCFS2 = no $norm"
      START_ORACLE_RAC_OCFS2="cannot";
    fi

    if [ ! -x /etc/init.d/init.crs -a ${START_ORACLE_RAC_CRS:-no} = "yes" ]; then
      echo "${warn}Can't find needed file: init.crs - Setting START_ORACLE_RAC_CRS = no $norm"
      START_ORACLE_RAC_CRS="cannot";
    fi
  else
    if [ ! -x $ORACLE_HOME/bin/dbstart -a ${START_ORACLE_DB:-no} = "yes" ]; then
      echo "${warn}Can't find needed file: dbstart - Setting START_ORACLE_DB = no $norm"
      START_ORACLE_DB="cannot";
    fi

    if [ ! -x $ORACLE_HOME/bin/lsnrctl -a ${START_ORACLE_DB_LISTENER:-no} = "yes" ]; then
      echo "${warn}Can't find needed file: lsnrctl - Setting START_ORACLE_DB_LISTENER = no $norm"
      START_ORACLE_DB_LISTENER="cannot";
    fi
     
    if [ ! -x $ORACLE_HOME/Apache/Apache/bin/apachectl -a ${START_ORACLE_DB_APACHE:-no} = "yes" ]; then
      echo "${warn}Can't find needed file: apachectl - Setting START_ORACLE_DB_APACHE = no $norm"
      START_ORACLE_DB_APACHE="cannot";
    fi
  fi    
    if [ ! -x $ORACLE_HOME/bin/emctl -a ${START_ORACLE_DB_EMANAGER:-no} = "yes" ]; then
      echo "${warn}Can't find needed file: emctl - Setting START_ORACLE_DB_EMANAGER = no $norm"
      START_ORACLE_DB_EMANAGER="cannot";
    fi
 
    if [ ! -x $ORACLE_HOME/bin/isqlplusctl -a ${START_ORACLE_DB_ISQLPLUS:-no} = "yes" ]; then
      echo "${warn}Can't find needed file: isqlplusctl - Setting START_ORACLE_DB_ISQLPLUS = no $norm"
      START_ORACLE_DB_ISQLPLUS="cannot";
    fi

    case $AGENT_VERSION in
    10g)
        if [ ! -x $ORACLE_HOME/bin/agtctl -a ${START_ORACLE_DB_AGENT:-no} = "yes" ]; then
          echo "${warn}Can't find needed file: agtctl - Setting START_ORACLE_DB_AGENT = no $norm"
          START_ORACLE_DB_AGENT="cannot"
	fi
    ;;
    10gA)
        if [ ! -x $AGENT_HOME/bin/emctl -a ${START_ORACLE_DB_AGENT:-no} = "yes" ]; then
          echo "${warn}Can't find needed file: emctl - Setting START_ORACLE_DB_AGENT = no $norm"
          START_ORACLE_DB_AGENT="cannot"
        fi
    ;;
    9i)
        if [ ! -x $ORACLE_HOME/bin/agentctl -a ${START_ORACLE_DB_AGENT:-no} = "yes" ]; then
          echo "${warn}Can't find needed file: agentctl - Setting START_ORACLE_DB_AGENT = no $norm"
          START_ORACLE_DB_AGENT="cannot"
        fi
    ;;
    8i)
        if [ ! -x $ORACLE_HOME/bin/lsnrctl -a ${START_ORACLE_DB_AGENT:-no} = "yes" ]; then
          echo "${warn}Can't find needed file: lsnrctl - Setting START_ORACLE_DB_AGENT = no $norm"
          START_ORACLE_DB_AGENT="cannot"
        fi
    ;;
    *)
        echo "Oracle Agent undefinied or not properly set. Please check."
    ;;
    esac

    echo

    # Set kernel parameters for Oracle
    if [ "${SET_ORACLE_KERNEL_PARAMETERS:-no}" == "yes" ]; then
      echo
      echo "Setting kernel parameters for Oracle, see file"
      if test -f /etc/sysconfig/oracle; then
        echo "/etc/sysconfig/oracle for explanations."
      else
        echo "/etc/rc.config.d/oracle.rc.config for explanations."
      fi
      echo
    
      if  [ ! -d /proc/sys/kernel ]; then
          echo; echo "No sysctl kernel interface - cannot set kernel parameters."; echo
          rc_failed
      else
        # Set shared memory parameters
        echo -n "${extd}Shared memory:$norm    "
        test -f /proc/sys/kernel/shmmax && echo -n "  SHMMAX=${SHMMAX:-3294967296}"
        test -f /proc/sys/kernel/shmmax && echo ${SHMMAX:-3294967296} > /proc/sys/kernel/shmmax
        test -f /proc/sys/kernel/shmmni && echo -n "  SHMMNI=${SHMMNI:-4096}"
        test -f /proc/sys/kernel/shmmni && echo ${SHMMNI:-4096}       > /proc/sys/kernel/shmmni
        test -f /proc/sys/kernel/shmall && echo    "  SHMALL=${SHMALL:-2097152}"
        test -f /proc/sys/kernel/shmall && echo ${SHMALL:-2097152}    > /proc/sys/kernel/shmall
        test -f /proc/sys/kernel/shmall || echo
    
        # Set the semaphore parameters:
        # see Oracle release notes for Linux for how to set these values
        # SEMMSL, SEMMNS, SEMOPM, SEMMNI
        echo -n "${extd}Semaphore values:$norm "
        test -f /proc/sys/kernel/sem && echo -n "  SEMMSL=${SEMMSL:-1250}"
        test -f /proc/sys/kernel/sem && echo -n "  SEMMNS=${SEMMNS:-32000}"
        test -f /proc/sys/kernel/sem && echo -n "  SEMOPM=${SEMOPM:-100}"
        test -f /proc/sys/kernel/sem && echo    "  SEMMNI=${SEMMNI:-256}"
        test -f /proc/sys/kernel/sem && echo ${SEMMSL:-1250} ${SEMMNS:-32000} ${SEMOPM:-100} ${SEMMNI:-128} > /proc/sys/kernel/sem
        test -f /proc/sys/kernel/sem || echo

        echo -n "${extd}Other values:$norm     "
        test -f /proc/sys/fs/file-max && echo -n "  FILE_MAX_KERNEL=${FILE_MAX_KERNEL:-6815744}"  
        test -f /proc/sys/fs/file-max && echo ${FILE_MAX_KERNEL:-6815744} > /proc/sys/fs/file-max  
        test -f /proc/sys/net/ipv4/ip_local_port_range && echo "  IP_LOCAL_PORT_RANGE=${IP_LOCAL_PORT_RANGE:-"9000 65500"}"  
        test -f /proc/sys/net/ipv4/ip_local_port_range && echo ${IP_LOCAL_PORT_RANGE:-"9000 65500"} > /proc/sys/net/ipv4/ip_local_port_range
        test -f /proc/sys/net/core/rmem_default && echo -n "  RMEM_DEFAULT=${RMEM_DEFAULT:-262144}"
        test -f /proc/sys/net/core/rmem_default && echo ${RMEM_DEFAULT:-262144} > /proc/sys/net/core/rmem_default
        test -f /proc/sys/net/core/wmem_default && echo -n "  WMEM_DEFAULT=${WMEM_DEFAULT:-262144}"
        test -f /proc/sys/net/core/wmem_default && echo ${WMEM_DEFAULT:-262144} > /proc/sys/net/core/wmem_default
        test -f /proc/sys/net/core/rmem_max && echo -n "  RMEM_MAX=${RMEM_MAX:-4194304}"
        test -f /proc/sys/net/core/rmem_max && echo ${RMEM_MAX:-4194304} > /proc/sys/net/core/rmem_max
        test -f /proc/sys/net/core/wmem_max && echo -n "  WMEM_MAX=${WMEM_MAX:-1048586}"
        test -f /proc/sys/net/core/wmem_max && echo ${WMEM_MAX:-1048586} > /proc/sys/net/core/wmem_max
        test -f /proc/sys/vm/vm_mapped_ratio && echo -n "  VM_MAPPED_RATIO=${VM_MAPPED_RATIO:-100}"
        test -f /proc/sys/vm/vm_mapped_ratio && echo ${VM_MAPPED_RATIO:-250} > /proc/sys/vm/vm_mapped_ratio
# aio-max-size is obsolete in 2.6 & it's changed to aio-max-nr 
        test -f /proc/sys/fs/aio-max-nr && echo "  AIO_MAX_SIZE=${AIO_MAX_SIZE:-1048576}"
        test -f /proc/sys/fs/aio-max-nr && echo ${AIO_MAX_SIZE:-1048576} > /proc/sys/fs/aio-max-nr
        test -f /proc/sys/fs/aio-max-nr || echo

# HUGEPAGES
        echo -n "${extd}Huge Pages:$norm     "
	# on SLES9
        test -f /proc/sys/vm/disable_cap_mlock && echo 1 > /proc/sys/vm/disable_cap_mlock

	# on SLES10
        test -f /proc/sys/vm/hugetlb_shm_group && echo -n "    SHM_GROUP=${SHM_GROUP}"
	SHM_GROUP_GID=`cat /etc/group | grep -w ${SHM_GROUP} |awk -F: '{ print $3 }'`
        test -f /proc/sys/vm/hugetlb_shm_group && echo ${SHM_GROUP_GID} > /proc/sys/vm/hugetlb_shm_group

        test -f /proc/sys/vm/nr_hugepages && echo -n "    NR_HUGE_PAGES=${NR_HUGE_PAGES:-0}"
        test -f /proc/sys/vm/nr_hugepages && echo ${NR_HUGE_PAGES:-0} > /proc/sys/vm/nr_hugepages

        echo

        echo -n "${extd}ULIMIT values:$norm    "
        echo    "  MAX_CORE_FILE_SIZE_SHELL=${MAX_CORE_FILE_SIZE_SHELL:-0}"
        ulimit -c ${MAX_CORE_FILE_SIZE_SHELL:-0}
        echo -n "                    FILE_MAX_SHELL=${FILE_MAX_SHELL:-65536}"
        ulimit -n ${FILE_MAX_SHELL:-65536}
        echo    "  PROCESSES_MAX_SHELL=${PROCESSES_MAX_SHELL:-16384}"
        ulimit -u ${PROCESSES_MAX_SHELL:-16384}

        # Check if shmmax is really set to what we want - on some systems and
        # certain settings the result could be shmmax=0 if you set it to e.g. 4GB!
        if [ `cat /proc/sys/kernel/shmmax` != "${SHMMAX:-3294967296}" ]; then
          echo "${warn}---- WARNING - SHMMAX could not be set properly ----$norm"
          echo "   Tried to set it to: ${SHMMAX:-3294967296}"
          echo "   Value is now:       `cat /proc/sys/kernel/shmmax`"
          echo "   You might try again with a lower value."
        fi
      fi
      echo

        # Check for /etc/sysctl.conf settings (see 'man sysctl') for conflicting kernel settings
        test -s /etc/sysctl.conf &&   \
        {
          echo "Checking sysctl.conf to overwrite previous kernel settings..."
          for i in kernel.shmmax kernel.shmmni kernel.shmall kernel.sem vm.nr_hugepages vm.hugetlb_shm_group\
                net.core.rmem_default net.core.rmem_max net.core.wmem_default net.core.wmem_max \
                fs.file-max net.ipv4.ip_local_port_range vm.vm_mapped_ratio fs.aio-max-nr ; do
                    line=`grep $i /etc/sysctl.conf | sed 's/\ //g'`
            name=`echo $line | cut -d'=' -f 1`
            value=`echo $line | cut -d'=' -f 2`
            if [ -n "$line" ]; then
              echo "   sysctl value $name=$value restored."
                USE_SYSCTL="yes"
            fi
          done
        }

        if [ "$USE_SYSCTL" = "yes" ]; then
              sysctl -q -p
        fi
      echo

      echo -n "Kernel parameters set for Oracle: "
      rc_status -v
      echo
      echo
    fi

    rc_reset

  if [ "$ORACLE_RAC" = "yes" ]; then
    echo -n "  - Starting Oracle Cluster Filesystem..."
    if [ "${START_ORACLE_RAC_OCFS2:-no}" = "yes" ]; then
	# Start o2cb If it's configured and not started
    	if ( (chkconfig -c o2cb off) ); then
      		$LOAD_O2CB start >& /dev/null;
    	fi
       $LOAD_OCFS2 start >& /dev/null
       rc_status -v -r
       echo -n "  - Mounting Oracle Cluster Filesystem(s)..."
       $MOUNT -a -t ocfs2
       rc_status -v -r
    else
       if [ ${START_ORACLE_RAC_OCFS2:-no} = "cannot" ]; then
         rc_status -s
       else
         rc_status -u
       fi
    fi

    rc_reset

    echo -n "  - Starting Oracle CRS..."
    if [ "${START_ORACLE_RAC_CRS:-no}" = "yes" ]; then
       /etc/init.d/init.crs start >& /dev/null
       rc_status -v -r
       sleep 2
       kill -1 1
    else
       if [ ${START_ORACLE_RAC_CRS:-no} = "cannot" ]; then
         rc_status -s
       else
         rc_status -u
       fi
    fi

    rc_reset

# Find Better way to start Enterprise Manager in RAC
    if [ "${START_ORACLE_DB_EMANAGER:-no}" = "yes" ]; then
    	echo -n "  - Starting Enterprise Manager... "
        rc_status -v -s
    fi

  else
# For Single Instance Database (ASM)
   if [ -x /etc/init.d/init.cssd -a ! -x /etc/init.d/init.crs ]
   then
       echo -n "  - Starting css daemon..."
       /etc/init.d/init.cssd start
       rc_status -v -r
      kill -1 1
      sleep 5
   fi

    rc_reset

    echo -n "  - Starting Listener..."
    if [ "${START_ORACLE_DB_LISTENER:-no}" = "yes" ]; then
       su - $ORACLE_OWNER -c "export ORACLE_HOME=$ORACLE_HOME TNS_ADMIN=$TNS_ADMIN; $ORACLE_HOME/bin/lsnrctl start > /dev/null"
      rc_status -v -r
    else
       if [ ${START_ORACLE_DB_LISTENER:-no} = "cannot" ]; then
         rc_status -s
       else
         rc_status -u
       fi
    fi
 
    rc_reset

    echo
    echo "  + Starting Database(s)..."
    if [ "${START_ORACLE_DB:-no}" = "yes" ]; then
      # Fix log directories: 9iR2 RAC installation forgets these directories on the nodes
      su - $ORACLE_OWNER -c "$MKDIR -p $ORACLE_HOME/rdbms/audit $ORACLE_HOME/rdbms/log $ORACLE_HOME/network/log 2>/dev/null"
      # The database will not start if the entry in /etc/oratab is "N"
      cat /etc/oratab | while read LINE
      do
        case $LINE in
          \#*) # skip over comment-line in oratab
           ;;
          *)
          # Proceed only if third field is 'Y'.
          if [ "`echo $LINE | awk -F: '{print $3}' -`" = "N" ] ; then
             ORACLE_SID=`echo $LINE | awk -F: '{print $1}' -`
             ORACLE_HOME=`echo $LINE | awk -F: '{print $2}' -`
             echo; echo -n "No start entry for SID $ORACLE_SID at $ORACLE_HOME in /etc/oratab"
             rc_status -s
          fi
          # Check if CRS is up before starting ASM - Bug#214774
	  # Need Better logic, this puts server in unusable state, If ASM is not up.
          #if [ "`echo $LINE | awk -F: '{print $1}' -`" = "+ASM" ] ; then
          #        if [ "`echo $LINE | awk -F: '{print $3}' -`" = "Y" ] ; then
          #              false
          #              while (($?)) ; do
          #                      echo "Waiting 20 seconds for CRS daemon"
          #                      sleep 20
          #                      ps -fe|grep -v grep|grep cssd
          #              done
          #        fi
          #fi
          ;;
        esac
      done
 
      echo -n "    "
	# bz#353957
	ORACLE_VERSION=`$ORACLE_HOME/bin/sqlplus -v | awk '{split($3, V, "."); print V[1]}'`
	if [ $ORACLE_VERSION = "11" ]; then
      		su - $ORACLE_OWNER -c "export ORACLE_HOME=$ORACLE_HOME TNS_ADMIN=$TNS_ADMIN; $ORACLE_HOME/bin/dbstart $ORACLE_HOME"
	else
      		su - $ORACLE_OWNER -c "export ORACLE_HOME=$ORACLE_HOME TNS_ADMIN=$TNS_ADMIN; $ORACLE_HOME/bin/dbstart"
	fi
      rc_status
      echo -n "    Status of Oracle database(s) start:"
      rc_status -v -r
      echo
    else
     if [ ${START_ORACLE_DB:-no} = "cannot" ]; then
       rc_status -s
     else
       rc_status -u
     fi
      echo
    fi

    rc_reset

    echo -n "  - Starting Apache..."
    if [ "${START_ORACLE_DB_APACHE:-no}" = "yes" ]; then
       SSL=""
       if [ ${ORACLE_DB_APACHE_USE_SSL} = "yes" ]; then
         SSL="ssl"
       fi
       export DISPLAY=:42.0; $ORACLE_HOME/Apache/Apache/bin/apachectl start$SSL >& /dev/null
       if [ "$?" != "0" ]; then rc_failed; else rc_failed 0; fi
       rc_status -v -r
    else
       if [ ${START_ORACLE_DB_APACHE:-no} = "cannot" ]; then
         rc_status -s
       else
         rc_status -u
       fi
    fi

    rc_reset

    echo -n "  - Starting Application Server..."
    if [ "${START_ORACLE_AS_ALL:-no}" = "yes" ]; then
       su - $ORACLE_OWNER -c "export ORACLE_HOME=$ORACLE_HOME; $ORACLE_HOME/opmn/bin/opmnctl startall > /dev/null"
       if [ "$?" != "0" ]; then rc_failed; else rc_failed 0; fi
       rc_status -v -r
    else
       if [ ${START_ORACLE_AS_ALL:-no} = "cannot" ]; then
         rc_status -s
       else
         rc_status -u
       fi
    fi

    rc_reset


    echo -n "  - Starting Application Server Console..."
    if [ "${START_ORACLE_AS_CONSOLE:-no}" = "yes" ]; then
       su - $ORACLE_OWNER -c "export ORACLE_HOME=$ORACLE_HOME; $ORACLE_HOME/bin/emctl start iasconsole > /dev/null"
       if [ "$?" != "0" ]; then rc_failed; else rc_failed 0; fi
       rc_status -v -r
    else
       if [ ${START_ORACLE_AS_CONSOLE:-no} = "cannot" ]; then
         rc_status -s
       else
         rc_status -u
       fi
    fi

    rc_reset

    echo -n "  - Starting Agent..."
    if [ "${START_ORACLE_DB_AGENT:-no}" = "yes" ]; then
       su - $ORACLE_OWNER -c "export ORACLE_HOME=$AGENT_HOME TNS_ADMIN=$TNS_ADMIN; $AGENT_START > /dev/null"
       rc_status -v -r
    else
       if [ ${START_ORACLE_DB_AGENT:-no} = "cannot" ]; then
         rc_status -s
       else
         rc_status -u
       fi
    fi

    rc_reset

    echo -n "  - Starting Enterprise Manager..."
    if [ "${START_ORACLE_DB_EMANAGER:-no}" = "yes" ]; then
	   echo
           for SID in `sed -n 's/^\([^#+]*\):.*:Y/\1/p' /etc/oratab`
           do
               echo -n "    Starting DB Console for $SID"
               su - $ORACLE_OWNER -c "export ORACLE_SID=$SID ORACLE_HOME=$ORACLE_HOME TNS_ADMIN=$TNS_ADMIN; $ORACLE_HOME/bin/emctl start dbconsole > /dev/null" < /dev/null &
               sleep 5
           done
           rc_status -v -r
    else
       if [ ${START_ORACLE_DB_EMANAGER:-no} = "cannot" ]; then
         rc_status -s
       else
         rc_status -u
       fi
    fi

    rc_reset

    echo -n "  - Starting iSQLPLUS Manager..."
    if [ "${START_ORACLE_DB_ISQLPLUS:-no}" = "yes" ]; then
        su - $ORACLE_OWNER -c "export ORACLE_HOME=$ORACLE_HOME TNS_ADMIN=$TNS_ADMIN; $ORACLE_HOME/bin/isqlplusctl start > /dev/null"
        rc_status -v -r
    else
       if [ ${START_ORACLE_DB_ISQLPLUS:-no} = "cannot" ]; then
         rc_status -s
       else          rc_status -u
       fi
    fi

    rc_reset

    echo -n "  - Starting Oracle Internet Filesystem..."
    if [ "${START_ORACLE_DB_IFS:-no}"  = "yes"  ]; then
       echo -n " ${extd}Not yet implemented.$norm"
       rc_status -s
    else
       if [ ${START_ORACLE_DB_IFS:-no} = "cannot" ]; then
         rc_status -s
       else
         rc_status -u
       fi
    fi

    rc_reset

    echo -n "  - Starting Oracle Internet Directory..."
    if [ "${START_ORACLE_DB_OID:-no}" = "yes" ]; then
       echo -n " ${extd}Not yet implemented.$norm"
       rc_status -s
    else
       if [ ${START_ORACLE_DB_OID:-no} = "cannot" ]; then
         rc_status -s
       else
         rc_status -u
       fi
    fi
  fi
    ;;
  stop)
    echo
    echo "#############################################################################"
    echo "#                 Begin of   O R A C L E   shutdown section                 #"
    echo "#############################################################################"
    echo
    ora_environment stop

    echo "Shutting down Oracle services (only those running)"; echo

   if [ "$ORACLE_RAC" = "yes" ]; then
    # TO DO - Find Better way to shutdown
    # test -x $ORACLE_HOME/bin/emctl           && test "${START_ORACLE_DB_EMANAGER:-no}" = "yes"      && echo -n "Shutting down Enterprise Manager: " && (su - $ORACLE_OWNER -c "export ORACLE_HOME=$ORACLE_HOME TNS_ADMIN=$TNS_ADMIN; $ORACLE_HOME/bin/emctl stop dbconsole > /dev/null"; rc_status -v -r)

    # init.crs stop prints Shutting down message
    test -x /etc/init.d/init.crs    && $CHECKPROC $ORA_CRS_HOME/bin/ocssd.bin && test  "${START_ORACLE_RAC_CRS:-no}" = "yes"              && echo -n   && (/etc/init.d/init.crs stop; sleep 5; rc_status -v -r)

    test -x /etc/init.d/ocfs2                   &&  test "${START_ORACLE_RAC_OCFS2:-no}" = "yes"         && echo -n "Unmounting OCFS2 Filesystem: " && ($UMOUNT -t ocfs2 -a; rc_status -v -r)

   else
    test -x $ORACLE_HOME/Apache/Apache/bin/apachectl && $CHECKPROC $ORACLE_HOME/Apache/Apache/bin/httpd  && echo -n "Shutting down Apache: " && (export ORACLE_HOME=$ORACLE_HOME TNS_ADMIN=$TNS_ADMIN; $ORACLE_HOME/Apache/Apache/bin/apachectl stop > /dev/null; rc_status -v -r)

# Shutting Down Agent
    if [ ${START_ORACLE_DB_AGENT:-no} = "yes" ]; then
    case $AGENT_VERSION in
    10g)
    test -x $AGENT_PROG && $CHECKPROC $AGENT_PROG  && echo -n "Shutting down Agent: " && (su - $ORACLE_OWNER -c "export ORACLE_HOME=$ORACLE_HOME TNS_ADMIN=$TNS_ADMIN; $AGENT_STOP > /dev/null"; rc_status -v -r)
    ;;
    10gA)
    test -x $AGENT_PROG && $CHECKPROC $AGENT_PROG  && echo -n "Shutting down Agent: " && (su - $ORACLE_OWNER -c "export ORACLE_HOME=$ORACLE_HOME TNS_ADMIN=$TNS_ADMIN; $AGENT_STOP > /dev/null"; rc_status -v -r)
    ;;
    9i)
    test -x $AGENT_PROG && $CHECKPROC $AGENT_PROG  && echo -n "Shutting down Agent: " && (su - $ORACLE_OWNER -c "export ORACLE_HOME=$ORACLE_HOME TNS_ADMIN=$TNS_ADMIN; $AGENT_STOP > /dev/null"; rc_status -v -r)
    ;;
    8i)
    test -x $AGENT_PROG && $CHECKPROC $AGENT_PROG  && echo -n "Shutting down Agent: " && (su - $ORACLE_OWNER -c "export ORACLE_HOME=$ORACLE_HOME TNS_ADMIN=$TNS_ADMIN; $AGENT_STOP > /dev/null"; rc_status -v -r)
    ;;
    *)
        echo "Oracle Agent undefinied or not properly set. Please check."
    ;;
    esac
    fi

    test -x $ORACLE_HOME/bin/isqlplusctl        && test  "${START_ORACLE_DB_ISQLPLUS:-no}" = "yes"        && echo -n "Shutting down iSQLPLUS Manager: " && (su - $ORACLE_OWNER -c "export ORACLE_HOME=$ORACLE_HOME TNS_ADMIN=$TNS_ADMIN; $ORACLE_HOME/bin/isqlplusctl stop > /dev/null"; rc_status -v -r)

    test -x $ORACLE_HOME/bin/emctl           && test "${START_ORACLE_DB_EMANAGER:-no}" = "yes"      && echo -n "Shutting down Enterprise Manager: " && (su - $ORACLE_OWNER -c "export ORACLE_HOME=$ORACLE_HOME TNS_ADMIN=$TNS_ADMIN; $ORACLE_HOME/bin/emctl stop dbconsole > /dev/null"; rc_status -v -r)

    test -x $ORACLE_HOME/bin/lsnrctl           && test  "${START_ORACLE_DB_LISTENER:-no}" = "yes"      && $CHECKPROC $ORACLE_HOME/bin/tnslsnr              && echo -n "Shutting down Listener: " && (su - $ORACLE_OWNER -c "export ORACLE_HOME=$ORACLE_HOME TNS_ADMIN=$TNS_ADMIN; $ORACLE_HOME/bin/lsnrctl stop > /dev/null"; rc_status -v -r)

    test -x $ORACLE_HOME/bin/dbshut            && test  "${START_ORACLE_DB:-no}" = "yes"      && $CHECKPROC $ORACLE_HOME/bin/oracle               && echo -n "Shutting down Database: " && (su - $ORACLE_OWNER -c "export ORACLE_HOME=$ORACLE_HOME TNS_ADMIN=$TNS_ADMIN; $ORACLE_HOME/bin/dbshut > /dev/null"; rc_status -v -r)

# This case is for Single Instance Database using ASM storage (Not RAC)
# for RAC init.crs will do this
    if [ -x /etc/init.d/init.cssd -a ! -x /etc/init.d/init.crs ]
    then
        echo -n "Shutting down CSSD:"
        /etc/init.d/init.cssd stop >/dev/null
	rc_status -v -r
        kill -1 1
        sleep 5
    fi
   fi
    ;;
  status)
    echo
    echo "#############################################################################"
    echo "#                  Begin of   O R A C L E   status section                  #"
    echo "#############################################################################"
    echo
    ora_environment status

    echo "${extd}Kernel Parameters$norm"
    echo -n "Shared memory:"
    echo -n "  SHMMAX=" `cat /proc/sys/kernel/shmmax`
    echo -n "  SHMMNI=" `cat /proc/sys/kernel/shmmni`
    echo    "  SHMALL=" `cat /proc/sys/kernel/shmall`
    echo -n "Semaphore values:"
    echo    "  SEMMSL, SEMMNS, SEMOPM, SEMMNI: " `cat /proc/sys/kernel/sem`
    echo

    if [ "$ORACLE_RAC" = "yes" ]; then
      echo "${extd}OCFS2 Status$norm"
      su - $ORACLE_OWNER -c "$LOAD_OCFS2 status"
      echo

      echo "${extd}Oracle Clusterware Status$norm"
      $CHECKPROC $ORA_CRS_HOME/bin/ocssd.bin &&( su - $ORACLE_OWNER -c "$ORA_CRS_HOME/bin/crsctl check crs" )
      echo
    fi

    if [ "$ORACLE_RAC" = "yes" ]; then
      echo "${extd}Cluster Database Status$norm"
      if ( $CHECKPROC $ORA_CRS_HOME/bin/ocssd.bin ); then
        DBNAME="`$ORA_CRS_HOME/bin/srvctl config database`"
#	echo "Database Name : " $DBNAME
	if [ "$DBNAME" != "" ]; then
     	$ORA_CRS_HOME/bin/srvctl status database -d `$ORA_CRS_HOME/bin/srvctl config database`
    	fi
    fi
      echo
    else
    if [ -x $ORACLE_HOME/bin/oracle ]; then
      echo "${extd}Database-Instances$norm"
      # loop over the instances (very simple !!!)
      IFS=:
      grep -v '^\(#\|$\)' /etc/oratab | while read sid ohome autostart ; do
        state=up
        su - $ORACLE_OWNER -c "export ORACLE_SID=$sid; sqlplus /nolog" <<-! 2>/dev/null | grep ORA-01034 >/dev/null && state=down
connect / as sysdba
show sga
!
        echo "Instance $sid is $state (autostart: $autostart)"
      done
      echo
    fi
    if [ -x $ORACLE_HOME/bin/lsnrctl ]; then
      state=up
      su - $ORACLE_OWNER -c "$ORACLE_HOME/bin/lsnrctl status" | grep "[nN]o [lL]istener" >/dev/null && state=down
      echo "${extd}TNS-Listener:$norm $state"
      echo
    fi

    numhttpd=`ps -e | grep httpd | wc -l | sed 's/ //g'`
    state=up
    if [ "$numhttpd" -lt 1 ] ; then state=down ; fi
    echo "${extd}Web-Server (Apache httpd):$norm $state ($numhttpd processes)"
    echo

    if [ -x $ORACLE_HOME/bin/agentctl ]; then
      state=up
      su - $ORACLE_OWNER -c "agentctl status" | grep "Could not contact agent" >/dev/null && state=down
      echo "${extd}Intelligent Agent:$norm $state"
      echo
    fi
   fi

    echo "${extd}Process list for user oracle:$norm"
    ps U oracle
    ps ax | grep oracm | grep -v grep
    ;;
  restart)
    ## Stop the service and regardless of whether it was
    ## running or not, start it again.
    $0 stop
    $0 start
    ;;
  *)
    echo "Usage: $0 {start|stop|status|restart}"
    exit 1
esac

echo
echo "#############################################################################"
echo "#                      End of   O R A C L E   section                       #"
echo "#############################################################################"
echo

# Global return value of this script is "success", always. We have too many
# individual return values...
rc_status -r

rc_exit

