#!/bin/sh
#
# $Header: install/tools/deinstall/deinstall /unix/24 2009/06/23 03:19:44 prsubram Exp $
#
# Copyright (c) 2005, 2009, Oracle and/or its affiliates. All rights reserved. 
#
#    NAME
#      deinstall - wrapper script that calls deinstall tool.
#
#    DESCRIPTION
#      This script will set all the necessary variables and call the tools
#      entry point.
#
#    NOTES
#      <other useful comments, qualifications, etc.>
#
#    MODIFIED   (MM/DD/YY)
#    prsubram    06/22/09 - Resolve port specific id cmd issue
#    ssampath    06/02/09 - Fix for bug 8566942
#    ssampath    05/19/09 - Move removal of /tmp/deinstall<timestamp> to java
#                           code.
#    prsubram    04/30/09 - Fix for the bug#8474891
#    mwidjaja    04/29/09 - Added user check between the user running the
#                           script and inventory owner
#    ssampath    04/29/09 - Changes to make error message better when deinstall
#                           tool is invoked from inside ORACLE_HOME and -home
#                           is passed.
#    ssampath    04/15/09 - Fix for bug 8414555
#    prsubram    04/09/09 - LD_LIBRARY_PATH is ported for sol,hp-ux & aix
#    mwidjaja    03/26/09 - Disallow -home for running from OH
#    ssampath    03/24/09 - Fix for bug 8339519
#    wyou        02/25/09 - restructure the ohome check
#    wyou        02/25/09 - change the error msg for directory existance check
#    wyou        02/12/09 - add directory existance check
#    wyou        02/09/09 - add the check for the writablity for the oracle
#                           home passed-in
#    ssampath    01/21/09 - Add oui/lib to LD_LIBRARY_PATH
#    poosrini    01/07/09 - LOG related changes
#    ssampath    11/24/08 - Create /main/osds/unix branch
#    dchriste    10/30/08 - eliminate non-generic tools like 'cut'
#    ssampath    08/18/08 - Pickup srvm.jar from JLIB directory.
#    ssampath    07/30/08 - Add http_client.jar and OraCheckpoint.jar to
#                           CLASSPATH
#    ssampath    07/08/08 - assistantsCommon.jar and netca.jar location has
#                           changed.
#    ssampath    04/11/08 - If invoking the tool from installed home, JRE_HOME
#                           should be set to $OH/jdk/jre.
#    ssampath    04/09/08 - Add logic to instantiate ORA_CRS_HOME, JAVA_HOME
#                           etc.,
#    ssampath    04/03/08 - Pick up ldapjclnt11.jar
#    idai        04/03/08 - remove assistantsdc.jar and netcadc.jar
#    bktripat    02/23/07 - 
#    khsingh     07/18/06 - add osdbagrp fix 
#    khsingh     07/07/06 - fix regression 
#    khsingh     06/20/06 - fix bug 5228203 
#    bktripat    06/12/06 - Fix for bug 5246802 
#    bktripat    05/08/06 - 
#    khsingh     05/08/06 - fix tool to run from any parent directory
#    khsingh     05/08/06 - fix LD_LIBRARY_PATH to have abs. path 
#    ssampath    05/01/06 - Fix for bug 5198219
#    bktripat    04/21/06 - Fix for bug 5074246 
#    khsingh     04/11/06 - fix bug 5151658
#    khsingh     04/08/06 - Add WA for bugs 5006414 & 5093832
#    bktripat    02/08/06 - Fix for bug 5024086 & 5024061 
#    bktripat    01/24/06 - 
#    mstalin     01/23/06 - Add lib to pick libOsUtils.so 
#    bktripat    01/19/06 - adding library changes 
#    rahgupta    01/19/06 - 
#    bktripat    01/19/06 - 
#    mstalin     01/17/06 - Modify the assistants deconfig jar file name 
#    rahgupta    01/17/06 - updating emcp classpath 
#    khsingh     01/17/06 - export ORACLE_HOME 
#    khsingh     01/17/06 - fix for CRS deconfig. 
#    hying       01/17/06 - netcadc.jar
#    bktripat    01/16/06 - 
#    ssampath    01/16/06 - 
#    bktripat    01/11/06 - 
#    clo         01/10/06 - add EMCP entries
#    hying       01/10/06 - netcaDeconfig.jar
#    mstalin     01/09/06 - Add OraPrereqChecks.jar 
#    mstalin     01/09/06 - 
#    khsingh     01/09/06 - 
#    mstalin     01/09/06 - Add additional jars for assistants 
#    ssampath    01/09/06 - removing parseOracleHome temporarily 
#    ssampath    01/09/06 - 
#    khsingh     01/08/06 - fix for CRS deconfig
#    ssampath    12/08/05 - added java version check
#    ssampath    12/08/05 - initial run,minor bugs fixed 
#    ssampath    12/07/05 - Creation
#

