#!/bin/sh
#
# $Header: rthd.sh.pp 3090000.1 95/09/05 15:16:46 wyim Generic<base> $ Copyr (c) 1992 Oracle
#
#
#  root.sh
#
# This script is intended to be run by root. The script contains
# all the product installation actions that require root privileges.
#
# IMPORTANT NOTES - READ BEFORE RUNNING SCRIPT:
#
# (1) ORACLE_HOME, ORACLE_OWNER, and ORACLE_SID can be defined in user's
#     environment to override default values defined in this script.
#
# (2) The environment variable LBIN (defined within the script) points to
#     the default local bin area. Three executables will be moved there as
#     part of this script execution
#
# (3) Define (if desired) LOG variable to name of log file
#
# (4) TWO_TASK must be defined if running script for remote database
#
#----------------------------------------------------------------------
# IT IS ASSUMED THAT THE VERSION NUMBER IN THE FOLLOWING STRING WILL
# TRACK THE VERSION NUMBER OF THIS FILE.
#----------------------------------------------------------------------
rev_num='$Revision: 3090000.1 $'
#

#  This is the default value for CHOWN
#  It will redefined later in this script for those ports
#  which have it conditionally defined in ss_install.h
CHOWN=/bin/chown

#
# Define variables to be used in this script
#

#  ORACLE_SERVER is an environment variable used in low-end ORACLE Server
#  packages to determine whether it is installing as a Server-only system.

if [ "$TWO_TASK" != "" -o "$REMOTE_DB" = "T" ]; then
    REMOTE_DB=T
fi
export REMOTE_DB

# oratab location

ORATAB=/etc/oratab

#  Default local bin directory.

LBIN=/usr/lbin

# chown
# OBSOLETE: CHOWN should be set in ss_install.h

# SunOS 5 (Solaris 2.x) installation does not use a compiler.
# cc

CC="cc"

#
#  Display abort message on interrupt.
#

trap 'echo "Oracle8 root.sh execution aborted!";exit' 1 2 3 15

#
#  Allow verification/trace to be turned on and off.
#
case $ORACLE_TRACE in
    T) set -x ;;
esac

#
#  If LOG is not set, then send output to /dev/null.
#
case $LOG in
    "") LOG=/dev/null ;;
esac

# Enter log message
echo "
Running Oracle8 root.sh script..." | tee -a $LOG

#
# Determine how to suppress newline with echo command.
#
case ${N}$C in
    "") if echo "\c" | grep c >/dev/null 2>&1; then
     N='-n'
 else
     C='\c'
 fi ;;
esac

#
# Default values
#
ORACLE_HOME=${ORACLE_HOME:=/home/oracle/8.0.5}
ORACLE_SID=${ORACLE_SID:=test}
ORACLE_OWNER=${ORACLE_OWNER:=oracle}
INSTALL_LOG=/home/oracle/8.0.5/orainst/install.log
SQL_LOG=/home/oracle/8.0.5/orainst/sql.log
MAKE_LOG=/home/oracle/8.0.5/orainst/make.log
OS_LOG=/home/oracle/8.0.5/orainst/os.log

#
#  Check to make sure that required variables are set. If
#  they are not, prompt user to set them and restart install.
#

case $ORACLE_HOME in
    "") echo "ORACLE_HOME is not set."
 echo "Set and export ORACLE_HOME, then restart root.sh execution."
 exit 1 ;;
esac

case $ORACLE_OWNER in
    "") echo "ORACLE_OWNER is not set."
 echo "Set and export ORACLE_OWNER, then restart root.sh execution."
 exit 1 ;;
esac

case $INSTALL_HOME in
    "") INSTALL_HOME="$ORACLE_HOME/orainst"
 export INSTALL_HOME ;;
esac

#
# Last chance for the user to confirm these values.
#
echo ""
echo "The following environment variables are set as:"
echo "    ORACLE_OWNER= $ORACLE_OWNER"
echo "    ORACLE_HOME=  $ORACLE_HOME"
echo "    ORACLE_SID=   $ORACLE_SID"
echo $N "Are these settings correct (Y/N)? $C"
DEFLT="Y"; . $INSTALL_HOME/read.sh; ANSWER=$RDVAR
case $ANSWER in
    Y|y) ;;
    *)  echo "Exiting ORACLE ROOT install script.  Please set these environment variables to the desired values"
  exit 0 ;;
esac

#
#  Make sure effective uid is $ROOT.
#

> /tmp/fil$$
INAME=`/bin/ls -l /tmp/fil$$ | awk '{print $3}'`
rm -f /tmp/fil$$
case "$INAME" in
    root) ;;
    *) echo "You must be logged in as root to run root.sh."
 echo "Exiting install procedure."
 exit 1 ;;
