#!/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
START "Check connection to the database via 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 [ "$MODE" = "0" ] ; then
    echo -n "Please enter the name of the database [$ORACLE_SID] : "
    read ans
    if [ "$ans" != "" ] ; then	
        ORACLE_SID=$ans	
    fi 	
fi

export ORACLE_HOME

PATH=${PATH}:${ORACLE_HOME}/bin
export PATH
if [ "$PROMPT_FOR_ORA_PASS" = "N" ] ; then
# check oracle passwd
    ORA11_PASSWD="oradba"
    ORA10_PASSWD="exlibris2005"
    echo "quit" | sqlplus -L $DB_USER/$ORA11_PASSWD@$ORACLE_HOST.$ORACLE_SID > /dev/null 2>&1
    if [ "$?" = "0" ] ; then
        DB_PASSWD=$ORA11_PASSWD
        Update_params "DB_PASSWD" "$ORA11_PASSWD"
    else
        DB_PASSWD=$ORA10_PASSWD
        Update_params "DB_PASSWD" "$ORA10_PASSWD"
    fi
fi

sqlplus  /nolog <<EOF >> $gen_log 2>&1
connect EXL_ADMIN/$EXL_ADMIN_PASSWD@$ORACLE_HOST.$ORACLE_SID
spool $temp/test
select * from v\$version;
/
spool off
exit
EOF
cat $temp/test.lst >> $gen_log
grep "Oracle Database " $temp/test.lst
if [ "$?" = "0" ] ; then 
    DONE "the connection to the database $ORACLE_SID via listener working properly" "$0"	
    echo "Press Enter when Ready..."
    read
else
    INFO "The connection to the database $ORACLE_SID via listener hasn't been established yet..." "$0"
    exit 1
fi
