#! /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
# Required-Stop:
# 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


LOAD_OCFS=/sbin/load_ocfs
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
  if [ ! -z "$ORACLE_HOME" -a ! -d "$ORACLE_HOME" ]; then
    echo
    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 test -x "$AGENT_HOME/bin/emctl"; then
    # If Oracle Grid Control Agent is installed 
    AGENT_VERSION="10gA"
    AGENT_PROG="$AGENT_HOME/bin/emctl"
    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

#  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

  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}"
    echo " - Start Oracle OCFS:                  ${START_ORACLE_DB_OCFS:-no}"
    echo " - Start Oracle OCM:                   ${START_ORACLE_DB_OCM:-no}"
    echo " - Start Oracle GSD:                   ${START_ORACLE_DB_GSD:-no}"
    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
}


# 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 [ ! -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
     
    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

    if [ ! -x /sbin/load_ocfs -a ${START_ORACLE_DB_OCFS:-no} = "yes" ]; then
      echo "${warn}Can't find needed file: /sbin/load_ocfs - Setting START_ORACLE_DB_OCFS = no $norm"
      START_ORACLE_DB_OCFS="cannot";
    fi

    if [ ! -x $ORACLE_HOME/oracm/bin/oracm -a ${START_ORACLE_DB_OCM:-no} = "yes" ]; then
      echo "${warn}Can't find needed file: oracm - Setting START_ORACLE_DB_OCM = no $norm"
      START_ORACLE_DB_OCM="cannot";
    fi

    if [ ! -x $ORACLE_HOME/bin/gsdctl -a ${START_ORACLE_DB_GSD:-no} = "yes" ]; then
      echo "${warn}Can't find needed file: gsdctl - Setting START_ORACLE_DB_GSD = no $norm"
      START_ORACLE_DB_GSD="cannot";
    fi

    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:-131072}"  
        test -f /proc/sys/fs/file-max && echo ${FILE_MAX_KERNEL:-131072} > /proc/sys/fs/file-max  
        test -f /proc/sys/net/ipv4/ip_local_port_range && echo "  IP_LOCAL_PORT_RANGE=${IP_LOCAL_PORT_RANGE:-"1024 65000"}"  
        test -f /proc/sys/net/ipv4/ip_local_port_range && echo ${IP_LOCAL_PORT_RANGE:-"1024 65000"} > /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:-262144}"
        test -f /proc/sys/net/core/rmem_max && echo ${RMEM_MAX:-262144} > /proc/sys/net/core/rmem_max
        test -f /proc/sys/net/core/wmem_max && echo -n "  WMEM_MAX=${WMEM_MAX:-262144}"
        test -f /proc/sys/net/core/wmem_max && echo ${WMEM_MAX:-262144} > /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
        test -f /proc/sys/fs/aio-max-size && echo "  AIO_MAX_SIZE=${AIO_MAX_SIZE:-262144}"
        test -f /proc/sys/fs/aio-max-size && echo ${AIO_MAX_SIZE:-262144} > /proc/sys/fs/aio-max-size
        test -f /proc/sys/fs/aio-max-size || 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
    
      echo -n "Kernel parameters set for Oracle: "
      rc_status -v
      echo
      echo
    fi

    rc_reset

    echo -n "  - Starting Oracle Cluster Filesystem..."
    if [ "${START_ORACLE_DB_OCFS:-no}" = "yes" ]; then
       $RMMOD ocfs >& /dev/null
       $LOAD_OCFS >& /dev/null
       rc_status -v -r
       echo -n "  - Mounting Oracle Cluster Filesystem(s)..."
       $MOUNT -a -t ocfs >& /dev/null
       rc_status -v -r
    else
       if [ ${START_ORACLE_DB_OCFS:-no} = "cannot" ]; then
         rc_status -s
       else
         rc_status -u
       fi
    fi

    rc_reset

    echo -n "  - Starting Oracle Cluster Manager..."
    if [ "${START_ORACLE_DB_OCM:-no}" = "yes" ]; then
       # Fix log directories: 9iR2 RAC installation forgets these directories on the nodes
       su - $ORACLE_OWNER -c "$MKDIR -p $ORACLE_HOME/oracm/log"

       # Startup oracm
       cat <<EOF > /etc/rac_on
This file is created by /etc/rc.d/oracle when START_ORACLE_DB_OCM="yes"
in /etc/sysconfig/oracle. This is a dirty hack from Oracle - their
$ORACLE_HOME/bin/dbca script uses the presence of this file to select
one of two ways to start the JRE and when this file you are looking at
right now does not exist it uses a method that lets the JRE crash.
EOF
       test -x $ORACLE_HOME/oracm/bin/oracm && PATH=${ORACLE_HOME}/oracm/bin:${PATH} oracm $ORACLE_DB_OCM_PARAMETERS </dev/null 2>&1 >$ORACLE_HOME/oracm/log/cm.out &
       sleep 7
       rc_status -v -r
    else
       if [ ${START_ORACLE_DB_OCM:-no} = "cannot" ]; then
         rc_status -s
       else
         rc_status -u
       fi
    fi

    rc_reset

   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

    echo -n "  - Starting Oracle GSD..."
    if [ "${START_ORACLE_DB_GSD:-no}" = "yes" ]; then
       test -x $ORACLE_HOME/bin/gsdctl && su - $ORACLE_OWNER -c "export ORACLE_HOME=$ORACLE_HOME TNS_ADMIN=$TNS_ADMIN; unset JAVA_BINDIR; unset JAVA_HOME; $ORACLE_HOME/bin/gsdctl start > /dev/null"
       rc_status -v -r
    else
       if [ ${START_ORACLE_DB_GSD:-no} = "cannot" ]; then
         rc_status -s
       else
         rc_status -u
       fi
    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
          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 "    "
      su - $ORACLE_OWNER -c "export ORACLE_HOME=$ORACLE_HOME TNS_ADMIN=$TNS_ADMIN; $ORACLE_HOME/bin/dbstart"
      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/opmntcl 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/e
mctl 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
           for SID in `sed -n 's/^\([^#+]*\):.*:Y/\1/p' /etc/oratab`
           do
               echo "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 10
           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
    ;;
  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

    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                 && $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                  && $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)

    test -x $ORACLE_HOME/bin/gsdctl                  && test "" != "`ps U oracle|grep 'DPROGRAM=gsd'`"   && echo -n "Shutting down GSD: " && (su - $ORACLE_OWNER -c "export ORACLE_HOME=$ORACLE_HOME TNS_ADMIN=$TNS_ADMIN; unset JAVA_BINDIR; unset JAVA_HOME; $ORACLE_HOME/bin/gsdctl stop > /dev/null"; rc_status -v -r)

    test -x $ORACLE_HOME/oracm/bin/oracm             && $CHECKPROC $ORACLE_HOME/oracm/bin/oracm          && echo -n "Shutting down OCM: " && (killall oracm > /dev/null; rm -f /etc/rac_on; rc_status -v -r)

    if [ "`$MOUNT -t ocfs | grep ocfs`" != "" ]; then
      echo -n "Unmounting all OCFS filesystems: " && ($UMOUNT -t ocfs -a; rc_status -v -r)
    fi

    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
    ;;
  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 [ -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

    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

