#!/bin/bash

. ./param_bash.ini

clear
START "Creating auto stratup/shutdown scripts(start_stop and init.dat files)" "$0"
if [ ! -w /etc/passwd ] && [ "$USER" != "$MAIN_USER" ] ; then
	ERROR "Run the script as $MAIN_USER user or root user" "$0"
fi

MainUserExist
if [ "$?" = "1" ] ; then 
	ERROR "$MAIN_USER user does not exist" "$0"
fi

if [ "$MODE" = "0" ] ; then
	echo -n "Enter a slot number ${SOFT_DIR}[$SLOT_NUMBER] : "
	read ans
	if [ "$ans" != "" ] ; then	
		SLOT_NUMBER=$ans	
	fi 	
fi
if [ ! -d $APP_HOME/$SOFT_DIR$SLOT_NUMBER ] ; then
	ERROR "The directory ${APP_HOME}/${SOFT_DIR}$SLOT_NUMBER does not exist on the server" "$0"
fi
if [ ! -d $STARTUP_DIR ] ; then
	ERROR "The directory $STARTUP_DIR does not exist on the server" "$0"
fi
	
m_out=$SOFT_DIR$SLOT_NUMBER
u_name=$MAIN_USER

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"
HTTPD_PORT=$PORT_PRE$SLOT_NUMBER


if [ "$HTTPD_PORT" -gt  1024 ] ; then
   apache_owner=$u_name
else
   apache_owner=root
fi

if [ "$u_name" = "dps" ] ; then
   _alephe_dev=$APP_HOME/$SOFT_DIR$SLOT_NUMBER
fi

apache_string_httpd="Y:apache:$apache_owner:httpd:$_alephe_dev:../$SOFT_DIR$SLOT_NUMBER:apachectl_auto:apachectl"
exlibris_string="Y:dps:$u_name:$_alephe_dev:dps_startup_all:dps_shutdown_all"
fle=0
fla=0
flo=0
fls=0
flt=none

grep "$exlibris_string" $STARTUP_DIR/init.dat > /dev/null 2>&1
STATUS=$?
if [ "$STATUS" = "0" ] ; then
   fle=1
fi
grep "$ORACLE_HOME" $STARTUP_DIR/init.dat > /dev/null 2>&1
STATUS=$?
if [ "$STATUS" = "0" ] ; then
   flo=1
fi
grep "$apache_string_httpd" $STARTUP_DIR/init.dat  > /dev/null 2>&1
STATUS=$?
if [ "$STATUS" = "0" ] ; then
   fla=1
fi
grep "$apache_string_https" $STARTUP_DIR/init.dat  > /dev/null 2>&1
STATUS=$?
if [ "$STATUS" = "0" ] ; then
   fls=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 [ "$s1" = "#" -a "$s2" = "dtl" -o "$s2" = "metalib" -o "$s2" = "aleph" -o "$s2" = "ill" -o "$s2" = "dps" ] ; then
		flt="exlibris"
	fi
	if [ "$s1" = "#" -a "$s2" = "apache" ] ; then
		flt="apache"
	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
         		 ;;
          		exlibris)
            			if [ "$fle" = "0" ] ; then
               				echo $exlibris_string >> $FILE_OUT
            			else
               				echo "The following string was found in $STARTUP_DIR/init.dat:"
               				echo $exlibris_string
            			fi
          		;;
          		apache)
            			if [ "$fla" = "0" ] ; then
               				echo $apache_string_httpd >> $FILE_OUT
            			else
               				echo "The following string was found in $STARTUP_DIR/init.dat:"
               				echo $apache_string_httpd
            			fi
            			if [ "$flag_ssl" = "Y" -o "$flag_ssl" = "y" ]; then
              				if [ "$fls" = "0" ] ; then
               					echo $apache_string_https >> $FILE_OUT
              				else
               					echo "The following string was found in $STARTUP_DIR/init.dat:"
               					echo $apache_string_https
              				fi
            			fi
          		;;
        	esac
        	flt=none
  	fi 
	if [ "$LINE" = "" ] ; then
     		echo "" >> $FILE_OUT
  	fi
done

mv $STARTUP_DIR/init.dat $STARTUP_DIR/init.dat.old
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
##############################################################################
if [ "${INST_TYPE}" = "SW_ONLY" ] ; then
	cat $STARTUP_DIR/init.dat | \
        sed 's/^.\(:oracle:.*\/920\)/N\1/' > init.dat.tmp
        mv init.dat.tmp $STARTUP_DIR/init.dat 
        cat $STARTUP_DIR/init.dat | \
        sed 's/^.\(:oracle:.*\/102\)/N\1/' > init.dat.tmp
        mv init.dat.tmp $STARTUP_DIR/init.dat 
        cat $STARTUP_DIR/init.dat | \
        sed 's/^.\(:oracle:.*\/11\)/N\1/' > init.dat.tmp
        mv init.dat.tmp $STARTUP_DIR/init.dat 
        rm -f init.dat.tmp > /dev/null 2>&1
fi
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(start_stop and init.dat files) were updated correctly" "$0" 