#MACROS

if [ -z "$UNAME" ]; then UNAME="/bin/uname"; fi
if [ -z "$ECHO" ]; then ECHO="/bin/echo"; fi
if [ -z "$GREP" ]; then GREP="/bin/grep"; fi
if [ -z "$AWK" ]; then AWK="/bin/awk"; fi
if [ -z "$CUT" ]; then CUT="/bin/cut"; fi
if [ -z "$ID" ]; then ID="/usr/bin/id"; fi
if [ -z "$MKDIRP" ]; then MKDIRP="/bin/mkdir -p"; fi
if [ -z "$TOUCH" ]; then TOUCH="/bin/touch"; fi
if [ -z "$CHMOD" ]; then CHMOD="/bin/chmod"; fi
if [ -z "$RMRF" ]; then RMRF="/bin/rm -rf"; fi
if [ -z "$CP" ]; then CP="/bin/cp"; fi
if [ -z "$BASENAME" ]; then BASENAME="/bin/basename"; fi
if [ -z "$TR" ]; then TR="/usr/bin/tr"; fi
if [ -z "$DIRNAME" ]; then DIRNAME="/usr/bin/dirname"; fi
if [ -z "$DATE" ]; then DATE="/bin/date"; fi
if [ -z "$MV" ]; then MV="/bin/mv"; fi
if [ -z "$LS" ]; then LS="/bin/ls"; fi

INSTALLED_VERSION_FLAG=%setInstallFlag%

TOOL_ARGS=$* # initialize this always.

# Since the OTN and the installed version of the tool is same, only way to 
# differentiate is through the instantated variable ORA_CRS_HOME.  If it is 
# NOT instantiated, then the tool is a downloaded version.
# Set HOME_VER to true based on the value of $INSTALLED_VERSION_FLAG
if [ x"$INSTALLED_VERSION_FLAG" = x"true" ]
then
   ORACLE_HOME=%ORACLE_HOME%
   ORA_CRS_HOME=$ORACLE_HOME
   HOME_VER=0  # HOME_VER = true
   TOOL_ARGS="-home $ORACLE_HOME $TOOL_ARGS"
else
   HOME_VER=1  # HOME_VER = false
fi

#Different error conditions
EMPTY_JRE_LOC=1
INCORRECT_JRE_LOC=2
INCORRECT_JRE_VER=3
MISSING_JRE_ARG=4
ROOT_USER=5
MISSING_OH_LOC=6
OH_LOC_EMPTY=7
NOT_VALID_DIR_OH=8
INCORRECT_OH=9
INCORRECT_USER=10

# Save current working directory
CURR_DIR=`pwd`

# Set if -help option needs to be shown
USER_HELP=0

# If CURR_DIR is different from TOOL_DIR get that location and cd into it.
TOOL_REL_PATH=`$DIRNAME $0`
cd $TOOL_REL_PATH

DOT=`$ECHO $TOOL_REL_PATH | $AWK -F'/' '{ print $1}'`

if [ "$DOT" = "." ];
then
  TOOL_DIR=$CURR_DIR/$TOOL_REL_PATH
elif [ `expr "$DOT" : '.*'` -gt 0 ];
then
  TOOL_DIR=$CURR_DIR/$TOOL_REL_PATH
else
  TOOL_DIR=$TOOL_REL_PATH
fi

