#! /bin/sh
# $Id$
#bcpyrght
#***************************************************************************
#* $Copyright: Copyright (c) 2022 Veritas Technologies LLC. All rights reserved $ *
#***************************************************************************
#ecpyrght
#
# This script copies the bpstart_notify.oracle_bli scripts from
# /usr/openv/netbackup/ext/db_ext/oracle/samples on the client
# and prompts for the variables that need to be filled in.

BP_DIR=/usr/openv/netbackup
BP_BIN=${BP_DIR}/bin
SAMPLES_DIR=${BP_DIR}/ext/db_ext/oracle/samples
DEST_DIR=${BP_BIN}

# Make sure this is being run as root.

ISROOT=`id | egrep "^uid=0\("`
if [ "${ISROOT}" = "" ] ; then
	/bin/echo ""
	/bin/echo "This must be run while logged in as root."
	/bin/echo ""
	exit 1
fi

for file in bpstart_notify.oracle_bli.sh \
	    post_checkpoint_notify.oracle_bli.sh \
	    bpend_notify.oracle_bli.sh
do
	if [ -f ${SAMPLES_DIR}/${file} ] ; then
		cp ${SAMPLES_DIR}/${file} ${BP_BIN}/`basename ${file} .sh`.orig
		/bin/chmod 700 ${BP_BIN}/`basename ${file} .sh`.orig
	else
		/bin/echo ""
		/bin/echo "${SAMPLES_DIR}/${file} is missing.  Aborting..."
		/bin/echo ""
		exit 1
	fi
done

/bin/echo ""
/bin/echo "Please enter the user name of your Oracle administrator? \c"
read ORACLE_DBA



/bin/echo ""
/bin/echo "ORACLE_BASE is the Oracle enviroment variable that identifies"
/bin/echo "the directory at the top of the Oracle software and administrative"
/bin/echo "file structure. The value of this variable is typically"
/bin/echo "/MOUNTPOINT/app/oracle"
/bin/echo ""
/bin/echo "Please enter your ORACLE_BASE? \c"
read ORACLE_BASE

while [ -z "${ORACLE_BASE}" -o ! -d "${ORACLE_BASE}" ]
do
	/bin/echo "The directory ${ORACLE_BASE} cannot be found."
	/bin/echo "Please re-enter your ORACLE_BASE? \c"
	read ORACLE_BASE
done



/bin/echo ""
/bin/echo "ORACLE_HOME is the Oracle enviroment variable that identifies the"
/bin/echo "directory containing the Oracle software for a given Oracle server"
/bin/echo "release. The value of this variable is typically"
/bin/echo "${ORACLE_BASE}/product/RELEASE"
/bin/echo ""
/bin/echo "Please enter your ORACLE_HOME? \c"
read ORACLE_HOME

while [ -z "${ORACLE_HOME}" -o ! -d "${ORACLE_HOME}" ]
do
	/bin/echo "The directory ${ORACLE_HOME} cannot be found."
	/bin/echo "Please re-enter your ORACLE_HOME? \c"
	read ORACLE_HOME
done


SQLCMD="\"${ORACLE_HOME}/bin/sqlplus /nolog\""
SQLLOGIN="\"connect / as sysdba\""
/bin/echo ""
/bin/echo "sqlplus will be used."
/bin/echo ""
/bin/echo "The default \"connect\" statement that will be used to connect to the database is:"
/bin/echo "                            \"connect / as sysdba\""
/bin/echo ""
/bin/echo "Would you like to modify the connect and use a specific login? (y/n) \c"
read SQLRESP
if [ "${SQLRESP}" = "y" -o "${SQLRESP}" = "Y" ]
then
	/bin/echo ""
	/bin/echo "The Oracle login has the following format: "
	/bin/echo ""
	/bin/echo "login/passwd as sysdba  "
	/bin/echo ""
	/bin/echo "\"as sysdba\" - identifies the login will be used "
	/bin/echo "              for administrative purposes. "
	/bin/echo ""
	/bin/echo "Please enter the sqlplus login: \c"
	read LOGIN
	/bin/echo "Please enter the sqlplus passwd: \c"
	read PASSWD
	SQLLOGIN="\"connect $LOGIN/$PASSWD as sysdba\""
