#!/bin/sh

ORACLE_HOME=/oracle/EPP/11203
CHOWN=/bin/chown
CHMOD=/bin/chmod
RM=/bin/rm
AWK=/bin/awk
ECHO=/bin/echo
CP=/bin/cp

#
# check for zero UID
#
RUID=`/usr/bin/id|$AWK -F\( '{print $1}'|$AWK -F= '{print $2}'`
if [ $RUID -ne 0 ];then
  $ECHO "You must be logged in as user with UID as zero (e.g. root user) to run root.sh."
  $ECHO "Log in as user with UID as zero (e.g. root user) and restart root.sh execution."
  exit 1
fi

if [ -f $ORACLE_HOME/bin/oradism ]; then
	$CHOWN root $ORACLE_HOME/bin/oradism
	$CHMOD 4750 $ORACLE_HOME/bin/oradism
fi
# remove backup copy
if [ -f $ORACLE_HOME/bin/oradism.old ]; then
	$RM -f $ORACLE_HOME/bin/oradism.old
fi
# copy extjobo to extjob if it doesn't exist
if [ ! -f $ORACLE_HOME/bin/extjob -a -f $ORACLE_HOME/bin/extjobo ]; then
	$CP -p $ORACLE_HOME/bin/extjobo $ORACLE_HOME/bin/extjob
fi
if [ -f $ORACLE_HOME/bin/extjob ]; then
	$CHOWN root $ORACLE_HOME/bin/extjob
	$CHMOD 4750 $ORACLE_HOME/bin/extjob
fi
if [ -f $ORACLE_HOME/rdbms/admin/externaljob.ora ]; then
	$CHOWN root $ORACLE_HOME/rdbms/admin/externaljob.ora
	$CHMOD 640 $ORACLE_HOME/rdbms/admin/externaljob.ora
fi
# properly setup job scheduler switch user executable
if [ -f $ORACLE_HOME/bin/jssu ]; then
        $CHOWN root $ORACLE_HOME/bin/jssu
        $CHMOD 4750 $ORACLE_HOME/bin/jssu
fi
# remove backup copy
if [ -f $ORACLE_HOME/rdbms/admin/externaljob.ora.orig ]; then
	$RM -f $ORACLE_HOME/rdbms/admin/externaljob.ora.orig
fi

# tighten permissions on $ORACLE_HOME/scheduler/wallet
$CHMOD 0700 $ORACLE_HOME/scheduler/wallet

# Add a large number to file-max kernel param for memory_target on Linux
OSINFO=`/bin/uname -a`
SYSCTL=/sbin/sysctl
FSMAXFILE=/proc/sys/fs/file-max
FSMVAL=""
SYSCTLCONF=/etc/sysctl.conf
AWK=/bin/awk
SED=/bin/sed
case $OSINFO in
Linux*)
 # Read value from /etc/sysctl.conf
 if [ -w $SYSCTLCONF ]; then
  while read line
  do
  case ${line} in
    fs.file-max*)
      # Value exists in /etc/sysctl.conf. Strip space/tabs and store its value.
      FSMVAL=`$ECHO ${line} | $SED 's/fs.file-max\s*=\s*//g'`
    ;;
   esac
  done < $SYSCTLCONF
 fi
 
 # Update the filemax for current usage
 # bug 9797468: change value from 6.4M to 6815744 (due to validated configs)
 if [ -z $FSMVAL ] || [ $FSMVAL -lt 6815744 ]; then
   # Value is less than our 6815744, update it
   $SYSCTL -q -w fs.file-max=6815744
   if [ -f $FSMAXFILE ]; then
    $ECHO 6815744 > $FSMAXFILE
   fi
 fi
  
 # Update the filemax across machine reboots by persisting to /etc/sysctl.conf
 if [ $FSMVAL ]; then
    # Value exists, but less than our requirement. So overwrite it.
    if [ $FSMVAL -lt 6815744 ]; then
     # Persist it to /etc/sysctl.conf using sed
     # Take into account tabs/spaces - bug 9462640
     if [ -f $SED ]; then
        $SED -i.bak -e "s/^fs.file-max\s*=.*/fs.file-max = 6815744/" $SYSCTLCONF 
     fi
    fi
 else
    # Value does not exist, so append our required value.
    FSMVAL="fs.file-max = 6815744"
    $ECHO $FSMVAL >> $SYSCTLCONF
 fi

;;
esac