if [ $HOME_VER = 0 ];
then
   # Run tool from ORACLE_HOME.  Hence, use ORA_CRS_HOME for everything we do. 
   TOOL_DIR=$ORA_CRS_HOME
   TIMESTAMP=`$DATE '+%F_%I-%M-%S-%p'`
   BOOTSTRAP_DIR=/tmp/deinstall$TIMESTAMP
   BOOTSTRAP_JAR=$BOOTSTRAP_DIR/jdk/bin/jar
   BOOTSTRAP_JRE=$BOOTSTRAP_DIR/jdk/jre

   if [ -d $BOOTSTRAP_DIR ]
   then
      $RMRF $BOOTSTRAP_DIR
   fi

   $MKDIRP $BOOTSTRAP_DIR
else
   BOOTSTRAP_DIR=$TOOL_DIR
fi

OUI_DIR=$BOOTSTRAP_DIR/oui/jlib
JLIB_DIR=$BOOTSTRAP_DIR/jlib

LIB_DIR=$BOOTSTRAP_DIR/lib/linux

BIN_DIR=$BOOTSTRAP_DIR/bin
TEMPLATE_DIR=$BOOTSTRAP_DIR/templates
DEINSTALL_DIR=$BOOTSTRAP_DIR
DEINSTALL_JLIB=$DEINSTALL_DIR/jlib

# Check if this script is run as root.  If so, then error out.
# This is fix for bug 5024086.

RUID=`$ID|$AWK -F\( '{print $2}'|$AWK -F\) '{print $1}'`
if [ ${RUID} = "root" ];then
 $ECHO "You must not be logged in as root to run $0."
 $ECHO "Log in as Oracle user and rerun $0."
 exit $ROOT_USER
fi

usage()
{
   CMD=`$ECHO $0 | $AWK '{print substr($0, length($0)-14, length($0))}'`
   $ECHO "Usage:"
   $ECHO ""
   if [ $HOME_VER = 0 ];
   then
      # No need to pass -home when running the tool from existing ORACLE_HOME.
      $ECHO "$CMD "
   else
      $ECHO "$CMD -home <Complete path of Oracle home>"
   fi
   $ECHO "                [ -silent ]"
   $ECHO "                [ -checkonly ]"
   $ECHO "                [ -local ]"
   $ECHO "                [ -paramfile <complete path of input parameter property file> ]"
   $ECHO "                [ -params <name1=value[,name2=value,...]> ]"
   $ECHO "                [ -o <complete path of directory for saving files> ]"
   $ECHO "                [ -help : to get this help instruction. ]"
}

version_check()
#  Arg1 - JRE location entered by user
{
   pname=$1
   MIN_JAVA_VER="150"
   JAVA_VER_CMD=" -fullversion"
   #JAVA_VER="`$pname/bin/java $JAVA_VER_CMD 2>&1 | $AWK '{print substr($0,20,5)}' | $CUT -c1,3,5`"
   JAVA_VER="`$pname/bin/java $JAVA_VER_CMD 2>&1 | $AWK '{print substr($0,20,5)}' | $AWK 'BEGIN {FS="."}{print $1$2$3}'`"
   if [ $JAVA_VER -lt $MIN_JAVA_VER ]
   then
      $ECHO "JAVA version specified is $JAVA_VER.  This application needs atleast $MIN_JAVA_VER to run."
      exit $INCORRECT_JRE_VER
   fi
}

unsetEnv()
{
   # In case of downloaded version, we unset ORACLE_HOME and ORACLE_BASE.

   if [ x"$ORACLE_HOME" != "x" ]
   then
      unset ORACLE_HOME
   fi 

   if [ x"$ORACLE_BASE" != "x" ]
   then
      unset ORACLE_BASE
   fi 
}

parseJREHome()
{
  # This for loop will parse all commanline arguments and extract jrehome, and 
  # other arguments needed for the deconfig tool itself.

  for JPOS in $ARGS
  do
     if [ "$JFLAG_SET" = "t" ]
     then
       JRE_HOME=$JPOS
       $ECHO "JRE_HOME IS $JRE_HOME"
       JFLAG_SET="f"
     fi

     if [ "$JPOS" = "-j" ]
     then
        #$ECHO "JPOS IS $JPOS"
        JFLAG_SET="t"
     fi

     if [ "$JPOS" != "-j" ] && [ x"$JPOS" != x"$JRE_HOME" ]
     then
        #$ECHO "JPOS is $JPOS"
        TOOL_ARGS=`$ECHO "$TOOL_ARGS $JPOS"`
     fi
  done
}