fi
/bin/echo ""
/bin/echo "$SQLLOGIN will be used."
/bin/echo ""

/bin/echo ""
/bin/echo "Please enter the Oracle instance (ORACLE_SID) you want to back up? \c"
read ORACLE_SID

/bin/echo ""
/bin/echo "Are you using an init file? \c"
read initans

case $initans in
Y*|y*)
	ORACLE_INIT=${ORACLE_BASE}/admin/${ORACLE_SID}/pfile/init${ORACLE_SID}.ora

	while [ -z "${ORACLE_INIT}" -o ! -f "${ORACLE_INIT}" ]
	do
		/bin/echo ""
		/bin/echo "To restart databases that are down, we need the path"
		/bin/echo "for your Oracle init file. Typically this would be:"
		/bin/echo "   ${ORACLE_BASE}/admin/${ORACLE_SID}/pfile/init${ORACLE_SID}.ora"
		/bin/echo "but this file could not be found."
		/bin/echo ""
		/bin/echo "Please enter your Oracle init file path? \c"
		read ORACLE_INIT
	done
	;;
esac


ORACLE_CONFIG=${ORACLE_BASE}/admin/${ORACLE_SID}/pfile/config${ORACLE_SID}.ora

if [ -z "${ORACLE_CONFIG}" -o ! -f "${ORACLE_CONFIG}" ]; then
	/bin/echo ""
	/bin/echo "If you are using a CONFIG.ORA file, you need to specify where" 
        /bin/echo "it is, so that it can be backed up. If this does not apply" 
	/bin/echo "apply to your configuration, hit ENTER to go on. If this does"
        /bin/echo "apply to your configuration, specify the file path."
	/bin/echo "Typically this would be:"
	/bin/echo "${ORACLE_BASE}/admin/${ORACLE_SID}/pfile/config${ORACLE_SID}.ora"
	/bin/echo "but this file could not be found."
	/bin/echo ""
	/bin/echo "Enter your Oracle config file path or hit ENTER: \c"
	read ORACLE_CONFIG
fi

while [ ! -z "${ORACLE_CONFIG}" -a ! -f "${ORACLE_CONFIG}" ]
do
        /bin/echo ""
        /bin/echo "If you are using a CONFIG.ORA file, you need to specify where"
        /bin/echo "it is, so that it can be backed up. If this does not apply"
        /bin/echo "to your configuration, hit ENTER to go on. If this does"
        /bin/echo "apply to your configuration, specify the file path."
        /bin/echo "Typically this would be:"
        /bin/echo "${ORACLE_BASE}/admin/${ORACLE_SID}/pfile/config${ORACLE_SID}.ora"
        /bin/echo "but this file could not be found."
        /bin/echo ""
        /bin/echo "Enter your Oracle config file path or hit ENTER: \c"
        read ORACLE_CONFIG
done




/bin/echo ""
/bin/echo "To back up a copy of the Oracle control file, you need to specify a file"
/bin/echo "path where Oracle can write a copy of the control file."
/bin/echo ""
/bin/echo "Please enter the file path where Oracle is to write a copy of your"
/bin/echo "control file? \c"
read ORACLE_CNTRL




/bin/echo ""
/bin/echo "To back up the Oracle archive logs, you need to specify their location."
/bin/echo ""


ORACLE_LOG_LIST=
MORE_LOGS=y
while [ "${MORE_LOGS}" = "y" -o "${MORE_LOGS}" = "Y" ]
do
  /bin/echo ""
  /bin/echo "Enter the directory path to your Oracle archive logs? \c"
  read ORACLE_LOGS

  while [ -z "${ORACLE_LOGS}" -o ! -d "${ORACLE_LOGS}" ]
  do
        /bin/echo ""
        /bin/echo "The directory entered does not exist."
        /bin/echo "Enter the directory path to your Oracle archive logs: \c"
        read ORACLE_LOGS
  done

  if [ -z "$ORACLE_LOG_LIST" ]
  then
        ORACLE_LOG_LIST="$ORACLE_LOG_LIST$ORACLE_LOGS"
        ORACLE_LOGS=""
  else
        ORACLE_LOG_LIST="$ORACLE_LOG_LIST $ORACLE_LOGS"
        ORACLE_LOGS=""
  fi

  MORE_LOGS=""
  while [ -z "${MORE_LOGS}" ]
  do
        /bin/echo ""
        /bin/echo "Do you have more archive log locations? (y/n): \c"
        read MORE_LOGS
  done
