#!/bin/bash
#
# instance_stop
#

# YYYY-MM-DD when the script has been started.
STARTED=`date +"%Y-%m-%d"`

# -----
# Go to directory where this script is placed
cd `dirname $0`

# -----
# Set environment
. ./oracle_env
if [ $? -ne 0 ] ; then
  echo
  echo "Error: Cannot source Oracle's environment script './oracle_env'"
  echo
  exit 1
fi

# -----
# HOSTNAME is used, but normally not set in cronjobs

HOSTNAME=`uname -n`
export HOSTNAME

# Remember to include the directory where flush_test_values can
# be found ('/usr/bin' or '/usr/local/bin') in the PATH. Should be
# set in the './oracle_env'.

echo
echo "================================================================"
echo "Stopping Oracle Database Server"
echo
date +"%Y-%m-%d %H:%M:%S"
echo
echo "ORACLE_HOME=$ORACLE_HOME"
echo "ORACLE_SID=$ORACLE_SID"
echo

if [ ! -z "$1" ] ; then
  if [ "$1" != "NONE" ] ; then
    echo "Stopping the listener $1"
    lsnrctl stop $1
  fi
else
  echo "Stopping the default listener"
  lsnrctl stop
fi

# -----
# ipprot daemon
#
# Recording of external access, see also 'instance_start'
# Activate also the related part in the instance_start script.

# if [ -r /oracle/admin/$ORACLE_SID/connection_protocol/pid_<listen_port>_<listen_ip> ]; then
#   echo "Stopping the ipprot daemon."
#   kill `cat /oracle/admin/$ORACLE_SID/connection_protocol/pid_<listen_port>_<listen_ip>`
#   echo "Done."
# fi

# -----
# Shut down the database

echo
echo "+------------------------------------------------------------+"
echo "|                                                            |"
echo "| Performing a SHUTDOWN IMMEDIATE, this may take a while...  |"
echo "|                                                            |"
echo "| 'SQL>' may be displayed for several minutes,               |"
echo "|        don't be impatient, this is Oracle!                 |"
echo "|                                                            |"
echo "+------------------------------------------------------------+"

echo 'SHUTDOWN IMMEDIATE' | sqlplus "/ as sysdba"

echo "================================================================"
exit 0