parseOracleHome()
{
  # This for loop will parse all commanline arguments and extract oracle home,
  # and other arguments needed for the deconfig tool itself.

  USER_ARGS=$*
  while [ $# -gt 0 ];
  do
     temp=`$ECHO $1 | $TR  "[A-Z]" "[a-z]"`
     if [ "$temp" = "-help" ];
     then
     	USER_HELP=1
     fi
     if [ "$temp" = "-home" ];
     then
       if [ $HOME_VER = 0 ];
       then
          $ECHO "Error: invalid argument -home."
          $ECHO "The deinstall tool located in an Oracle home is configured to remove,"
	  $ECHO "only that Oracle home. You cannot specify the -home option."
          exit 1;
       fi

       if [ $# -gt 1 ];
       then
          ORACLE_HOME=$2
          
          # If first character following -home is -, then we error out.
          # For example, -home -checkonly or -home -help etc.,

          FIRST_CHAR=`$ECHO $ORACLE_HOME | $AWK '{print substr($1, 1, 1)}'`
          DELIMIT_CHAR="-"

          if [ x"$FIRST_CHAR" = x"$DELIMIT_CHAR" ];
          then
             $ECHO ""
             usage
             $ECHO "Specify the complete path of the Oracle home that you want to remove."
             exit $MISSING_OH_LOC
          fi
         
          if [ ! -d $ORACLE_HOME/inventory ];
          then
             if [[ -f $ORACLE_HOME/inventory || -h $ORACLE_HOME/inventory ]]
             then
                usage
                $ECHO ""
                $ECHO "The path specified for Oracle home $ORACLE_HOME is not a valid directory."
                $ECHO "Make sure to specify a valid Oracle home path with inventory directory in it."
                exit $NOT_VALID_DIR_OH
             else
                usage
                $ECHO ""
                $ECHO "The path specified for Oracle home $ORACLE_HOME does not exist or there is no inventory directory within Oracle home."
                $ECHO "In order to proceed, create an empty Oracle home directory with an empty inventory directory in it, then rerun the tool."
                exit $OH_LOC_EMPTY
             fi
          fi
	  
          $ECHO "ORACLE_HOME = $ORACLE_HOME"
	  shift;
       fi
     fi
     shift;
  done
}

parseCheckOnly()
{
  # This for loop will parse all commanline arguments and extract checkonly,
  # and other arguments needed for the deconfig tool itself.

  USER_ARGS=$*
  while [ $# -gt 0 ];
  do
     temp=`$ECHO $1 | $TR  "[A-Z]" "[a-z]"`
     if [ "$temp" = "-checkonly" ];
     then
       CHECK_ONLY_USED=true
     fi
     shift;
  done

}

setClassPath()
{
    #Component specific deconfig JARs
    TOOL_JAR=deinstall_wrapper.jar
    INSTALL_DECONFIG_JAR=deinstall_core.jar
    XML_PARSER_JAR=xmlparserv2.jar
    CRS_DECONFIG_JAR=$JLIB_DIR/crsdc.jar
    OUI_DECONFIG_JAR=$OUI_DIR/OraInstaller.jar
    OUI_CHECKPOINT_JAR=$OUI_DIR/OraCheckPoint.jar
    OCM_DECONFIG_JAR=$JLIB_DIR/deconfigCCR.jar

    ##Other JARs for components
    OUI_PREREQ_JAR=$OUI_DIR/OraPrereq.jar
    OUI_PREREQ_CHECKS_JAR=$OUI_DIR/OraPrereqChecks.jar
    SHARE_JAR=$OUI_DIR/share.jar
    EWT_JAR=$JLIB_DIR/ewt3.jar
    HELP_JAR=$JLIB_DIR/help4.jar

    ##Dependent JARs for components
    SRVM_JAR=$JLIB_DIR/srvm.jar
    SRVMHAS_JAR=$JLIB_DIR/srvmhas.jar
    SRVMASM_JAR=$JLIB_DIR/srvmasm.jar
    VIPCA_JAR=$JLIB_DIR/vipca.jar
    CVU_JAR=$JLIB_DIR/cvu.jar
    NETCA_JAR=$BOOTSTRAP_DIR/assistants/netca/jlib/netca.jar
    NETCAM_JAR=$JLIB_DIR/netcam.jar
    NETCFG_JAR=$JLIB_DIR/netcfg.jar
    LDAP_JAR=$JLIB_DIR/ldapjclnt11.jar
    DBCA_JAR=$JLIB_DIR/dbca.jar
    DBUA_JAR=$JLIB_DIR/dbma.jar
    UTIL_JAR=$BOOTSTRAP_DIR/assistants/jlib/assistantsCommon.jar
    EMCORE_JAR=$JLIB_DIR/emCORE.jar
    EMCONFIG_JAR=$JLIB_DIR/emConfigInstall.jar 
    EMCA_JAR=$JLIB_DIR/emca.jar
    HTTP_CLIENT_JAR=$OUI_DIR/http_client.jar

    TOOL_CLASSPATH=$DEINSTALL_JLIB/$TOOL_JAR
    INSTALL_CLASSPATH=$DEINSTALL_JLIB/$INSTALL_DECONFIG_JAR
    XML_PARSER_CLASSPATH=$OUI_DIR/$XML_PARSER_JAR
    CRS_CLASSPATH=$CRS_DECONFIG_JAR:$VIPCA_JAR:$CVU_JAR
    COMMON_DEP_CLASSPATH=$SRVM_JAR:$SRVMHAS_JAR
    OUI_CLASSPATH=$OUI_DECONFIG_JAR:$OUI_CHECKPOINT_JAR:$OUI_PREREQ_JAR:$SHARE_JAR:$EWT_JAR:$HELP_JAR:$XML_PARSER_CLASSPATH
    ASSISTANTS_CLASSPATH=$UTIL_JAR:$DBCA_JAR:$DBUA_JAR:$OUI_PREREQ_CHECKS_JAR:$SRVMASM_JAR
    NETCA_CLASSPATH=$NETCA_JAR:$NETCAM_JAR:$NETCFG_JAR:$LDAP_JAR
    EMCP_CLASSPATH=$SRVMASM_JAR:$XML_PARSER_CLASSPATH:$UTIL_JAR:$SHARE_JAR:$EMCONFIG_JAR:$EMCORE_JAR:$EMCA_JAR:$HTTP_CLIENT_JAR
    OCM_CLASSPATH=$OCM_DECONFIG_JAR

    CLASSPATH=$TOOL_CLASSPATH:$CRS_CLASSPATH:$COMMON_DEP_CLASSPATH:$INSTALL_CLASSPATH:$OUI_CLASSPATH:$ASSISTANTS_CLASSPATH:$NETCA_CLASSPATH:$EMCP_CLASSPATH:$OCM_CLASSPATH
}

setLoaderPath()
{
case `$UNAME` in
SunOS)
	 LD_LIBRARY_PATH=$LIB_DIR:$BOOTSTRAP_DIR/lib:$BOOTSTRAP_DIR/oui/lib/solaris:${LD_LIBRARY_PATH}
	 export LD_LIBRARY_PATH
;;
HP-UX)
         LD_LIBRARY_PATH=$LIB_DIR:$BOOTSTRAP_DIR/lib:$BOOTSTRAP_DIR/oui/lib/hpunix:${LD_LIBRARY_PATH}
         export LD_LIBRARY_PATH
;;
AIX)
         LD_LIBRARY_PATH=$LIB_DIR:$BOOTSTRAP_DIR/lib:$BOOTSTRAP_DIR/oui/lib/aix:${LD_LIBRARY_PATH}
         export LD_LIBRARY_PATH
;;
*)	
	LD_LIBRARY_PATH=$LIB_DIR:$BOOTSTRAP_DIR/lib:$BOOTSTRAP_DIR/oui/lib/linux:${LD_LIBRARY_PATH}
	export LD_LIBRARY_PATH