done



/bin/echo ""
/bin/echo "Do you want the output of successful executions of the NetBackup"
/bin/echo "scripts mailed to you? \c"
read ans

case $ans in
Y*|y*)
	/bin/echo "Please enter the mail address to send it to? \c"
	read MAIL_ADDR_SUCCESS
	;;
*)
	MAIL_ADDR_SUCCESS=xxxxxx
	;;
esac

/bin/echo ""
/bin/echo "Do you want the output of unsuccessful executions of the NetBackup"
/bin/echo "scripts mailed to you? \c"
read ans

case $ans in
Y*|y*)
	/bin/echo "Please enter the mail address to send it to? \c"
	read MAIL_ADDR_FAILURE
	;;
*)
	MAIL_ADDR_FAILURE=xxxxxx
	;;
esac




/bin/echo ""
/bin/echo "There are 4 backup methods to choose from:"
/bin/echo "   ALTER_TABLESPACE      - Use alter tablespace begin backup method"
/bin/echo "   NODATA_CKPT_HOT       - Use alter tablespace begin backup with nodata ckpts"
/bin/echo "   SHUTDOWN_CKPT_RESTART - Shutdown, create the ckpt clones, and restart"
/bin/echo "   SHUTDOWN_BKUP_RESTART - Shutdown the DB, backup, and then restart"
/bin/echo "If one of the methods requiring DB shutdown are selected, you may experience"
/bin/echo "problems with timeouts if the database can't be shut down in a timely"
/bin/echo "manner. You may want to change the shutdown command in the notify scripts to"
/bin/echo "shutdown immediate, or you may have to increase the BPSTART_TIMEOUT value in the"
/bin/echo "bp.conf file on the master server, or you may want to change the backup"
/bin/echo "method to ALTER_TABLESPACE or NODATA_CKPT_HOT."
/bin/echo "Note: the default BPSTART_TIMEOUT value is 300 seconds."

/bin/echo ""
/bin/echo "Do you want to use the ALTER_TABLESPACE method? \c"
read ans
case $ans in
Y*|y*)
	METHOD=ALTER_TABLESPACE
	;;
*)
	/bin/echo ""
	/bin/echo "Do you want to use the NODATA_CKPT_HOT method? \c"
	read ans
	case $ans in
	Y*|y*)
		METHOD=NODATA_CKPT_HOT
		;;
	*)
		/bin/echo ""
		/bin/echo "Do you want to use the SHUTDOWN_CKPT_RESTART method? \c"
		read ans
		case $ans in
		Y*|y*)
			METHOD=SHUTDOWN_CKPT_RESTART
			;;
		*)
			/bin/echo ""
			/bin/echo "Do you want to use the SHUTDOWN_BKUP_RESTART method? \c"
			read ans
			case $ans in
			Y*|y*)
				METHOD=SHUTDOWN_BKUP_RESTART
				;;
			*)
				/bin/echo ""
				/bin/echo "Setting the backup method to ALTER_TABLESPACE"
				METHOD=ALTER_TABLESPACE
				;;
			esac
			;;
		esac
		;;
	esac
	;;
esac


if [ ! -d ${ORACLE_HOME}/lib ]
then
	/bin/echo ""
	/bin/echo "The Oracle libraries need to be added to LD_LIBRARY_PATH."
	/bin/echo "Normally ${ORACLE_HOME}/lib would be added, but"
	/bin/echo "${ORACLE_HOME}/lib could not be found. You may have to"
	/bin/echo "modify the LD_LIBRARY_PATH variable in the scripts if"
	/bin/echo "you have problems."
fi


/bin/echo ""
/bin/echo "You now need to decide on how many NetBackup policies you will have"
/bin/echo "backing up simultaneously. The first one you enter will be known"
/bin/echo "as the POLICY_IN_CONTROL in the scripts and will perform any needed"
/bin/echo "DB operations. When you create the policies on the NetBackup server,"
/bin/echo "you will have to divide the filesystems between these policies."

