#!/bin/sh

# The environment variable $SRCHOME cannot be set during the installation
unset SRCHOME

# The environment variable $LD_LIBRARY_PATH should be cleared initially 
unset LD_LIBRARY_PATH
 
AWK=/bin/awk
SED=/bin/sed
UNAME=/bin/uname
GETCONF=/usr/bin/getconf
PSE='/bin/ps -e'
ISAINFO='/usr/bin/isainfo -k'
SUCCESS=0
ERROR=1
NULL='/dev/null'
ECHO=echo
GREP=grep
LOCALONLY=local_only
LOCAL=TRUE
_bootClassPath=""

BUNDLE=client

CMDDIR=`dirname $0`

if [ "$CMDDIR" = "." ]; then
    CMDDIR=`pwd`;
fi

SKIP_ROOTPRE_PROPERTY=FALSE
SKIP_ROOTPRE_PROPERTY_PASSED=FALSE
for arg in $@
do
          if [ `echo $arg | awk '/^oracle.install.skipRootPre=/'` ] ; then
		SKIP_ROOTPRE_PROPERTY_PASSED=TRUE
                val=`echo $arg | cut -d '=' -f 2`
                val=`echo $val | tr -s '[:upper:]' '[:lower:]'`
                if [ $val = "true" ]; then
                        SKIP_ROOTPRE_PROPERTY=TRUE
                fi
                break
          fi
done

# Replace relative path with fully qualified path.
if [ ! "`echo $CMDDIR|grep '^/'`" ]; then
    CMDDIR=`pwd`/$CMDDIR;
fi

if [ `$UNAME` = "HP-UX" ]; then
    unset SHLIB_PATH
fi
 
case `$UNAME` in
SunOS | HP-UX)
   OCR_LOC=/var/opt/oracle/ocr.loc
;;
*)
   OCR_LOC=/etc/oracle/ocr.loc
;;
esac

# Determine whether there is an existing CRS or not.
# Local is value of local_only in ocr.loc.
# False means there is an existing CRS installation on the system.
if [ -r $OCR_LOC ]; then
    LOCAL=`$AWK -F= /$LOCALONLY/'{print $2}' $OCR_LOC |\
           $SED 'y/trufalse/TRUFALSE/'`
fi

#
# Check if SunCluster is running
#
isSunClusterRunning()
{
  if `$PSE -u 0 | $GREP 'ucm[m]d' 1>$NULL 2>$NULL`
  then
    $ECHO $SUCCESS
  else
    $ECHO $ERROR
  fi
}

#
# Prompt for rootpre.sh For AIX
#
rootpreForAIX()
{
SHOW_ROOTPRE_PROMPT=FALSE
if [ "$BUNDLE" = "crs" -o "$BUNDLE" = "db" ]; then
	 # For AIX, we require rootpre.sh be run before installation. 
	 # we show the rootpre prompt in the following cases:
	 # 1. if the oracle.install.skipRootPre property is passed as false
	 # 2. If oracle.install.skipRootPre property is not passed, and SKIP_ROOTPRE is also not set in env
	 
	 if [ "$SKIP_ROOTPRE_PROPERTY_PASSED" = "TRUE" ] ; then
		if [ "$SKIP_ROOTPRE_PROPERTY" = "FALSE" ] ; then
			SHOW_ROOTPRE_PROMPT=TRUE
		fi 
	 else  # oracle.install.skipRootPre not passed from cmd line
	 # check if env variable SKIP_ROOTPRE is not set
   	 if [ ! "$SKIP_ROOTPRE" ]; then
		SHOW_ROOTPRE_PROMPT=TRUE
	 else 
      	  if [ "$SKIP_ROOTPRE" = "TRUE" ]; then
         	 echo "SKIP_ROOTPRE environment variable is set, which means "
          	echo "rootpre.sh was already run as root."
          else
               	echo "SKIP_ROOTPRE not set to TRUE."
               	exit 0;
	  fi
	 fi
	fi
	 if [ "$SHOW_ROOTPRE_PROMPT" = "TRUE" ] ; then
     		 echo "********************************************************************************"
     		 echo ""
     		 echo "Your platform requires the root user to perform certain pre-installation"
     		 echo "OS preparation.  The root user should run the shell script 'rootpre.sh' before"
      	 	 echo "you proceed with Oracle installation.  rootpre.sh can be found at the top level "
      		 echo "of the CD or the stage area."
     		 echo ""
     		 echo "Answer 'y' if root has run 'rootpre.sh' so you can proceed with Oracle "
     		 echo "installation."
     		 echo "Answer 'n' to abort installation and then ask root to run 'rootpre.sh'."
      		 echo ""
     		 echo "********************************************************************************"
      		 echo ""
      		 if [ "$BUNDLE" = "db" -a "$LOCAL" = "TRUE" ]; then
			 echo "Has 'rootpre.sh' been run by root? [y/n] (n)"
		 else
			 echo "Has 'rootpre.sh' been run by root on all nodes? [y/n] (n)"
		 fi
      		 read ans

      		 if [ $ans = 'y' -o $ans = "Y" ]; then
         		echo ""
     		 else
         		echo "Installation stopped to run 'rootpre.sh' by root."
        		exit 0
      		 fi
   	fi
fi
}


