#!/bin/bash

. ./param_bash.ini

clear
if [ "$INST_TYPE" != "DB_ONLY" ] ; then
    INFORM "This step is not required" 
    DONE "" "$0"
fi
START "Creating auto stratup/shutdown scripts for oracle(start_stop and init.dat files)" "ora_strstp.bash"
if [ ! -w /etc/passwd ] && [ ~ != "$ORACLE_HOME" ] ; then
        ERROR "Run the script as $ORACLE_USER user or root user" "ora_strstp.bash"
fi

OracleUserExist
if [ "$?" = "1" ] ; then
        ERROR "The $ORACLE_HOME user does not exist" "ora_strstp.bash"
else
        ORACLE_USER=`cat /etc/passwd | grep $ORACLE_HOME |tail -1 | awk -F: '{print $1}'` > /dev/null 2>&1
fi
if [ ! -d $STARTUP_DIR ] ; then
	ERROR "The directory $STARTUP_DIR does not exist on the server" "ora_strstp.bash"
fi

case $PATH in
    "") PATH=/bin:/usr/bin:/etc:.
        export PATH ;;
esac

grep "#ver_num" start_stop > /dev/null 
if [ "$?" = "0" ] ; then
     ver_num_s=`grep "#ver_num" start_stop | awk -F: '{print $2}'`
fi
if [ ! -f $STARTUP_DIR/start_stop ] ; then
     cp -p start_stop $STARTUP_DIR
else
     grep "#ver_num" $STARTUP_DIR/start_stop > /dev/null 2>&1
     if [ "$?" = "0" ] ; then
		ver_num_t=`grep "#ver_num" $STARTUP_DIR/start_stop | awk -F: '{print $2}'`
         if [ "$ver_num_s" -gt "$ver_num_t" ] ; then
            mv $STARTUP_DIR/start_stop $STARTUP_DIR/start_stop.$DD
            cp -p start_stop $STARTUP_DIR 
	 fi
     else
        mv $STARTUP_DIR/start_stop $STARTUP_DIR/start_stop.$DD
        cp -p start_stop $STARTUP_DIR
     fi
fi

if [ ! -f $STARTUP_DIR/init.dat ] ; then
     cp -p init.dat $STARTUP_DIR
fi

ORACLE_USER=`cat /etc/passwd | grep $ORACLE_HOME | tail -1 | awk -F: '{print $1}'`
oracle_string="Y:oracle:$ORACLE_USER:$ORACLE_HOME"
flo=0
flt=none
grep "$ORACLE_HOME" $STARTUP_DIR/init.dat > /dev/null 2>&1
STATUS=$?
if [ "$STATUS" = "0" ] ; then
   flo=1
fi
FILE_OUT=$STARTUP_DIR/init.dat.tmp
rm -rf $FILE_OUT > /dev/null 2>&1
cat $STARTUP_DIR/init.dat | while read LINE
do
# echo $LINE
	if [ "$LINE" != "" ] ; then
		echo $LINE >> $FILE_OUT
	fi

	s1=`echo $LINE | awk '{print $1}' -`
	s2=`echo $LINE | awk '{print $2}' -`
	if [ "$s1" = "#" -a "$s2" = "oracle" ] ; then
		flt="oracle"
	fi
	if [ "$flt" != "none" ] ; then
     		s1a=`echo $LINE | awk -F: '{print $1}' -`
        	case $flt in
          		oracle)
            			if [ "$flo" = "0" ] ; then
               				echo $oracle_string >> $FILE_OUT
            			else
               				echo "The following string was found in $STARTUP_DIR/init.dat:"
               				echo $oracle_string
            			fi
         		 ;;
		esac
        	flt=none
  	fi 
	if [ "$LINE" = "" ] ; then
     		echo "" >> $FILE_OUT
  	fi
done
DD=`date +'%y%m%d.%H%M%S'`
mv $STARTUP_DIR/init.dat $STARTUP_DIR/init.dat.$DD
mv $FILE_OUT $STARTUP_DIR/init.dat
##############################################################################
grep "$ORACLE_HOME" $STARTUP_DIR/init.dat > /dev/null 2>&1
if [ "$?" = "0" ] ; then
	cat $STARTUP_DIR/init.dat| \
	sed 's/^.\(:oracle:.*\/920\)/N\1/' > init.dat.tmp
fi
mv init.dat.tmp $STARTUP_DIR/init.dat 
rm -f init.dat.tmp > /dev/null 2>&1
##############################################################################
chmod 774 $STARTUP_DIR/start_stop
chmod 777 $STARTUP_DIR/init.dat
chown -R $MAIN_USER:$GROUP $STARTUP_DIR
DONE "The auto stratup/shutdown scripts for oracle (start_stop and init.dat files) were updated correctly" "ora_strstp.bash" 