/bin/echo ""
/bin/echo "Please enter the name of the policy that will be the POLICY_IN_CONTROL? \c"
read POLICY_IN_CONTROL

/bin/rm -f /tmp/bli_ed_?.$$ /tmp/bli_ed.out.$$
/bin/echo "1,\$s#ORACLE_DBA=xxxxxx#ORACLE_DBA=${ORACLE_DBA}#" >/tmp/bli_ed_1.$$
/bin/echo "w" >>/tmp/bli_ed_1.$$
/bin/echo "q" >>/tmp/bli_ed_1.$$
/bin/echo "1,\$s#POLICY_IN_CONTROL=xxxxxx#POLICY_IN_CONTROL=${POLICY_IN_CONTROL}#" >/tmp/bli_ed_2.$$
/bin/echo "w" >>/tmp/bli_ed_2.$$
/bin/echo "q" >>/tmp/bli_ed_2.$$
/bin/echo "1,\$s#ORACLE_BASE=xxxxxx#ORACLE_BASE=${ORACLE_BASE}#" >/tmp/bli_ed_3.$$
/bin/echo "w" >>/tmp/bli_ed_3.$$
/bin/echo "q" >>/tmp/bli_ed_3.$$
/bin/echo "1,\$s#ORACLE_HOME=xxxxxx#ORACLE_HOME=${ORACLE_HOME}#" >/tmp/bli_ed_4.$$
/bin/echo "w" >>/tmp/bli_ed_4.$$
/bin/echo "q" >>/tmp/bli_ed_4.$$
/bin/echo "1,\$s#SQLCMD=xxxxxx#SQLCMD=${SQLCMD}#" >/tmp/bli_ed_5.$$
/bin/echo "w" >>/tmp/bli_ed_5.$$
/bin/echo "q" >>/tmp/bli_ed_5.$$
/bin/echo "1,\$s#ORACLE_SID=\$KEYWORD#ORACLE_SID=${ORACLE_SID}#" >/tmp/bli_ed_6.$$
/bin/echo "w" >>/tmp/bli_ed_6.$$
/bin/echo "q" >>/tmp/bli_ed_6.$$
/bin/echo "1,\$s#ORACLE_INIT=.*#ORACLE_INIT=${ORACLE_INIT}#" >/tmp/bli_ed_7.$$
/bin/echo "w" >>/tmp/bli_ed_7.$$
/bin/echo "q" >>/tmp/bli_ed_7.$$
/bin/echo "1,\$s#ORACLE_CONFIG=.*#ORACLE_CONFIG=${ORACLE_CONFIG}#" >/tmp/bli_ed_8.$$
/bin/echo "w" >>/tmp/bli_ed_8.$$
/bin/echo "q" >>/tmp/bli_ed_8.$$
/bin/echo "1,\$s#ORACLE_CNTRL=.*#ORACLE_CNTRL=${ORACLE_CNTRL}#" >/tmp/bli_ed_9.$$
/bin/echo "w" >>/tmp/bli_ed_9.$$
/bin/echo "q" >>/tmp/bli_ed_9.$$
/bin/echo "1,\$s#SQLLOGIN=\"sys/change_on_install as sysdba\"#SQLLOGIN=${SQLLOGIN}#" >/tmp/bli_ed_10.$$
/bin/echo "w" >>/tmp/bli_ed_10.$$
/bin/echo "q" >>/tmp/bli_ed_10.$$
/bin/echo "1,\$s#ORACLE_LOGS=xxxxxx#ORACLE_LOGS=\"${ORACLE_LOG_LIST}\"#" >/tmp/bli_ed_a.$$
/bin/echo "w" >>/tmp/bli_ed_a.$$
/bin/echo "q" >>/tmp/bli_ed_a.$$
/bin/echo "1,\$s#MAIL_ADDR_SUCCESS=xxxxxx#MAIL_ADDR_SUCCESS=${MAIL_ADDR_SUCCESS}#" >/tmp/bli_ed_b.$$
/bin/echo "w" >>/tmp/bli_ed_b.$$
/bin/echo "q" >>/tmp/bli_ed_b.$$
/bin/echo "1,\$s#MAIL_ADDR_FAILURE=xxxxxx#MAIL_ADDR_FAILURE=${MAIL_ADDR_FAILURE}#" >/tmp/bli_ed_c.$$
/bin/echo "w" >>/tmp/bli_ed_c.$$
/bin/echo "q" >>/tmp/bli_ed_c.$$
/bin/echo "1,\$s#METHOD=ALTER_TABLESPACE#METHOD=${METHOD}#" >/tmp/bli_ed_d.$$
/bin/echo "w" >>/tmp/bli_ed_d.$$
/bin/echo "q" >>/tmp/bli_ed_d.$$