esac

if [ "$ORACLE_SERVER" = "T" ]; then
#
#  Make sure /etc/services is there
#
   SERVFILE=/etc/services
   if [ ! -f $SERVFILE ]; then
       echo "orasrv    1525/tcp        oracle" > $SERVFILE
   elif grep orasrv $SERVFILE > /dev/null 2>&1; then :
   else
      chmod u+w $SERVFILE
      echo "orasrv    1525/tcp        oracle" >> $SERVFILE
   fi # [ ! -f $SERVFILE ]
   if grep tcptlisrv $SERVFILE > /dev/null 2>&1; then :
   else
       chmod u+w $SERVFILE
       echo "tcptlisrv    1527/tcp        oracle" >> $SERVFILE
   fi # grep tcptlisrv $SERVFILE

fi #  [ORACLE_SERVER == T]

#
#  Get name of Local Bin directory.
#

if [ "$ORACLE_SERVER" != "T" ]; then
   echo ""
   echo $N "Enter the full pathname of the local bin directory $C"
   DEFLT=${LBIN}; . $INSTALL_HOME/read.sh; LBIN=$RDVAR
fi

#
# Check for oracle uid in /etc/passwd; retrieve home directory & group id
#
echo ""
echo "Checking for \"$ORACLE_OWNER\" user id..." | tee -a $LOG

PW_ENTRY=`grep "^${ORACLE_OWNER}:" /etc/passwd 2> /dev/null`

case $PW_ENTRY in

    "") PW_ENTRY=`(ypmatch $ORACLE_OWNER passwd.byname) 2>/dev/null` ;;
esac
case $PW_ENTRY in
    "") PW_ENTRY=`(nismatch $ORACLE_OWNER passwd.org_dir) 2>/dev/null` ;;
esac
case $PW_ENTRY in
    "") echo "Cannot find password file entry for \"$ORACLE_OWNER\" user id."
 echo "Exiting install script."
 exit 1 ;;
esac

ORACLE_OWNER_HOME=`echo $PW_ENTRY | awk -F: '{print $6}'`
GID=`echo $PW_ENTRY | awk -F: '{print $4}'`

#
#  Compare oracle user's home dir. to ORACLE_HOME.  Move to $ORACLE_HOME.
#

case $ORACLE_OWNER_HOME in
    $ORACLE_HOME) ;;
    *) echo "ORACLE_HOME does not match the home directory for $ORACLE_OWNER."
     echo $N "Okay to continue? $C"

     DEFLT="N"; . $INSTALL_HOME/read.sh; ANSWER=$RDVAR
 case $ANSWER in
     Y|y) ;;
     *) echo "Exiting ORACLE ROOT install script."
  exit 0 ;;
 esac ;;
esac
if [ ! -d $ORACLE_HOME ]; then
    echo "The ORACLE home directory \"$ORACLE_HOME\" does not exist."
    echo "Exiting install script."
    exit 1
fi

#
# Check existence of LBIN directory
#

#  Create local bin directory.
#
if [ ! -d $LBIN ] ; then
    echo "Directory $LBIN does not exist. Abort.  Please create $LBIN"
    exit 1;
fi

#
#  Make sure an oratab file exists on this system
#

oratab_exists=true
if [ "$REMOTE_DB" != "T" ]; then
    if [ ! -s ${ORATAB} ] ; then
     echo ""
     echo "Creating ${ORATAB} file..." | tee -a $LOG

     cat <<!>> ${ORATAB}
#
#  This file is used by ORACLE utilities.  It is created by root.sh
#  and updated by the Oracle8 and SQL*Net install procedures.
#
#  A colon, ':', is used as the field terminator.  A new line terminates
#  the entry.  Lines beginning with a pound sign, '#', are comments.
#
#  Entries are of the form:
#      \$ORACLE_SID:\$ORACLE_HOME:<N|Y>:
#
#  The first and second fields are the system identifier and home
#  directory of the database respectively.  The third field indicates
#  to the dbstart utility that the database should, "Y", or should not,
#  "N", be brought up at system boot time.
#
#  Multiple entries with the same \$ORACLE_SID are not allowed.
#
#
!
 $CHOWN $ORACLE_OWNER ${ORATAB}
 chmod 664 ${ORATAB}
    fi # [ ! -s ${ORATAB} ]

#
#  Make an entry in oratab file for this dbs.
#

