#! /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 
# Should-Start: sshd
# Required-Stop:
# Default-Start: 3 5
# Default-Stop: 0 1 2 6
# Description: Start the Oracle database
### END INIT INFO

 
# 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


MOUNT=/bin/mount
UMOUNT=/bin/umount
MKDIR=/bin/mkdir
RMMOD=/sbin/rmmod

ORACLE_HOME=/opt/oracle/product/11.2.0/db_1
 
# 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

    echo -n "Starting DB..."
       su - oracle -c "dbstart $ORACLE_HOME > /dev/null"
DB_ROLE=$(su - oracle -c "sqlplus -S /nolog<<SQL1
  connect / as sysdba
  set pages 0
  set feedback off
  select trim(database_role) from v\\\$database;
  exit
SQL1")

if [ "${DB_ROLE}" = "PHYSICAL STANDBY" ]; then
su - oracle -c "sqlplus -S /nolog >/dev/null <<EOF1
connect / as sysdba
shutdown immediate
startup mount
EOF1"
fi
    ;;
  stop)
    echo
    echo "#############################################################################"
    echo "#                 Begin of   O R A C L E   shutdown section                 #"
    echo "#############################################################################"
    echo

	echo -n "Shutting down Database..." 
	 su - oracle -c "dbshut $ORACLE_HOME > /dev/null"
    ;;
  status)
    echo
    echo "#############################################################################"
    echo "#                  Begin of   O R A C L E   status section                  #"
    echo "#############################################################################"
    echo

      su - oracle -c "lsnrctl status" 
      echo "Process list for user oracle"
      ps -ef | grep ora
    ;;
  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
