#!/bin/sh
# @(#) $Id: //bas/722_STACK/src/include/oraroot.sh#1 $ SAP
#***********************************************************************
#								       *
# SAP AG Walldorf						       *
# Systeme, Anwendungen und Produkte in der Datenverarbeitung	       *
#								       *
# (C) Copyright SAP AG, 1994					       *
#								       *
# oraroot.sh							       *
#								       *
# Run some Oracle specific commands that have to be done as root       *
#								       *
# Usage:							       *
#   oraroot.sh <SAPSID> [<DBSID>|oracle]			       *
#   oraroot.sh C11						       *
#								       *
#======================================================================*

# AIX wants this for tr
LANG=C

if [ $# -ne 1 -a $# -ne 2 ]; then
    echo
    echo "Bad number of arguments!"
    echo
    echo "Call $0 <SAPSID> [<DBSID>|oracle]"
    echo "  e.g. $0 C11"
    echo "  <DBSID> must be specified only on MCOD systems or if it differs from <SAPSID>"
    echo "  'oracle' must be specified for Oracle 11g (or higher) ASM and RAC databases"
    echo
    exit 1
fi

SAPSID=`echo $1 | tr "[a-z]" "[A-Z]"`
SAPSIDL=`echo $1 | tr "[A-Z]" "[a-z]"`

DBSID=$SAPSID
if [ -n "$2" ]; then
    DBSID=`echo $2 | tr "[a-z]" "[A-Z]"`
fi
DBSIDL=`echo $DBSID | tr "[A-Z]" "[a-z]"`

if [ `whoami` != root ]; then
    echo
    echo "You have to be root to run this script!"
    echo
    exit 1
fi

if [ ! -d /usr/sap/${SAPSID} ]; then
    echo
    echo "There's no SAP-System $SAPSID installed!"
    echo
    exit 1
fi

# if the BR*Tools are there, chown and chmod of the files
# process all platforms supported by SAP on Oracle

PLATLST1="hp_64 hpia64 linuxia64 linuxintel linuxppc64 linuxx86_64 rs6000_64 sun_64 sunx86_64"
for p in $PLATLST1
do
# If file brarchive exists, system must be running Oracle. Execute block.
  if [ -f /usr/sap/${SAPSID}/SYS/exe/nuc/$p/brarchive ]; then
    if [ $DBSIDL != oracle ]; then
       FILELST1="brarchive brbackup brconnect"
    else
       FILELST1="brarchive brbackup brconnect brrecover brrestore brspace"
    fi
    for f in $FILELST1
    do
       if [ -f /usr/sap/${SAPSID}/SYS/exe/nuc/$p/$f ]; then
	  echo "Preparing /usr/sap/${SAPSID}/SYS/exe/nuc/$p/$f ..."
	  if [ $DBSIDL != oracle ]; then
	     chown ora${DBSIDL} /usr/sap/${SAPSID}/SYS/exe/nuc/$p/$f
	     if [ $? -ne 0 ]; then
		echo "   Error occurred by chown file /usr/sap/${SAPSID}/SYS/exe/nuc/$p/$f!"
		echo "   Please check what's the matter"
	     fi
	     chmod 4774 /usr/sap/${SAPSID}/SYS/exe/nuc/$p/$f
	     if [ $? -ne 0 ]; then
		echo "   Error occurred by chmod file /usr/sap/${SAPSID}/SYS/exe/nuc/$p/$f!"
		echo "   Please check what's the matter"
	     fi
	  else
	     chown oracle /usr/sap/${SAPSID}/SYS/exe/nuc/$p/$f
	     if [ $? -ne 0 ]; then
		echo "   Error occurred by chown file /usr/sap/${SAPSID}/SYS/exe/nuc/$p/$f!"
		echo "   Please check what's the matter"
	     fi
	     chgrp oinstall /usr/sap/${SAPSID}/SYS/exe/nuc/$p/$f
	     if [ $? -ne 0 ]; then
		echo "   Error occurred by chgrp file /usr/sap/${SAPSID}/SYS/exe/nuc/$p/$f!"
		echo "   Please check what's the matter"
	     fi
	     chmod 6774 /usr/sap/${SAPSID}/SYS/exe/nuc/$p/$f
	     if [ $? -ne 0 ]; then
		echo "   Error occurred by chmod file /usr/sap/${SAPSID}/SYS/exe/nuc/$p/$f!"
		echo "   Please check what's the matter"
	     fi
	  fi
       fi
    done
  fi
  if [ -f /usr/sap/${SAPSID}/SYS/exe/uc/$p/brarchive ]; then
    if [ $DBSIDL != oracle ]; then
       FILELST1="brarchive brbackup brconnect"
    else
       FILELST1="brarchive brbackup brconnect brrecover brrestore brspace"
    fi
    for f in $FILELST1
    do
       if [ -f /usr/sap/${SAPSID}/SYS/exe/uc/$p/$f ]; then
	  echo "Preparing /usr/sap/${SAPSID}/SYS/exe/uc/$p/$f ..."
	  if [ $DBSIDL != oracle ]; then
	     chown ora${DBSIDL} /usr/sap/${SAPSID}/SYS/exe/uc/$p/$f
	     if [ $? -ne 0 ]; then
		echo "   Error occurred by chown file /usr/sap/${SAPSID}/SYS/exe/uc/$p/$f!"
		echo "   Please check what's the matter"
	     fi
	     chmod 4774 /usr/sap/${SAPSID}/SYS/exe/uc/$p/$f
	     if [ $? -ne 0 ]; then
		echo "   Error occurred by chmod file /usr/sap/${SAPSID}/SYS/exe/uc/$p/$f!"
		echo "   Please check what's the matter"
	     fi
	  else
	     chown oracle /usr/sap/${SAPSID}/SYS/exe/uc/$p/$f
	     if [ $? -ne 0 ]; then
		echo "   Error occurred by chown file /usr/sap/${SAPSID}/SYS/exe/uc/$p/$f!"
		echo "   Please check what's the matter"
	     fi
	     chgrp oinstall /usr/sap/${SAPSID}/SYS/exe/uc/$p/$f
	     if [ $? -ne 0 ]; then
		echo "   Error occurred by chgrp file /usr/sap/${SAPSID}/SYS/exe/uc/$p/$f!"
		echo "   Please check what's the matter"
	     fi
	     chmod 6774 /usr/sap/${SAPSID}/SYS/exe/uc/$p/$f
	     if [ $? -ne 0 ]; then
		echo "   Error occurred by chmod file /usr/sap/${SAPSID}/SYS/exe/uc/$p/$f!"
		echo "   Please check what's the matter"
	     fi
	  fi
       fi
    done
  fi
done
if [ -f /usr/sap/${SAPSID}/SYS/exe/run/brarchive ]; then
  if [ $DBSIDL != oracle ]; then
     FILELST1="brarchive brbackup brconnect"
  else
     FILELST1="brarchive brbackup brconnect brrecover brrestore brspace"
  fi
  for f in $FILELST1
  do
     if [ -f /usr/sap/${SAPSID}/SYS/exe/run/$f ]; then
	echo "Preparing /usr/sap/${SAPSID}/SYS/exe/run/$f ..."
	if [ $DBSIDL != oracle ]; then
	   chown ora${DBSIDL} /usr/sap/${SAPSID}/SYS/exe/run/$f
	   if [ $? -ne 0 ]; then
	      echo "   Error occurred by chown file /usr/sap/${SAPSID}/SYS/exe/run/$f!"
	      echo "   Please check what's the matter"
	   fi
	   chmod 4774 /usr/sap/${SAPSID}/SYS/exe/run/$f
	   if [ $? -ne 0 ]; then
	      echo "   Error occurred by chmod file /usr/sap/${SAPSID}/SYS/exe/run/$f!"
	      echo "   Please check what's the matter"
	   fi
	else
	   chown oracle /usr/sap/${SAPSID}/SYS/exe/run/$f
	   if [ $? -ne 0 ]; then
	      echo "   Error occurred by chown file /usr/sap/${SAPSID}/SYS/exe/run/$f!"
	      echo "   Please check what's the matter"
	   fi
	   chgrp oinstall /usr/sap/${SAPSID}/SYS/exe/run/$f
	   if [ $? -ne 0 ]; then
	      echo "   Error occurred by chgrp file /usr/sap/${SAPSID}/SYS/exe/run/$f!"
	      echo "   Please check what's the matter"
	   fi
	   chmod 6774 /usr/sap/${SAPSID}/SYS/exe/run/$f
	   if [ $? -ne 0 ]; then
	      echo "   Error occurred by chmod file /usr/sap/${SAPSID}/SYS/exe/run/$f!"
	      echo "   Please check what's the matter"
	   fi
	fi
     fi
  done
fi

echo "done"
exit 0