#

    TMPORATB=/tmp/oratab$$
    case "$ORACLE_SID" in
     "") SID='*' ;;
     *) SID=$ORACLE_SID ;;
    esac
    # If an entry exists in $ORATAB for this $SID and this $ORACLE_HOME, don't
    # do anything.
    FOUND=`grep "^${SID}:${ORACLE_HOME}:" ${ORATAB}`
    if [ -z "${FOUND}" ]; then
 
       # if there is a old entry with no sid and same oracle home, that entry
       # will be marked as a comment.
       FOUND_OLD=`grep "^*:${ORACLE_HOME}:" ${ORATAB}`
       if [ -n "${FOUND_OLD}" ]; then

           # Here we are assuming there is no oracle home name which contain '?'
           sed -e "s?^*:$ORACLE_HOME:?# *:$ORACLE_HOME:?" $ORATAB > $TMPORATB
           cat $TMPORATB > $ORATAB
           rm -f $TMPORATB 2>/dev/null
       fi

       FOUND=`grep "^$SID:" ${ORATAB}`
       case "$FOUND" in
       "") echo "Updating ${ORATAB} file..." | tee -a $LOG
                echo "" ;;
        *) echo "Replacing existing entry for SID in ${ORATAB}..." | tee -a $LOG
         # Changing the SID field to *.
         sed -e "s/^$SID:/*:/" $ORATAB > $TMPORATB
  cat $TMPORATB > $ORATAB
            echo "The previous entry is now in a comment."
     echo ""
  rm -f $TMPORATB 2>/dev/null
  ;;
        esac

       cat <<!>> ${ORATAB}
$SID:$ORACLE_HOME:N
!
    fi # [ -z "${FOUND}" ]

fi
# above fi matches if $REMOTE_DB

OSHMAKEF=""

#
# Move files to LBIN
#

cd $ORACLE_HOME/bin
if [ "$REMOTE_DB" != "T" ]; then
    chmod 755 dbhome oraenv coraenv
    cp dbhome oraenv coraenv $LBIN
    chmod a+w oraenv coraenv # allow later net.installs to write new *env
    $CHOWN $ORACLE_OWNER $LBIN/dbhome $LBIN/oraenv $LBIN/coraenv
fi # [ "$REMOTE_DB" != "T" ]

if [ "$REMOTE_DB" = "T" ]; then
    ORAENV_STR="TWO_TASK=$TWO_TASK; export TWO_TASK"
    CORAENV_STR="setenv TWO_TASK $TWO_TASK"
else
    ORAENV_STR="ORAENV_ASK=NO; . oraenv; ORAENV_ASK="
    CORAENV_STR="set ORAENV_ASK=NO; source $ORACLE_HOME/bin/coraenv; unset ORAENV_ASK"
fi

if [ "$ORACLE_SERVER" != "T" ]; then
   echo "
Leaving common section of Oracle8 root.sh.
"
fi

# Remove the duplicate copy of libclntsh.so
rm -f $ORACLE_HOME/lib/libclntsh.so
cd $ORACLE_HOME/lib;
ln -s libclntsh.so.1.0 libclntsh.so

sync;sync;sync

# Change permissions on tnslsnr to be setuid to root.
# This is required for SPX/IPX V2 on UnixWare.

cd $ORACLE_HOME/bin

if [ -x $ORACLE_HOME/bin/tnslsnr ] ; then
   $CHOWN root  $ORACLE_HOME/bin/tnslsnr   # make tnslsnr set-uid root
   chmod 4750 $ORACLE_HOME/bin/tnslsnr
fi
if [ -x $ORACLE_HOME/bin/ntisbsdm ]; then
   $CHOWN root  $ORACLE_HOME/bin/ntisbsdm
   chmod 4750 $ORACLE_HOME/bin/ntisbsdm
fi

if [ -x $ORACLE_HOME/bin/tnsmon ]; then
   $CHOWN root  $ORACLE_HOME/bin/tnsmon
   chmod 4750 $ORACLE_HOME/bin/tnsmon
fi

# Make install log read-only for the Oracle owner.
if [ -f "$INSTALL_LOG" ]; then
   chmod 600 $INSTALL_LOG
fi

# Make oracle executable setuid.
if [ -x $ORACLE_HOME/bin/oracle ]; then
   chmod 4755 $ORACLE_HOME/bin/oracle
fi

chown root /home/oracle/8.0.5/bin/osh
chmod u+s /home/oracle/8.0.5/bin/osh
chown root /home/oracle/8.0.5/bin/dbsnmp
chmod u+s /home/oracle/8.0.5/bin/dbsnmp
chown root /home/oracle/8.0.5/bin/oratclsh
chmod u+s /home/oracle/8.0.5/bin/oratclsh