;;
esac

}

setOracleBinary()
{
  if [ ! -z "$ORACLE_HOME" ];
  then
    if [ ! -d $ORACLE_HOME ];
    then
       $MKDIRP $ORACLE_HOME
       ORACLE_HOME_TOUCHED=true
    fi

    if [ ! -f $ORACLE_HOME/bin/oracle ];
    then
       $MKDIRP $ORACLE_HOME/bin

       if [ -w $ORACLE_HOME/bin ];
       then
          $TOUCH $ORACLE_HOME/bin/oracle
       fi
    fi
   fi
}

setRACGwrap()
{
  if [ ! -z "$ORACLE_HOME" ];
  then
    if [ ! -f $ORACLE_HOME/bin/racgwrap ];
    then
       $MKDIRP $ORACLE_HOME/bin
       $TOUCH $ORACLE_HOME/bin/racgwrap
       $ECHO "ORACLE_HOME=$ORACLE_HOME" > $ORACLE_HOME/bin/racgwrap
    fi
  fi
}

setOSDBAGrp()
{
  if [ ! -z "$ORACLE_HOME" ];
  then
    if [ ! -f $ORACLE_HOME/bin/osdbagrp ];
    then
       $MKDIRP $ORACLE_HOME/bin
       $CP $BIN_DIR/osdbagrp $ORACLE_HOME/bin/osdbagrp
    fi
  fi
}