POLICY=${POLICY_IN_CONTROL}
while [ "${POLICY}" != "DONE" -a "${POLICY}" != "done" ]
do
	cp ${BP_BIN}/bpstart_notify.oracle_bli.orig ${BP_BIN}/bpstart_notify.${POLICY}
	cp ${BP_BIN}/post_checkpoint_notify.oracle_bli.orig ${BP_BIN}/post_checkpoint_notify.${POLICY}
	cp ${BP_BIN}/bpend_notify.oracle_bli.orig ${BP_BIN}/bpend_notify.${POLICY}

	/bin/ed -s ${BP_BIN}/bpstart_notify.${POLICY} </tmp/bli_ed_1.$$ >/tmp/bli_ed.out.$$
	/bin/ed -s ${BP_BIN}/post_checkpoint_notify.${POLICY} </tmp/bli_ed_1.$$ >/tmp/bli_ed.out.$$
	/bin/ed -s ${BP_BIN}/bpend_notify.${POLICY} </tmp/bli_ed_1.$$ >/tmp/bli_ed.out.$$
	/bin/ed -s ${BP_BIN}/bpstart_notify.${POLICY} </tmp/bli_ed_2.$$ >/tmp/bli_ed.out.$$
	/bin/ed -s ${BP_BIN}/post_checkpoint_notify.${POLICY} </tmp/bli_ed_2.$$ >/tmp/bli_ed.out.$$
	/bin/ed -s ${BP_BIN}/bpend_notify.${POLICY} </tmp/bli_ed_2.$$ >/tmp/bli_ed.out.$$
	/bin/ed -s ${BP_BIN}/bpstart_notify.${POLICY} </tmp/bli_ed_3.$$ >/tmp/bli_ed.out.$$
	/bin/ed -s ${BP_BIN}/post_checkpoint_notify.${POLICY} </tmp/bli_ed_3.$$ >/tmp/bli_ed.out.$$
	/bin/ed -s ${BP_BIN}/bpend_notify.${POLICY} </tmp/bli_ed_3.$$ >/tmp/bli_ed.out.$$
	/bin/ed -s ${BP_BIN}/bpstart_notify.${POLICY} </tmp/bli_ed_4.$$ >/tmp/bli_ed.out.$$
	/bin/ed -s ${BP_BIN}/post_checkpoint_notify.${POLICY} </tmp/bli_ed_4.$$ >/tmp/bli_ed.out.$$
	/bin/ed -s ${BP_BIN}/bpend_notify.${POLICY} </tmp/bli_ed_4.$$ >/tmp/bli_ed.out.$$
	/bin/ed -s ${BP_BIN}/bpstart_notify.${POLICY} </tmp/bli_ed_5.$$ >/tmp/bli_ed.out.$$
	/bin/ed -s ${BP_BIN}/post_checkpoint_notify.${POLICY} </tmp/bli_ed_5.$$ >/tmp/bli_ed.out.$$
	/bin/ed -s ${BP_BIN}/bpend_notify.${POLICY} </tmp/bli_ed_5.$$ >/tmp/bli_ed.out.$$
	/bin/ed -s ${BP_BIN}/bpstart_notify.${POLICY} </tmp/bli_ed_6.$$ >/tmp/bli_ed.out.$$
	/bin/ed -s ${BP_BIN}/post_checkpoint_notify.${POLICY} </tmp/bli_ed_6.$$ >/tmp/bli_ed.out.$$
	/bin/ed -s ${BP_BIN}/bpend_notify.${POLICY} </tmp/bli_ed_6.$$ >/tmp/bli_ed.out.$$
	/bin/ed -s ${BP_BIN}/bpstart_notify.${POLICY} </tmp/bli_ed_7.$$ >/tmp/bli_ed.out.$$
	/bin/ed -s ${BP_BIN}/post_checkpoint_notify.${POLICY} </tmp/bli_ed_7.$$ >/tmp/bli_ed.out.$$
	/bin/ed -s ${BP_BIN}/bpend_notify.${POLICY} </tmp/bli_ed_7.$$ >/tmp/bli_ed.out.$$
	/bin/ed -s ${BP_BIN}/bpstart_notify.${POLICY} </tmp/bli_ed_8.$$ >/tmp/bli_ed.out.$$
	/bin/ed -s ${BP_BIN}/post_checkpoint_notify.${POLICY} </tmp/bli_ed_8.$$ >/tmp/bli_ed.out.$$
	/bin/ed -s ${BP_BIN}/bpend_notify.${POLICY} </tmp/bli_ed_8.$$ >/tmp/bli_ed.out.$$
	/bin/ed -s ${BP_BIN}/bpstart_notify.${POLICY} </tmp/bli_ed_9.$$ >/tmp/bli_ed.out.$$
	/bin/ed -s ${BP_BIN}/post_checkpoint_notify.${POLICY} </tmp/bli_ed_9.$$ >/tmp/bli_ed.out.$$
	/bin/ed -s ${BP_BIN}/bpend_notify.${POLICY} </tmp/bli_ed_9.$$ >/tmp/bli_ed.out.$$
	/bin/ed -s ${BP_BIN}/bpstart_notify.${POLICY} </tmp/bli_ed_10.$$ >/tmp/bli_ed.out.$$
	/bin/ed -s ${BP_BIN}/post_checkpoint_notify.${POLICY} </tmp/bli_ed_10.$$ >/tmp/bli_ed.out.$$
	/bin/ed -s ${BP_BIN}/bpend_notify.${POLICY} </tmp/bli_ed_10.$$ >/tmp/bli_ed.out.$$
	/bin/ed -s ${BP_BIN}/bpstart_notify.${POLICY} </tmp/bli_ed_a.$$ >/tmp/bli_ed.out.$$
	/bin/ed -s ${BP_BIN}/post_checkpoint_notify.${POLICY} </tmp/bli_ed_a.$$ >/tmp/bli_ed.out.$$
	/bin/ed -s ${BP_BIN}/bpend_notify.${POLICY} </tmp/bli_ed_a.$$ >/tmp/bli_ed.out.$$
	/bin/ed -s ${BP_BIN}/bpstart_notify.${POLICY} </tmp/bli_ed_b.$$ >/tmp/bli_ed.out.$$
	/bin/ed -s ${BP_BIN}/post_checkpoint_notify.${POLICY} </tmp/bli_ed_b.$$ >/tmp/bli_ed.out.$$
	/bin/ed -s ${BP_BIN}/bpend_notify.${POLICY} </tmp/bli_ed_b.$$ >/tmp/bli_ed.out.$$
	/bin/ed -s ${BP_BIN}/bpstart_notify.${POLICY} </tmp/bli_ed_c.$$ >/tmp/bli_ed.out.$$
	/bin/ed -s ${BP_BIN}/post_checkpoint_notify.${POLICY} </tmp/bli_ed_c.$$ >/tmp/bli_ed.out.$$
	/bin/ed -s ${BP_BIN}/bpend_notify.${POLICY} </tmp/bli_ed_c.$$ >/tmp/bli_ed.out.$$
	/bin/ed -s ${BP_BIN}/bpstart_notify.${POLICY} </tmp/bli_ed_d.$$ >/tmp/bli_ed.out.$$
	/bin/ed -s ${BP_BIN}/post_checkpoint_notify.${POLICY} </tmp/bli_ed_d.$$ >/tmp/bli_ed.out.$$
	/bin/ed -s ${BP_BIN}/bpend_notify.${POLICY} </tmp/bli_ed_d.$$ >/tmp/bli_ed.out.$$
	
	/bin/echo "Please enter the name of another policy or DONE to stop? \c"
	read POLICY
done



/bin/rm -f /tmp/bli_ed_?.$$ /tmp/bli_ed.out.$$