#
# Prompt for rootpre.sh For Solarisamd64
#
rootpreForSolarisamd64()
{
SHOW_ROOTPRE_PROMPT=FALSE
if [ "$BUNDLE" = "crs" ]; then
	 # For SOLARISAMD64, we require rootpre.sh for Vendor Cluster be run before installation
	 # we show the rootpre prompt in the following cases:
         # 1. if the oracle.install.skipRootPre property is passed as false
         # 2. If oracle.install.skipRootPre property is not passed, and SKIP_ROOTPRE is also not set in env

         if [ "$SKIP_ROOTPRE_PROPERTY_PASSED" = "TRUE" ] ; then
                if [ "$SKIP_ROOTPRE_PROPERTY" = "FALSE" ] ; then
                        SHOW_ROOTPRE_PROMPT=TRUE
                fi
         else  # oracle.install.skipRootPre not passed from cmd line
         # check if env variable SKIP_ROOTPRE is not set
         if [ ! "$SKIP_ROOTPRE" ]; then
                SHOW_ROOTPRE_PROMPT=TRUE
         else
          if [ "$SKIP_ROOTPRE" = "TRUE" ]; then
                 echo "SKIP_ROOTPRE environment variable is set, which means "
                echo "rootpre.sh was already run as root."
          else
                echo "SKIP_ROOTPRE not set to TRUE."
                exit 0;
          fi
         fi
	fi

	 if [ "$SHOW_ROOTPRE_PROMPT" = "TRUE" ] ; then
		 echo "********************************************************************************"
                 echo ""
                 echo "Your platform requires the root user to perform certain pre-installation"
                 echo "OS preparation.  The root user should run the shell script 'rootpre.sh' before"
                 echo "you proceed with Oracle installation.  rootpre.sh can be found at the top level "
                 echo "of the CD or the stage area."
                 echo ""
                 echo "Answer 'y' if root has run 'rootpre.sh' so you can proceed with Oracle "
                 echo "installation."
                 echo "Answer 'n' to abort installation and then ask root to run 'rootpre.sh'."
                 echo ""
                 echo "********************************************************************************"
                 echo ""
                 echo "Has 'rootpre.sh' been run by root? [y/n] (n)"
                 read ans

                 if [ $ans = 'y' -o $ans = "Y" ]; then
                        echo ""
                 else
                        echo "Installation stopped to run 'rootpre.sh' by root."
                        exit 0
                 fi
        fi
fi
}

   #Checking if the commandline argument passed to runInstaller contains "-help"
   for HELP in $@
   do
          if [ $HELP = "-help" ];then
                HELPENABLED=TRUE
                break
          fi
   done

if [ `$UNAME -m` = "s390x" ]; then
   OSLEVEL=/bin/oslevel
   UNAME=/bin/uname
   CUT=/bin/cut
   _bootClassPath="-J-Xbootclasspath/a:$CMDDIR/stage/ext/jlib/emocmclnt.jar:$CMDDIR/stage/ext/jlib/jsse.jar:$CMDDIR/stage/ext/jlib/emocmclnt-14.jar:$CMDDIR/stage/ext/jlib/emocmcommon.jar:$CMDDIR/stage/ext/jlib/http_client.jar:$CMDDIR/stage/ext/jlib/jcert.jar:$CMDDIR/stage/ext/jlib/jnet.jar:$CMDDIR/stage/ext/jlib/log4j-core.jar:$CMDDIR/stage/ext/jlib/osdt_core3.jar:$CMDDIR/stage/ext/jlib/osdt_jce.jar:$CMDDIR/stage/ext/jlib/regexp.jar:$CMDDIR/stage/ext/jlib/xmlparserv2.jar"