runFilesListCheck()
{
   $ECHO "Checking for required files and bootstrapping ..."
   $ECHO "Please wait ..."

   # Check to see if all the files we require for running Files List
   # is in present in the home.  If it is not present, then there is
   # no point in continuing further.

   if [ ! -f $ORACLE_HOME/deinstall/jlib/deinstall_wrapper.jar ];
   then
      $ECHO "The deinstall file, deinstall_wrapper.jar does not exist in Oracle home,"
      $ECHO "under deinstall/jlib directory."
      $ECHO "You cannot use the deinstall tool from this home. Download the stand-alone"
      $ECHO "version of the tool from OTN to deconfigure and deinstall the home."
      exit 1;
   fi

   # Extract required files from .lst
   cd $BOOTSTRAP_DIR
   FILES_TO_EXTRACT=`$ORACLE_HOME/jdk/bin/jar tf $ORACLE_HOME/deinstall/jlib/deinstall_wrapper.jar | $GREP ".lst"`
   $ORACLE_HOME/jdk/bin/jar xf $ORACLE_HOME/deinstall/jlib/deinstall_wrapper.jar $FILES_TO_EXTRACT

   # required_files.lst is already extracted to BOOTSTRAP_DIR.
   FILES_LIST=`$TR '\n' ' ' < $BOOTSTRAP_DIR/mapfiles/required_files.lst`
   FILES_TO_COPY=""

   for FILE in $FILES_LIST
   do
      FNAME=`$BASENAME $FILE` 
      # 1. Skip check for OUI dll's.
      case "$FNAME" in
         *.dll) continue;;
      # 2. Process EM template files as it gets instantiated and 
      #    gets copied as something else.
         *.template) FNAME=`$ECHO $FNAME | $AWK -F'.' '{ print $1 "." $2}'`; DIR=`$DIRNAME $FILE`; FILE="$DIR/$FNAME";;
         *);;
      esac
      
      # File bug against OUI.  There is 2 copies of ouica.properties
      # one under $OH/oui/ouica.properties and $OH/oui/bin/ouica.properties
      # File under $OH/oui/bin is not present.
  
      if [ $FILE = "oui/bin/ouica.properties" ];
      then
         continue;
      fi
 
      # Special handling needed for perl/bin_tmp, lib_tmp and modules_lib_tmp
      case "$FILE" in
         *_tmp*)continue;;
      esac

      if [ ! -f $ORACLE_HOME/$FILE ];
      then
         $ECHO "The file, $FILE, does not exist in the Oracle home."
         $ECHO "You cannot use the deinstall tool from this home. Download the stand-alone"
         $ECHO "version of the tool from OTN to deconfigure and deinstall the home."
         $RMRF $BOOTSTRAP_DIR
         exit 1;
      else
         DIR=`$DIRNAME $FILE`
         if [ ! -d $DIR ];
         then
            $MKDIRP $BOOTSTRAP_DIR/$DIR
         fi

         $CP -f $ORACLE_HOME/$FILE $BOOTSTRAP_DIR/$DIR
      fi
   done

}

