#!/bin/bash
#
# instance_start
#

# 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 "Starting Oracle Database Server"
echo
date +"%Y-%m-%d %H:%M:%S"
echo
echo "ORACLE_HOME=$ORACLE_HOME"
echo "ORACLE_SID=$ORACLE_SID"
echo
echo "+------------------------------------------------------------+"
echo "|                                                            |"
echo "| Performing a STARTUP, this may take a while...             |"
echo "|                                                            |"
echo "+------------------------------------------------------------+"
echo

echo "STARTUP" | sqlplus "/ as sysdba"

# exit value is zero if found
echo 'select status from v$instance;' | sqlplus "/ as sysdba" | grep OPEN
if [ $? -ne 0 ] ; then
  echo
  echo "================================================================"
  echo "ERROR: Database is not 'OPEN'! Shutting down..."
  echo "SHUTDOWN IMMEDIATE" | sqlplus "/ as sysdba"
  echo "================================================================"
  exit 1
fi

echo
echo "SUCCESS: Database is 'OPEN'."
echo


# -----
# Listener

if [ ! -z "$1" ] ; then
  if [ "$1" != "NONE" ] ; then
    echo "Starting the non-default listener $1"

    lsnrctl start $1
    if [ $? -ne 0 ] ; then
      echo "WARNING: Cannot start the non-default listener $1!"
    fi
  fi
else
  echo "Starting the default listener"
  lsnrctl start
  if [ $? -ne 0 ] ; then
    echo "WARNING: Cannot start the listener!"
  fi
fi

# ------
# ipprot logging
#
# Recording of external access via listener, see also 'instance_stop'
#
# You must change the <listen_ip> and <dest_ip> to your needs!
# You may change the ports also.
# Activate the related part in the instance_stop script also.

# ipprotd -p <listen_port>@<listen_ip> -P <dest_port>@<dest_ip> -L -s -t 180 -f /oracle/admin/$ORACLE_SID/connection_protocol/prot -j -u /oracle/admin/$ORACLE_SID/send_ipprot -Dp /oracle/admin/$ORACLE_SID/connection_protocol/pid_<listen_port>_<listen_ip>

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

