#!/bin/bash

. ./param_bash.ini

clear
test -s ${IKIT_DIR}/tmp/.dbcreation.pid
if [ "$?" = "0" ] ; then
    CANCEL "A pid file exist:${IKIT_DIR}/tmp/.dbcreation.pid, check if db creation step is running, if not delete the pid file and execute this step again" "$0"
fi
if [ "$INST_TYPE" = "SW_ONLY" ] ; then
    DONE "the installation type you choose doesn't require to start listener" "$0"
fi
START "Startup Oracle listener" "$0"
if [ ! -w /etc/passwd ] && [ "~" = "$ORACLE_HOME" ] ; then
    ERROR "please run the script as $ORACLE_USER  user or root user" "$0"
fi


OracleUserExist
if [ "$?" = "1" ] ; then
    ERROR "the $ORACLE_HOME user doesn't exist" "$0"
else
    ORACLE_USER=`cat /etc/passwd | grep $ORACLE_HOME |tail -1 | awk -F: '{print $1}'` > /dev/null 2>&1
fi


if [ ! -d $ORACLE_HOME/network/admin ] ; then
    ERROR "the Oracle software $ORACLE_HOME does not exist in the server" "$0"
fi

ORACLE_USER=`cat /etc/passwd | grep $ORACLE_HOME |tail -1 | awk -F: '{print $1}'` > /dev/null 2>&1

$PS | grep -v grep | grep LISTENER
if [ "$?" != "0" ] ; then
  WAIT "Starting Oracle listener"
  if [ -w /etc/passwd ] ; then
        su $param_to_su $ORACLE_USER -c "csh -f ora_lis_startup.csh ${ORACLE_HOME}"  >> $gen_log 2>&1
  else
        csh -f ora_lis_startup.csh ${ORACLE_HOME} >> $gen_log 2>&1
  fi
  if [ "$?" != "0" ] ; then
        ERROR "can not startup Oracle listener" "$0"
  else
        DONE "Oracle listener was startup properly" "$0"
  fi


else

        pi=`$PS | grep LISTENER | grep -v grep | head -1 | awk '{print $2}'` > /dev/null 2>&1
        echo ""
        echo "The Oracle listener pid = $pi is currently running on the server"
        echo "1. Do not close the existing window and open a new terminal window,"
        echo " in this second window please shutdown the other ExLibris applications on this server"
        echo " that using currently running listener."
        echo "2. In this additional window shutdown the currently running Oracle listener using (util o 2)."
        echo "3. In the first terminal window rerun KIT step 3.4"
        echo "4. In the additional window startup the other ExLibris applications on this server that were shutdown."
        CANCEL "" "$0"
fi