checkUser ()
{
   if [ x"$ORACLE_HOME" != "x" ]; then
     owner=`$LS -ld $ORACLE_HOME/inventory | $AWK '{print $3}'`
     id=`$ID | $AWK -F\( '{print $2}' | $AWK -F\) '{print $1}'`
     if [ "$owner" != "$id" ] 
      then
         echo "ERROR: Oracle home not owned by user."
         echo "The user running the deinstall tool is not the same as owner of Oracle home"
         echo "based on inventory ownership."
         echo "You must run this tool as the same user who installed the software."
         exit $INCORRECT_USER
      fi
   fi
}

ARGS="$@"

if [ $HOME_VER = 0 ];
then
   parseOracleHome $*
   checkUser
   runFilesListCheck   
   # Need to copy deinstall jars, deinstall xml to $BOOTSTRAP location for now.
   $CP -rf $ORACLE_HOME/deinstall/* $BOOTSTRAP_DIR

   # bootstrap jdk to /tmp/deinstall and invoke java
   $CP -prf $TOOL_DIR/jdk $BOOTSTRAP_DIR
   JRE_HOME=$BOOTSTRAP_JRE
   PERL_HOME=$BOOTSTRAP_DIR/perl
else
   # Downloaded version.
   unsetEnv
   JRE_HOME=$TOOL_DIR/jre
   PERL_HOME=$TOOL_DIR/perl
   parseOracleHome $*
   checkUser
   parseJREHome $ARGS
fi

PERL_BIN=$PERL_HOME/bin
PERL_LIB=$PERL_HOME/lib

# Export ORACLE_HOME.
export ORACLE_HOME

if [ ! -f $ORACLE_HOME/bin/oracle ];
then
   HOMELOCSTAT="true"      # Flag to determine if home is present or not 
else
   HOMELOCSTAT="false"
fi

# Check only flag passed or not
parseCheckOnly $*

if [ -f $JRE_HOME/bin/java ]
then
   version_check $JRE_HOME
else
   $ECHO "The location, $JRE_HOME is not a valid jre home.  Download the tool which has a packaged jre version."
   exit $INCORRECT_JRE_LOC
fi

setClassPath
setLoaderPath

# Fix for bug 5074246
# Give 777 permission for $TOOL_DIR/logs and $TOOL_DIR/templates, so that 
# the logs and properties file gets created, even though this utility is 
# run be user other than oracle user.

if [ -d "$TEMPLATE_DIR" ];
then
   $CHMOD 777 $TEMPLATE_DIR
fi

#set SRVM tracing
TRACE=""

if [ "x$SRVM_TRACE" != "x" ]
then
   TRACE="-DTRACING.ENABLED=true -DTRACING.LEVEL=2"
fi

setOracleBinary
setRACGwrap
setOSDBAGrp

if [ `$UNAME` != "Linux" ]; then
   JAVA_CMD="$JRE_HOME/bin/java -d64"
else
   JAVA_CMD=$JRE_HOME/bin/java
fi

$JAVA_CMD -DNOHOME=$HOMELOCSTAT -Dpid=$$ -Doracle.installer.jre_loc=$JRE_HOME -Doracle.installer.oui_loc=$TOOL_DIR/oui -DHOME_OPT=$HOME_VER -DBOOTSTRAP_DIR=$BOOTSTRAP_DIR $TRACE -classpath $CLASSPATH oracle.install.db.deinstall.wrapper.clusterDeconfig $TOOL_ARGS

status=$?
if [ $status != 0 ];
then
  if [ ! -z "$ORACLE_HOME_TOUCHED" ];
  then
    $ECHO "Deleting the Oracle home touched for deconfiguration"
    $RMRF $ORACLE_HOME
  fi
elif [ -d $ORACLE_HOME ];
then
  if [ ! -z "$ORACLE_HOME_TOUCHED" ];
  then
    $ECHO "Deleting the Oracle home touched for deconfiguration"
    $RMRF $ORACLE_HOME
  fi
fi

exit 0