fi

	
if [ `$UNAME` = "AIX" ]; then
   OSLEVEL=/bin/oslevel
   UNAME=/bin/uname
   CUT=/bin/cut
   _bootClassPath="-J-Xbootclasspath/a:$CMDDIR/stage/ext/jlib/emocmclnt.jar:$CMDDIR/stage/ext/jlib/jsse.jar:$CMDDIR/stage/ext/jlib/emocmclnt-14.jar:$CMDDIR/stage/ext/jlib/emocmcommon.jar:$CMDDIR/stage/ext/jlib/http_client.jar:$CMDDIR/stage/ext/jlib/jcert.jar:$CMDDIR/stage/ext/jlib/jnet.jar:$CMDDIR/stage/ext/jlib/log4j-core.jar:$CMDDIR/stage/ext/jlib/osdt_core3.jar:$CMDDIR/stage/ext/jlib/osdt_jce.jar:$CMDDIR/stage/ext/jlib/regexp.jar:$CMDDIR/stage/ext/jlib/xmlparserv2.jar"

   if [ "ORCL$IGNOREOSLEVEL" = "ORCL" ]; then
        MAIN_VERSION=`$OSLEVEL -r | $CUT -d"-"  -f1`;
        MinorVersion1=`$OSLEVEL -r | $CUT -d"-"  -f2`;

        if [ $MAIN_VERSION -lt "5300" ]; then
                echo "\nrunInstaller needs minimum oslevel : "5300-05-06"\n";
                exit 0;
        fi

        if [ $MAIN_VERSION -eq "5300" ]; then
                if [ MinorVersion1 -lt "05" ]; then
                        echo "\nrunInstaller needs minimum oslevel : "5300-05-06"\n";
                        exit 0;
                else
                        if [ $MinorVersion1 -eq "05" ]; then
                                MinorVersion2=`$OSLEVEL -s | $CUT -d"-"  -f3`;
				if [ $MinorVersion2 != "CSP" ]; then
	                                if [ $MinorVersion2 -lt "06" ]; then
        	                                echo "\nrunInstaller needs minimum oslevel : "5300-05-06"\n";
                	                        exit 0;
                        	        fi
				fi
	                fi
        	fi
	fi
   fi
   
   if [ ! "$HELPENABLED" ] ; then
        rootpreForAIX
   fi
  
fi

if [ `$UNAME` = "SunOS" ];then
   if [ `$ISAINFO` = "amd64" -a `isSunClusterRunning` = "${SUCCESS}" ];then
	if [ ! "$HELPENABLED" ] ;then
                rootpreForSolarisamd64
        fi
   fi

   #Start Fix for bug 16406820 - Pass the XToolkit as system property to suppress the warning messages on Solaris

   systemProperty="-J-Dawt.toolkit=sun.awt.X11.XToolkit"

   #End Fix for bug 16406820
fi

SILENT=FALSE

if [ x${PATH} != x ]; then
    PATH=/usr/bin:/usr/ccs/bin:$PATH:$CMDDIR/install
    export PATH 
else
    PATH=/usr/bin:/usr/ccs/bin:$CMDDIR/install
    export PATH
fi

CWDDIR=`pwd`;
case "$BUNDLE" in
db)
# One-click: only if there is no cmd line argument and it's not on CRS node
if [ $# -gt 0 -o "$LOCAL" = "FALSE" -o \
     ! -r $CMDDIR/install/oneclick.properties ]; then
    $CMDDIR/install/.oui $* -J-Doracle.install.setup.workDir=$CWDDIR  $systemProperty $_bootClassPath
else
    $CMDDIR/install/.oui -J-Doracle.install.setup.workDir=$CWDDIR $systemProperty $_bootClassPath
fi
;;
client)
    $CMDDIR/install/.oui $* CLUSTER_NODES={} -J-Doracle.install.setup.workDir=$CWDDIR $systemProperty
;;
crs)
    $CMDDIR/install/.oui $* -formCluster -J-Doracle.install.setup.workDir=$CWDDIR $systemProperty
;;
dv)
    $CMDDIR/install/.oui $* -J-Doracle.install.setup.workDir=$CWDDIR $systemProperty

;;
*)
    $CMDDIR/install/.oui $* $systemProperty
;;
esac
