#!/bin/ksh93
#  ALTRAN_PROLOG_BEGIN_TAG                                                    
#  This is an automatically generated prolog.                                  
#                                                                              
#  Copyright (C) Altran ACT S.A.S. 2020,2021.  All rights reserved.  
#                                                                              
#  ALTRAN_PROLOG_END_TAG                                                      
#                                                                              
# IBM_PROLOG_BEGIN_TAG 
# This is an automatically generated prolog. 
#  
# 61haes_r714 src/43haes/usr/sbin/cluster/sa/oracle/sbin/cl_oraSqlPlus.sh 1.7 
#  
# Licensed Materials - Property of IBM 
#  
# Restricted Materials of IBM 
#  
# COPYRIGHT International Business Machines Corp. 2006,2011 
# All Rights Reserved 
#  
# US Government Users Restricted Rights - Use, duplication or 
# disclosure restricted by GSA ADP Schedule Contract with IBM Corp. 
#  
# IBM_PROLOG_END_TAG 
# @(#)  7d4c34b 43haes/usr/sbin/cluster/sa/oracle/sbin/cl_oraSqlPlus.sh, 726, 2147A_aha726, Feb 05 2021 09:50 PM

[[ $VERBOSE_LOGGING == high ]] && set -x

##
## NAME:
##      cl_oraSqlPlus
##
## PURPOSE: 
##      Wrapper for the sqplus command
##      Runs as user oracle (group dba)
##
## ARGUMENTS:
##      -e filename         Environment File to source before
##                          calling sqlplus
##
##      -p path             Path to SQLPLUS 
##
##      -r path             Path to store the result
##
##      -s filename         File to provide to SQLPLUS 
##
##      -l login            Login Arguments to sqlplus ('/ as sysdba' default)
##
## OUTPUT:
##      Output of SQLPLUS command
##
## RETURNS:
##      return code from sqlplus
##

LOGIN='/ as sysdba'

while getopts o:e:p:r:s:l:S: option; do
    case $option in
    e)  ENVFILE=$OPTARG ;;
    p)  SQLPLUS_PATH=$OPTARG ;;
    r)  RCFILE_PATH=$OPTARG ;;
    s)  LOADFILE=$OPTARG ;;
    l)  LOGIN=$OPTARG ;;
    o)  ORACLE_HOME=$OPTARG ;;
    S)  ORACLE_SID=$OPTARG ;;
    esac
done

[[ -z $SQLPLUS_PATH || ! -f $SQLPLUS_PATH/sqlplus ]] && {
    echo "Invalid sqlplus path: \"$SQLPLUS_PATH\""
    exit 1
}

[[ -z $LOADFILE || ! -f $LOADFILE || ! -r $LOADFILE ]] && {
    echo "Invalid SQL command file: \"$LOADFILE\""
    exit 2
}

[[ -z $RCFILE_PATH ]] && {
    echo "Invalid result file: \"$RCFILE_PATH\""
    exit 2
}

[[ -n $ENVFILE && -f $ENVFILE ]] &&
    . $ENVFILE

# Record the actual sqlplus command and LOADFILE content into RCFILE_PATH.
# These will be later appended to oraclesa.log file in osaSQLPlusExecute() function.

echo "LC_ALL=C ORACLE_SID=$ORACLE_SID ORACLE_HOME=$ORACLE_HOME $SQLPLUS_PATH/sqlplus -S \"$LOGIN\"\n$(cat $LOADFILE)" > $RCFILE_PATH

typeset out=$(LC_ALL=C ORACLE_SID=$ORACLE_SID ORACLE_HOME=$ORACLE_HOME $SQLPLUS_PATH/sqlplus -S "$LOGIN" 2>>$RCFILE_PATH < $LOADFILE)
rc=$?
echo "$out" | tee -a $RCFILE_PATH

# Collect return code as well.
echo "sqlplus return code is:$rc" >> $RCFILE_PATH

