MINI MINI MANI MO

Path : /opt/oracle/product/18c/dbhomeXE/crs/install/
File Upload :
Current File : //opt/oracle/product/18c/dbhomeXE/crs/install/tfa_setup

#!/bin/sh
#
# $Header: tfa/src/v2/install_wrapper.sh /main/121 2017/11/16 11:44:20 gadiga Exp $
#
# install_wrapper.sh
#
# Copyright (c) 2012, 2017, Oracle and/or its affiliates. All rights reserved.
#
#    NAME
#      install_wrapper.sh - <one-line expansion of the name>
#
#    DESCRIPTION
#      <short description of component this file declares/defines>
#
#    NOTES
#      <other useful comments, qualifications, etc.>
#
#    MODIFIED   (MM/DD/YY)
#    gadiga      11/07/17 - bug 27046281. handle cases when CRSHOME is set in
#                           env
#    bibsahoo    09/22/17 - FIX BUG 26568567
#    cnagur      09/22/17 - Fixed variable Quote Issues on SunOS - Bug 26799680
#    gadiga      09/07/17 - DSC upgrade from 12.2, remove old TFA
#    cnagur      09/05/17 - Fix for Bug 26636362
#    gadiga      08/23/17 - solaris -e does not work
#    cnagur      08/18/17 - Fix for Bug 26650090
#    llakkana    08/08/17 - TFA upgrade changes in DSC from 12.0.1(COLLECTOR
#                           MODE) to 18.1(RECEIVER MODE)
#    cnagur      08/01/17 - Remove changes related to certificates
#    cnagur      07/24/17 - Fix for Bug 26522376
#    gadiga      07/24/17 - copy tfa resource file as tfar_crs.sh
#    cnagur      07/13/17 - Removed extra blank lines
#    cnagur      07/07/17 - Fix for Bug 26328304
#    bibsahoo    07/05/17 - FIX BUG 26393355
#    gadiga      06/25/17 - copy as tfa_crs.sh as srvctl created resource with
#                           that name
#    bburton     06/05/17 - Do not check for bash
#    bburton     05/26/17 - No receiver mode in this version
#    bburton     05/09/17 - Do not check for setting of tmplocdir
#    llakkana    05/04/17 - Don't show help TFA installed from root script
#    cnagur      03/09/17 - Copy Buildid after patching - Bug 25385434
#    cnagur      02/21/17 - Print Version and BuildID
#    bburton     01/31/17 - Allow user to provide TMP location instaead of
#                           using /tmp
#    cnagur      12/12/16 - Support for Open JDK - Bug 25237278
#    cnagur      11/25/16 - Fix for Bug 25054308
#    manuegar    10/25/16 - manuegar_extract_tfa_03. Added -setup to support
#                           first time ND configuration.
#    cnagur      10/23/16 - Fix for Bug 24935220
#    cnagur      05/29/17 - Fix for Bug 26133443
#    cnagur      10/12/16 - Fix for Bug 24821738
#    manuegar    10/11/16 - manuegar_extract_tfa_02.
#    cnagur      09/14/16 - TFA Client Logging
#    manuegar    08/24/16 - Support the -extractto switch in the TFA installer.
#    cnagur      08/16/16 - Verify Hostname - Bug 24414115
#    llakkana    06/10/16 - File permission changes
#    gadiga      05/31/16 - discover backup diskgroup
#    gadiga      05/24/16 - get backup diskgroup name
#    cnagur      05/17/16 - Fix for Bug 23195605
#    gadiga      05/03/16 - add tfar to crs
#    bburton     05/03/16 - use old discovery in ADE until new discovery is
#                           fixed
#    bibsahoo    04/19/16 - TFA DISCOVERY TO PERL
#    cnagur      03/28/16 - Fix for Bug 21340546
#    cnagur      03/06/16 - Update perms of TFA Install Log - Bug 22753361
#    llakkana    02/26/16 - which & bash fix
#    cnagur      02/18/16 - Copy JRE and Perl for SI
#    cnagur      02/03/16 - Copy tfactl to DB_HOME - Bug 22647922
#    cnagur      02/01/16 - Fix for Bug 22636834
#    bburton     01/29/16 - Set an installed Marker for use by API
#    bburton     01/26/16 - Write debug log for install/patch
#    gadiga      01/14/16 - setup receiver
#    cnagur      01/12/16 - Changes for Java 8
#    llakkana    12/21/15 - Fix 22393355
#    cnagur      11/05/15 - Changes to migrate TFA_HOME
#    cnagur      11/02/15 - Added .buildversion
#    cnagur      10/22/15 - Added internal flag -ade
#    cnagur      10/22/15 - Fix for Bug 22084627
#    arupadhy    09/16/15 - Added condition, if TFABASE and JAVA_HOME noth are
#                           available continue installation in silent mode even
#                           if crs is down
#    bburton     09/11/15 - XbranchMerge bburton_bug-21517347 from
#                           st_tfa_12.1.2.5
#    bibsahoo    09/02/15 - BUG 21767864
#    bburton     08/03/15 - fix export issue
#    cnagur      07/30/15 - Added internal flag -patch
#    cnagur      07/09/15 - Remove seconds from buildid on HP-UX - 21407010
#    cnagur      04/10/15 - Fix for Bug 20820136
#    gadiga      01/20/15 - copy oratop.pm in DB/CRS install
#    amchaura    01/19/15 - 20380630 - TFA SUPPORT FOR EXADATA VM/DOM0
#    cnagur      12/17/14 - Added TFA Upgrade Status
#    cnagur      11/06/14 - Fix for Bug 19470877
#    cnagur      10/27/14 - Fix for Bug 19665451
#    cnagur      09/12/14 - Fix for Bug 19559964
#    cnagur      09/08/14 - Use PERL instead of AWk for hostname
#    bburton     05/29/14 - bug 18862153 used invalied test args in oratop
#                           addition
#    cnagur      05/29/14 - Fix for Bug 18841413
#    cnagur      05/22/14 - BUILDID issues with SunOS
#    cnagur      05/20/14 - BUILDID issues with HP-UX
#    cnagur      05/19/14 - Fix for Bug 18765905
#    cnagur      04/25/14 - CD to home dir - Bug 18634296
#    cnagur      04/14/14 - AWK Issues - Bug 18533720
#    cnagur      03/17/14 - Export PERL
#    cnagur      03/04/14 - Fix for Bug 18343490
#    amchaura    02/28/14 - Fix scanner after patch/restart TFA
#    cnagur      02/25/14 - Updated TFA messages and exit status
#    cnagur      02/07/14 - Added check for BASEDIR if it ends with /tfa/
#    llakkana    01/21/14 - TFA Receiver Changes
#    cnagur      01/20/14 - Removed duplicate AWK
#    cnagur      12/30/13 - Added REL_DIR check for Non-GI Install
#    cnagur      12/18/13 - Not remove any files for debug
#    cnagur      12/18/13 - Bug 17964643
#    cnagur      12/17/13 - Bug 17964798
#    gadiga      11/22/13 - SI support
#    cnagur      11/18/13 - 12.1 Release directory changes
#    cnagur      11/11/13 - Added check to validate CRSHOME Bug:17730581
#    cnagur      11/05/13 - Added changes to remove trailing / from GIHOME
#    cnagur      10/29/13 - Remove Old TFA for GI Install
#    cnagur      10/23/13 - Changes to remove output dir in ORACLE_BASE if
#                           Install is interrupted
#    cnagur      10/11/13 - Updated REPO_DIR for ODA GI Install
#    cnagur      10/07/13 - Changes for ODA GI Install
#    cnagur      09/24/13 - Pass -local and -silent when patching
#    cnagur      09/11/13 - TFA Patching Changes for 12.1 Shiphome
#    cnagur      08/26/13 - Remove TFA_BASE if Install was not sucessful.
#    cnagur      07/12/13 - Added function getCommandLocation()
#    cnagur      09/03/13 - TFA Installer Changes for 12.1 Shiphome
#    cnagur      08/12/13 - Fix for Bugs 17279427 and 17290209
#    cnagur      08/01/13 - Fix for Bug: 17168390
#    bburton     06/26/13 - remove shell incompatibilities
#    bburton     06/25/13 - /c.
#    cnagur      06/19/13 - Fix for Bug 16973989
#    bburton     06/19/13 - remove -e from echo commands
#    cnagur      06/19/13 - Updated script to remove BDB and Logs if it in
#                           ORACLE_BASE
#    bburton     06/18/13 - add the debug option so as not to remove tfa on
#                           install failure
#    bburton     06/18/13 - remove -crshome option from help and make is silent
#                           by default.
#    cnagur      06/16/13 - Moving Tfa logs to /tmp incase the installation was
#                           successful
#    cnagur      06/11/13 - Should not prompt the Install Location if -silent
#                           is passed
#    bburton     06/10/13 - add some command help and warn for illegal
#                           combinations.
#    bburton     06/07/13 - allow for -local flag
#    bburton     06/06/13 - add option to defer discovery and pass javahome
#    gadiga      05/29/13 - add build id
#    bburton     05/23/13 - Add the -crshome flag for rootcrs.pl install
#    cnagur      05/10/13 - Added /tfa to <BASEDIR> if not present
#    cnagur      05/08/13 - Added /tfa to BASEDIR if it not ends with /tfa.
#    cnagur      05/02/13 - New Directory Structure for TFA
#    gadiga      02/14/13 - permission
#    bburton     01/22/13 - fix init path for other platforms
#    gadiga      01/13/13 - sunos
#    gadiga      01/11/13 - remove duplicates
#    gadiga      01/10/13 - fix tail command
#    bburton     07/30/12 - Creation
#

#Function to get the location of commands
getCommandLocation() {

        COMMAND=$1;

        if [ -f "/bin/$COMMAND" ]
        then
                CMDLOC="/bin/$COMMAND";
        elif [ -f "/usr/bin/$COMMAND" ]
        then
                CMDLOC="/usr/bin/$COMMAND";
        elif [ -f "/usr/local/bin/$COMMAND" ]
        then
                CMDLOC="/usr/local/bin/$COMMAND";
        else
                CMDLOC="$COMMAND";
        fi

        echo "$CMDLOC";
}

getJavaVersion() {

	JV_JAVA=$1;	

	if [ -x "$JV_JAVA" ]
	then
		JAVA_VERSION=`$JV_JAVA -version 2>&1 | $GREP " version " | $HEAD -1 | $CUT -d '"' -f2 | $CUT -c 1-3`;
	fi

	if [ ! -n "$JAVA_VERSION" ]
	then
		JAVA_VERSION="1.4";
	fi

	$ECHO "$JAVA_VERSION";
}

startlog() {

	DATEFORFILE=`$DATE "+%Y_%m_%d-%H_%M_%S"`
        LOGFILE="/tmp/tfa_install_$$_"$DATEFORFILE".log"
        if [ -f "$LOGFILE" ] || [ -L "$LOGFILE" ]
        then
                $ECHO "TFA-00017: Unable to create the TFA Installation Log File no log file will be written" 
                LOGFILE="/dev/null"
        else
                $ECHO "["`$DATE "+%Y-%m-%d %H:%M:%S"`"] Log File written to : "$LOGFILE > $LOGFILE
                if [ "$?" -gt "0" ]
		then              
                	$ECHO "TFA-00017: Unable to create Installation Log File no log file will be written" 
                	LOGFILE="/dev/null"
		else
                        $ECHO "TFA Installation Log will be written to File : "$LOGFILE
                	$CHMOD 600 $LOGFILE
		fi
        fi
        LOGFILEPARAM=" -logfile $LOGFILE"
}

writelog() {
 
	if [ -n "$2" ]
        then
                WRITEWHERE=$2
        else
                WRITEWHERE="BOTH"
        fi


        if [ "$WRITEWHERE" = "BOTH" ]
        then
                $ECHO "["`$DATE "+%Y-%m-%d %H:%M:%S"`"] "$1 >> $LOGFILE
                $ECHO $1
	elif [ "$WRITEWHERE" = "LOG" ]
	then
		$ECHO "["`$DATE "+%Y-%m-%d %H:%M:%S"`"] "$1 >> $LOGFILE
        else
                $ECHO $1
        fi
}


UNAME=`getCommandLocation "uname"`;
PLATFORM=`$UNAME`
AWK=`getCommandLocation "awk"`;
BASENAME=`getCommandLocation "basename"`;
PWD_PATH=`getCommandLocation "pwd"`;
CAT=`getCommandLocation "cat"`;
CHGRP=`getCommandLocation "chgrp"`;
CHOWN=`getCommandLocation "chown"`;
CHMOD=`getCommandLocation "chmod"`;
CP=`getCommandLocation "cp"`;
CD=`getCommandLocation "cd"`;
CUT=`getCommandLocation "cut"`;
DATE=`getCommandLocation "date"`;
DF=`getCommandLocation "df"`;
DIFF=`getCommandLocation "diff"`;
DIRNAME=`getCommandLocation "dirname"`;
ECHO=`getCommandLocation "echo"`;
EXPR=`getCommandLocation "expr"`;
FIND=`getCommandLocation "find"`;
GREP=`getCommandLocation "grep"`;
HEAD=`getCommandLocation "head"`;
LBIN=/usr/local/bin;
LS=`getCommandLocation "ls"`;
MKDIR=`getCommandLocation "mkdir"`;
MV=`getCommandLocation "mv"`;
PERL=`getCommandLocation "perl"`;
PS=`getCommandLocation "ps"`;
READLINK=`getCommandLocation "readlink"`;
RM=`getCommandLocation "rm"`;
SED=`getCommandLocation "sed"`;
SH=`getCommandLocation "sh"`;
BASH=`getCommandLocation "bash"`;
STAT=`getCommandLocation "stat"`;
TAIL=`getCommandLocation "tail"`;
TAR=`getCommandLocation "tar"`;
TEE=`getCommandLocation "tee"`;
TOUCH=`getCommandLocation "touch"`;
UNZIP=`getCommandLocation "unzip"`;
WC=`getCommandLocation "wc"`;

startlog

case $PLATFORM in
Linux)
        ID=/etc/init.d
        ;;
AIX)
        ID=/etc
        ;;
SunOS)
        ID=/etc/init.d
        GREP=/usr/xpg4/bin/grep
	AWK=`getCommandLocation "gawk"`;

	# if not able to find gawk then use nawk
	if [ `$ECHO $AWK | $GREP -c "^gawk$"` -eq 1 ]
	then
		AWK=`getCommandLocation "nawk"`;

		# if not found, then exit with proper message
		if [ `$ECHO $AWK | $GREP -c "^nawk$"` -eq 1 ]
		then
			writelog "TFA-00016: Oracle Trace File Analyzer (TFA) requires GAWK or NAWK. Please install gawk or nawk and try again."
		fi
	fi
	if [ -f "/usr/sfw/bin/gtar" ]; then
	  TAR="/usr/sfw/bin/gtar"
 	fi
        ;;	
HP-UX)
        ID=/sbin/init.d
        ;;
*)     writelog "TFA-00000: Oracle Trace File Analyzer (TFA) is not supported on this platform."
       exit -1
       ;;
esac

RUID=`/usr/bin/id | $AWK -F\( '{print $1}' | $AWK -F= '{print $2}'`;

SCRIPT=${0}
#MESHORT=${0##*/}
SILENT=""
CRSHOME=""
CRSHOMEFLAG=""
ORAHOME=""
ORABASE=""
ORAHOMEFLAG=""
DEFERDISCOVERY=""
LOCALONLY=""
MODE=""
DISCOVERYFILE=""
DISCOVERYFILEFLAG=""

BUILDID=18100020171203222424

# Extract Current TFA Build Version and Date
BUILD_DATE=`$ECHO $BUILDID | $TAIL -15c`;
BUILD_VERSION=`$ECHO $BUILDID | $SED "s/$BUILD_DATE//"`;

# We might need original Build Date later
ORG_BUILD_DATE="$BUILD_DATE";

# Remove Seconds from Build Date
BUILD_DATE=`$ECHO $BUILD_DATE | $CUT -c 1-12`;
RUID=`/usr/bin/id|$AWK -F\( '{print $1}'|$AWK -F= '{print $2}'`

# print help function
printhelp() {

$ECHO ""
$ECHO "   Usage for $SCRIPT"
$ECHO ""
$ECHO "   $SCRIPT [[-local][-deferdiscovery][-tfabase <install dir>][-javahome <path to JRE>][-silent][-receiver]|-extractto <extract dir> ]"
$ECHO ""
$ECHO "        -local            -    Only install on the local node"
$ECHO "        -deferdiscovery   -    Discover Oracle trace directories after installation completes"
$ECHO "        -tfabase          -    Install into the directory supplied "
$ECHO "        -javahome         -    Use this directory for the JRE"
$ECHO "        -silent           -    Do not ask any install questions"
$ECHO "        -receiver         -    TFA runs in receiver mode.  Otherwise run in full mode"
$ECHO "        -extractto        -    Extract TFA into the directory supplied (non daemon mode)"
$ECHO "        -tmploc           -    Temporary location directory for TFA to extract the install archive to (must exist)"
$ECHO "        -debug            -    Print debug tracing and do not remove TFA_HOME on install failure"
$ECHO "        -perlhome         -    Custom location of perl binaries"
$ECHO ""
$ECHO ""
$ECHO "   Note: Without parameters TFA will take you through an interview process for installation"
$ECHO "         /tfa will be appended to -tfabase if it is not already there. "
$ECHO ""
$ECHO ""

}

# Extract ext jar
install_ext() {
  if [ ! -n "$CRSHOME" ] ; then 
    if [ -d "$tfa_home/ext" ] ; then
      $CHMOD a+x $tfa_home/ext;
      $CHMOD a+x $tfa_home/ext/oratop 2>/dev/null;
      $CHMOD a+x $tfa_home/ext/oratop/* 2>/dev/null;
    fi
    return; 
  fi

  if [ -d "$tfa_home" ] ; then
    c_loc=`$PWD_PATH`
    java_home_loc=`grep "JAVA_HOME=" $tfa_home/tfa_setup.txt | sed 's/JAVA_HOME=//'`
    jar_exe="$java_home_loc/bin/jar";
    if [ ! -f "$jar_exe" ] ; then
      jar_exe="$java_home_loc/../bin/jar";
    fi
    if [ ! -f "$jar_exe" ] ; then
      crs_home_loc=`grep "CRS_HOME=" $tfa_home/tfa_setup.txt | sed 's/CRS_HOME=//'`;
      if [ -f "$crs_home_loc/jdk/bin/jar" ] ; then
        jar_exe="$crs_home_loc/jdk/bin/jar";
      fi
    fi
    if [ ! -f "$jar_exe" ] ; then
      echo "ORATOP-WARNING: Could not find jar program";
      return;
    fi
    cd $tfa_home/jlib
    $jar_exe xf RATFA.jar ext.jar
    $MV ext.jar $tfa_home/ext/
    if [ -d "$java_home_loc" ] ; then
      cd $tfa_home/ext
      if [ -f "ext.jar" ] ; then
        writelog "Installing oratop extension..";
        writelog "";
        case $PLATFORM in
          Linux)
            $RM -f oratop/oratop.RDBMS_12.1_LINUX.X64 oratop/oratop.RDBMS_12.1_LINUX.X32 oratop/oratop.RDBMS_11.2_LINUX.X64 oratop/oratop.RDBMS_11.2_LINUX.X32 oratop/oratop.pm
            $jar_exe xf ext.jar oratop/oratop.RDBMS_12.1_LINUX.X64 oratop/oratop.RDBMS_12.1_LINUX.X32 oratop/oratop.RDBMS_11.2_LINUX.X64 oratop/oratop.RDBMS_11.2_LINUX.X32 oratop/oratop.pm
          ;;
          AIX)
            $RM -f oratop/oratop.RDBMS_12.1_AIX.PPC64 oratop/oratop.RDBMS_11.2_AIX.PPC64 oratop/oratop.pm
            $jar_exe xf ext.jar oratop/oratop.RDBMS_12.1_AIX.PPC64 oratop/oratop.RDBMS_11.2_AIX.PPC64 oratop/oratop.pm
          ;;
          SunOS)
            $RM -f oratop/oratop.RDBMS_11.2_SOLARIS.X64 oratop/oratop.RDBMS_11.2_SOLARIS.SPARC64 oratop/oratop.RDBMS_12.1_SOLARIS.X64 oratop/oratop.RDBMS_12.1_SOLARIS.SPARC64 oratop/oratop.pm
            $jar_exe xf ext.jar oratop/oratop.RDBMS_11.2_SOLARIS.X64 oratop/oratop.RDBMS_11.2_SOLARIS.SPARC64 oratop/oratop.RDBMS_12.1_SOLARIS.X64 oratop/oratop.RDBMS_12.1_SOLARIS.SPARC64 oratop/oratop.pm
          ;;
          HP-UX)
            $RM -f oratop/oratop.RDBMS_11.2_HPUX.PARISC64 oratop/oratop.RDBMS_11.2_HPUX.IA64 oratop/oratop.RDBMS_12.1_HPUX.IA64 oratop/oratop.pm
            $jar_exe xf ext.jar oratop/oratop.RDBMS_11.2_HPUX.PARISC64 oratop/oratop.RDBMS_11.2_HPUX.IA64 oratop/oratop.RDBMS_12.1_HPUX.IA64 oratop/oratop.pm
          ;;
          *)     
          ;;
        esac
        $RM -f ext.jar
        $CHMOD a+x $tfa_home/ext 2>/dev/null;
        $CHMOD a+x $tfa_home/ext/oratop 2>/dev/null;
        $CHMOD a+x $tfa_home/ext/oratop/* 2>/dev/null;
        cd $c_loc
      fi
    else
      writelog "ORATOP-WARNING: Unable to find JAVA_HOME"
    fi
  else
    writelog "ORATOP-WARNING: Unable to find tfa_home.";
  fi
}

# Function to Start TFA
start_tfa() {

	# Start TFA if its not running
	STARTTFA=1;

	if [ -f "$tfa_home/internal/.pidfile" ]
	then
		TFAPID=`$CAT $tfa_home/internal/.pidfile`;

		if [ -n "$TFAPID" ]
		then
			TFAPID=`$PS -f -p $TFAPID | $GREP -v PID | $AWK '{print $2}'`

			if [ ! -z "$TFAPID" ]
			then
				STARTTFA=0;
			fi
		fi
	fi

	if [ "$STARTTFA" -eq 1 ]
	then
		$ID/init.tfa start
		writelog "";
	fi
}

get_inv_file() {
  inv_file=""
  if [ -f "/etc/oraInst.loc" ]; then
    inv_loc=`$CAT /etc/oraInst.loc | $GREP -v "^#" | $GREP inventory_loc | $CUT -d= -f2`
  elif [ -f "/var/opt/oracle/oraInst.loc" ]; then
    inv_loc=`$CAT /var/opt/oracle/oraInst.loc | $GREP -v "^#" | $GREP inventory_loc | $CUT -d= -f2`
  fi
  if [ -n "$inv_loc" ] && [ -d "$inv_loc" ]; then
    if [ -f "$inv_loc/ContentsXML/inventory.xml" ]; then
      inv_file="$inv_loc/ContentsXML/inventory.xml"
    fi
  fi
  writelog "get_inv_file : inv_file set to $inv_file" "LOG"
}

get_crs_home() {
  CHOME=""
  OLR_LOC=""
  if [ -f "/etc/oracle/olr.loc" ]; then
    OLR_LOC="/etc/oracle/olr.loc"
  elif [ -f "/var/opt/oracle/olr.loc" ]; then
    OLR_LOC="/var/opt/oracle/olr.loc"
  fi
  if [ -n "$OLR_LOC" ]; then
    CHOME=`$CAT $OLR_LOC | $GREP crs_home | $AWK -F= '{print $2}'`;
  fi
  if [ -n "$CHOME" ] && [ -d "$CHOME" ]; then
    writelog "get_crs_home : CHOME set to $CHOME" "LOG";
    return
  fi
  get_inv_file
  if [ -n "$inv_file" ]; then
    CHOME=`$CAT  "$inv_file" | $GREP "CRS=\"true\"" | $GREP -v "REMOVED=\"T\"" | $AWK '{print $3}' | $CUT -d= -f2 | $SED 's/\"//g'`;
  fi
  if [ -n "$CHOME" ] && [ -d "$CHOME" ]; then
    writelog "get_crs_home : CHOME set to $CHOME" "LOG";
    return;
  fi
  writelog "get_crs_home : unable to determine CHOME" "LOG";
  return
}

set_perl() {
  get_crs_home;
  if [ -n "$CHOME" ] && [ -d "$CHOME" ] && [ -f "$CHOME/perl/bin/perl" ]; then
    PERL="$CHOME/perl/bin/perl";
    return; 
  fi
  OHOME=""
  get_inv_file
  if [ -n "$inv_file" ]; then
    for row in `$CAT "$inv_file" | $GREP LOC | $GREP -v "CRS=\"true\"" | $GREP -iv ASM | $GREP -vi agent | $GREP -v "REMOVED=\"T\"" | $AWK '{print $3}' | $CUT -d= -f2 | $SED 's/\"//g'`; do 
      OHOME=$row;
      break;
    done
  fi
  if [ -n "$OHOME" ] && [ -d "$OHOME" ] && [ -f "$OHOME/perl/bin/perl" ]; then
    PERL="$OHOME/perl/bin/perl";
  fi
}

checkPerlVersion() {
  PERL_VERSION=`$PERL -e 'print $];'`
  PERL_VERSION_INT=`$ECHO $PERL_VERSION | $SED "s/\.//g"`

  if [ "$PERL_VERSION_INT" -lt 5010000 ] 
  then 
    writelog "Perl Version : $PERL_VERSION";
    writelog "";
    writelog "TFA-00026: Oracle Trace File Analyzer (TFA) requires Perl 5 version 10 or later";
    exit 1;
  fi
}

checkPerlOwner() {
  PERL_OWNER=`$LS -ld $PERL | $AWK '{print $3}'`
  if [ "$PERL_OWNER" != "root" ] && [ "$RUID" -eq 0 ]
  then
    writelog "TFA-00027: Perl Home specified is not root-owned";
    exit 1;
  fi
}

#
# Parse argument
#
  while [ $# -gt 0 ]
  do
    case $1 in
      -silent)  SILENT=$1;;         # silent is set to true
      -tfabase) shift; TFABASE=$1; export TFABASE;;  # TFABASE is set
      -crshome) shift; CRSHOME=$1; export CRSHOME;;  # CRSHOME is set
      -ohome) shift; ORAHOME=$1; export ORAHOME; ORAHOMEFLAG="-ohome";;  # DBHOME is set
      -javahome) shift; JAVA_HOME=$1; TFA_JAVA=$1; export JAVA_HOME;; # JAVA_HOME is set
      -receiver) MODE=$1;; #receiver mode is set
      -deferdiscovery) DEFERDISCOVERY=$1;;   # DEFERDISCOVERY  is set
      -local) LOCALONLY=$1;;   # LOCALONLY  is set
      -patch) PATCH=$1;;       # Internal flag
      -ade) TFA_ADE=$1; export TFA_ADE;;       # Internal flag
      -debug) INSTDEBUG=$1; TFA_DEBUG=7;export TFA_DEBUG;;   # DEBUG  is set
      -discoveryfile) shift; DISCOVERYFILE=$1;; #values can be sh or pl
      -extractto) shift; EXTRACTTO=$1; export EXTRACTTO;; # EXTRACTTO is set
      -tmploc) shift; TMPLOCDIR=$1; export TMPLOCDIR;; # TMPLOCDIR is set
      -perlhome) shift; PERLHOME=$1; export PERLHOME;; #PERLHOME is set
      -help) HELP=$1;;   # print the help and exit
      -h) HELP=$1;;   # print the help and exit
       *) writelog "   TFA-00003: Invalid Option $1 for TFA"; printhelp ; exit 1

    esac;
    shift
  done

if [ -n "$HELP" ]; then 
   printhelp
   exit 1
fi

if [ -n "$PERLHOME" ] 
then
  if [ -d "$PERLHOME" ] && [ -f "$PERLHOME/bin/perl" ]
  then 
    PERL="$PERLHOME/bin/perl";
    checkPerlVersion
    checkPerlOwner
  else
    writelog "TFA-00028: Invalid Perlhome: $PERLHOME"
    exit 1;
  fi
fi

#Temporary fix in ADE env
if [ -n "$TFA_ADE" ] || [ -n "$ADE_VIEW_ROOT" ]
then
     DISCOVERYFILE="sh"
fi
# End Temporary fix in ADE env

if [ -n "$EXTRACTTO" ]; then
   if [ ! -d "$EXTRACTTO" ]; then
      $MKDIR -p "$EXTRACTTO";
      if [ ! -d "$EXTRACTTO" ]; then
         writelog "TFA-00020: Provided TFA Extract Directory $EXTRACTTO could not be created."
         writelog "Please verify that the current user has the right permissions."
         exit 1
      fi
   fi
   if [ -n "$SILENT" ] || [ -n "$TMPLOCDIR" ] || [ -n "$TFABASE" ] || [ -n "$CRSHOME" ] || [ -n "$ORAHOME" ] || [ -n "$DEFERDISCOVERY" ] || [ -n "$LOCALONLY" ] ; then
      writelog "TFA-00021: -extractto switch can only be combined with -javahome or -debug switches"
      exit 1
   fi
fi

if [ -n "$TMPLOCDIR" ]; then
   if [ ! -d "$TMPLOCDIR" ]; then
      writelog "TFA-00025: Provided TFA Temporary Location Directory $TMPLOCDIR does not exist."
      writelog "Please ensure the directory is created prior to running."
      exit 1
  fi
else
  TMPLOCDIR="/tmp"
fi

if [ -n "$DISCOVERYFILE" ] # DISCOVERYFILE was supplied 
then
    writelog "DISC FLAG activated $DISCOVERYFILE"
    if [ "$DISCOVERYFILE" != "pl" ] && [ "$DISCOVERYFILE" != "sh" ] # DISCOVERYFILE was supplied 
    then
      echo "TFA-00018: Improper discoveryfile flag value. Allowed: pl/sh."
      exit 1;
    fi
    DISCOVERYFILEFLAG="-discoveryfile $DISCOVERYFILE"
fi

if [ -n "$TFA_ADE" ]
then
	$CHMOD 644 $LOGFILE;
fi

if [ -n "$TFABASE" ] && [ ! -d "$TFABASE" ]; then
   writelog "TFA-00004: Provided TFA Base Directory $TFABASE does not exist"
   exit 1
fi

if [ -n "$CRSHOME" ]  && [ ! -d "$CRSHOME" ]; then
   writelog "TFA-00005: Provided CRS_HOME directory $CRSHOME does not exist"
   exit 1
fi

if [ -n "$ORAHOMEFLAG" ] && [ -z "$ORAHOME" ] ; then
  ORAHOME=`$ECHO $0 | $SED 's/\/crs\/install.*//'`;
fi

if [ -n "$ORAHOMEFLAG" ]  && [ ! -d "$ORAHOME" ]; then
   writelog "TFA-00006: Provided ORACLE_HOME directory $ORAHOME does not exist"
   exit 1
fi

if [ -n "$ORAHOME" ] && [ -f "$ORAHOME/bin/orabase" ] ; then
  ORABASE=`ORACLE_HOME=$ORAHOME; export ORACLE_HOME;$ORAHOME/bin/orabase`
fi

if [ -n "$ORAHOMEFLAG" ]  && [ ! -d "$ORABASE" ]; then
   writelog "TFA-00007: ORACLE_BASE directory '$ORABASE' does not exist"
   exit 1
fi

if [ -n "$CRSHOME" ] || [ -n "$ORAHOME" ] ; then 
   if [ -n "$TFABASE" ] || [ -n "$TFA_JAVA" ] || [ -n "$DEFERDISCOVERY" ] || [ -n "$LOCALONLY" ]; then 
      writelog "TFA-00009: Invalid Parameters. Do not use any other parameters with -crshome or -ohome"
       printhelp
       exit 1
   fi
fi

if [ -n "$CRSHOME" ]  && [ -d "$CRSHOME/jdk/jre" ]; then
	TFA_JAVA="$CRSHOME/jdk/jre";
	JAVA_HOME="$TFA_JAVA";
	export JAVA_HOME;
fi

if [ -n "$ORAHOME" ]  && [ -d "$ORAHOME/jdk/jre" ]; then
	TFA_JAVA="$ORAHOME/jdk/jre";
	JAVA_HOME="$TFA_JAVA";
	export JAVA_HOME;
fi

if [ -n "$TFA_JAVA" ] && [ ! -x "$TFA_JAVA/bin/java" ]; then
   writelog "TFA-00008: JAVAHOME supplied does not have $TFA_JAVA/bin/java executable"
   exit 1
fi

# Check Java Version
if [ -n "$TFA_JAVA" ]
then
	if [ ! -n "$CRSHOME" ] && [ ! -n "$ORAHOME" ]
	then
		JAVA_VERSION=`getJavaVersion "$TFA_JAVA/bin/java"`;
		JAVA_NUM=`$ECHO $JAVA_VERSION | $SED "s/\.//g"`;

		if [ "$JAVA_NUM" -lt "18" ]
		then
			writelog "Java Version : $JAVA_VERSION";
			writelog "";
			writelog "TFA-00008: Oracle Trace File Analyzer (TFA) requires Java version 1.8 or later";
			exit 1;
		fi
	fi
fi

if [ -n "$TFA_JAVA" ] && [ ! -x "$TFA_JAVA/bin/keytool" ]; then
  out=`perl -e "use Cwd 'abs_path'; print abs_path(\"$TFA_JAVA/bin/java\");"`
  out=`echo $out |sed -n '1s/bin\/java$//p'`
  if [ ! -x "$out/bin/keytool" ]; then
    writelog "TFA-00008: JAVAHOME supplied does not have $out/bin/keytool executable"
    exit 1
  fi
  TFA_JAVA=$out
  JAVA_HOME=$out
  export JAVA_HOME
fi

if [ -n "$CRSHOME" ]  && [ ! -n "$SILENT" ]; then # -crshome is always a silent install
   SILENT="-silent"
fi

if [ -n "$CRSHOME" ]  && [ ! -n "$LOCALONLY" ]; then # -crshome is always a local install
   LOCALONLY="-local"
fi

if [ -n "$ORAHOME" ] ; then
  SILENT="-silent"
  LOCALONLY="-local"
fi

if [ -n "$CRSHOME" ]
then
	if [ `$ECHO $CRSHOME | $GREP -c "/$"` -eq 1 ]
	then
		CRSHOME=`$ECHO $CRSHOME | $SED 's/\/$//'`;
	fi

	if [ ! -f "$CRSHOME/crs/install/crsconfig_params" ]
	then
		writelog "TFA-00010: Provided CRS_HOME directory $CRSHOME is not valid. Please verify once again.";
		exit 1;
	fi

	REL_DIR="$CRSHOME/suptools/tfa/release";
  if [ -z "$PERLHOME" ]
  then
	  PERL="$CRSHOME/perl/bin/perl";
  fi

        if [ `$GREP -c "CLUSTER_CLASS=DOMAINSERVICES" $CRSHOME/crs/install/crsconfig_params` -gt 0 ]
        then # Start TFA in receiver mode on domain cluster
          MODE="-receiver"
        fi

	# If location of unzip is not found then use unzip from CRSHOME
	if [ `$ECHO $UNZIP | $GREP -c "^unzip$"` -eq 1 ]
	then
		if [ -f "$CRSHOME/bin/unzip" ]
		then
			UNZIP="$CRSHOME/bin/unzip";
		else
			writelog "TFA-00015: Unable to find unzip. Please ensure it is in your PATH and try again.";
			exit 1;
		fi
	fi
fi

if [ -n "$ORAHOME" ]
then
        if [ `$ECHO $ORAHOME | $GREP -c "/$"` -eq 1 ]
        then
                ORAHOME=`$ECHO $ORAHOME | $SED 's/\/$//'`;
        fi

        REL_DIR="$ORAHOME/suptools/tfa/release";
        if [ -z "$PERLHOME" ]
        then
      	   PERL="$ORAHOME/perl/bin/perl";
        fi
fi

#Try to use perl from CRS/ORA Home if available
if [ `getCommandLocation "perl"` = "$PERL" ] && [ -z "$PERLHOME" ]
then
  set_perl;
fi

HOSTNAME=`hostname | $CUT -d. -f1 | $PERL -ne 'print lc'`

if [ -z "$HOSTNAME" ] || [ `$ECHO $HOSTNAME | $GREP -c "^(none)$"` -ge 1 ]
then
	writelog "TFA-00019: Invalid Hostname [$HOSTNAME]. Please update hostname and try again."
	exit 1;
fi

writelog ""
if [ -n "$MODE" ] && [ `$ECHO $MODE | $GREP -c '\-receiver'` -gt 0 ]; then
 writelog "Starting TFA installation in receiver mode"
else
 writelog "Starting TFA installation"
fi
writelog ""
writelog "TFA Version: $BUILD_VERSION Build Date: $BUILD_DATE";
writelog ""

# Firstly check if we are root .. If not then stop here.

if [ "$RUID" -ne 0 ] && [ -z "$EXTRACTTO" ]
then
   writelog "TFA-00011: Oracle Trace File Analyzer (TFA) must be run as the root user"
   exit 1
fi

# If we are patching then we do not to do a number of the following checks.
# If tfa is already installed just patch
TFA_INSTALLED=0;
INSTALLED_BUILD=0;
INSTALLED_BLDDT=0;
INSTALLED_BLDVR=0;

# Below varables are used to remove Old TFA after Install.
OLD_TFA_HOME="";
REMOVE_OLD_TFA=0;

writelog "About to check previous TFA installations ..." "LOG"

if [ -r "$ID/init.tfa" -a -z "$EXTRACTTO" ] || [ -n "$EXTRACTTO" -a -r "$EXTRACTTO/tfa/tfa_home/bin/tfactl" ]
then
        if [ -z "$EXTRACTTO" ]
        then    
               tfa_home=`$GREP '^export TFA_HOME=' $ID/init.tfa | $AWK -F"=" '{print $2}'`
        else    
               tfa_home=$EXTRACTTO/tfa/tfa_home
        fi

  	if [ ! -n "$tfa_home" ]; then
    		tfa_home=`$GREP '^TFA_HOME=' $ID/init.tfa | $AWK -F"=" '{print $2}'`
  	fi

	writelog "TFA HOME : $tfa_home";
	writelog "";

  	BASEDIR=`$ECHO $tfa_home | $SED -e "s/\/[^\/]*$//"`

  	if [ -r "$tfa_home/bin/tfactl" ]
        then # TFA is already installed
          if [ -n "$CRSHOME" ] # And we are doing a TFA install in CRS
          then
            if [ `$GREP -c "CLUSTER_CLASS=DOMAINSERVICES" $CRSHOME/crs/install/crsconfig_params` -gt 0 ] # And its a DSC
            then
              if [ `$GREP -c "RUN_MODE=receiver" $tfa_home/tfa_setup.txt` -eq 0 ] # And we have a TFA running as collector
              then
                writelog "New home $CRSHOME is a DSC cluster.";
                writelog "Removing TFA - Collector Setup at $tfa_home";
                $tfa_home/bin/uninstalltfa -local -silent -crshome ""
                sleep 20
              fi
            fi
          fi
        fi
  	if [ -r "$tfa_home/bin/tfactl" ]
  	then
		TFA_INSTALLED=1;

		if [ -f "$tfa_home/internal/.buildid" ]
		then
			INSTALLED_BUILD=`$CAT $tfa_home/internal/.buildid`;

			# Extract Installed Build Version and Date
			INSTALLED_BLDDT=`$ECHO $INSTALLED_BUILD | $TAIL -15c`;
			INSTALLED_BLDVR=`$ECHO $INSTALLED_BUILD | $SED "s/$INSTALLED_BLDDT//"`;

			# Get Build Version from .buildversion
			if [ -f "$tfa_home/internal/.buildversion" ]
			then
				INSTALLED_BLDVR=`$CAT $tfa_home/internal/.buildversion`;
			fi

			# If Installed version is null then set it to 0
			if [ ! -n "$INSTALLED_BLDVR" ]
			then
				INSTALLED_BLDVR=0;
			fi

			# Remove Seconds from Installed Build Date
			INSTALLED_BLDDT=`$ECHO $INSTALLED_BLDDT | $CUT -c 1-12`;

			writelog "Installed Build Version: $INSTALLED_BLDVR Build Date: $INSTALLED_BLDDT";
			writelog "";
		fi

		# Get OLD INSTALL_TYPE
		INSTALL_TYPE="TYPICAL";
		if [ -f "$tfa_home/tfa_setup.txt" ]
		then
			INSTALL_TYPE=`$GREP '^INSTALL_TYPE=' $tfa_home/tfa_setup.txt | $AWK -F"=" '{print $2}'`;

			# If INSTALL_TYPE is null
			if [ ! -n "$INSTALL_TYPE" ]
			then
				OLD_CRSHOME=`$GREP '^CRS_HOME=' $tfa_home/tfa_setup.txt | $AWK -F"=" '{print $2}'`;

				if [ -n "$OLD_CRSHOME" ] && [ `$ECHO $tfa_home | $GREP -c $OLD_CRSHOME` -eq 1 ]
				then
					INSTALL_TYPE="GI";
				else
					INSTALL_TYPE="TYPICAL";
				fi
			fi
		fi
                writelog "INSTALL_TYPE $INSTALL_TYPE" "LOG"

		# Check and see its a new TFA_HOME for GI Install
		if [ -n "$CRSHOME" ] && [ `$ECHO $tfa_home | $GREP -c $CRSHOME` -eq 0 ] && [ "$INSTALL_TYPE" = "GI" ]
		then
			#Shutdown TFA if it is running
			if [ `$PS -ef | $GREP "$ID/init.tfa" | $GREP -v grep | $WC -l` -ge 1 ]
			then
				writelog  "Shutting down TFA for Migration...";
				$tfa_home/bin/tfactl stop_suptools > /dev/null 2>&1;
				$ID/init.tfa shutdown > /dev/null 2>&1;
				writelog "";
			fi

			# Remove init.tfa
			if [ -f "$ID/init.tfa" ]
			then
				writelog "Removing $ID/init.tfa...";
				$RM -f $ID/init.tfa;
				writelog "";
			fi

			writelog "Migrating TFA to $CRSHOME/tfa/$HOSTNAME/tfa_home...";

			OLD_TFA_HOME="$tfa_home";
			NEW_TFA_HOME="$CRSHOME/tfa/$HOSTNAME/tfa_home";

			# Get TFA_BASE
			OLD_TFA_BASE=`$ECHO $OLD_TFA_HOME | $SED "s/\/$HOSTNAME\/tfa_home//"`;

			# Get NEW ORACLE_BASE
			if [ -f "$CRSHOME/crs/install/crsconfig_params" ]
			then
				ORACLE_BASE=`$GREP '^ORACLE_BASE=' $CRSHOME/crs/install/crsconfig_params | $AWK -F"=" '{print $2}'`;
			else
				writelog "TFA-00010: $CRSHOME/crs/install/crsconfig_params not found. Please verify once again.";
				exit 1;
			fi

			# Get OLD CRS_HOME
			if [ -f "$OLD_TFA_HOME/tfa_setup.txt" ]
			then
				OLD_CRSHOME=`$GREP '^CRS_HOME=' $tfa_home/tfa_setup.txt | $AWK -F"=" '{print $2}'`;
			fi

			# Get OLD ORACLE_BASE
			if [ -f "$OLD_TFA_HOME/tfa_setup.txt" ]
			then
				OLD_ORACLE_BASE=`$GREP '^ORACLE_BASE=' $tfa_home/tfa_setup.txt | $AWK -F"=" '{print $2}'`;
			fi

			if [ ! -d "$CRSHOME/tfa/$HOSTNAME" ]
			then
				$MKDIR -p "$CRSHOME/tfa/$HOSTNAME";
			fi

			if [ ! -d "$ORACLE_BASE/tfa/$HOSTNAME" ]
			then
				$MKDIR -p "$ORACLE_BASE/tfa/$HOSTNAME";
			fi

			# Copy file to new GI HOME
			if [ -n "$OLD_TFA_BASE" ]
			then
				$CP -rfp $OLD_TFA_BASE/$HOSTNAME $CRSHOME/tfa/;
				$CP -rfp $OLD_TFA_BASE/bin $CRSHOME/tfa/;

				# Check ORACLE_BASE before copying
				if [ "$OLD_ORACLE_BASE" != "$ORACLE_BASE" ]
				then
					for dir in `$LS $OLD_ORACLE_BASE/tfa/$HOSTNAME`
					do
						if [ "$dir" != "log" ]
						then
							$CP -rfp $OLD_ORACLE_BASE/tfa/$HOSTNAME/$dir $ORACLE_BASE/tfa/$HOSTNAME/;
						else
							$MKDIR -p "$ORACLE_BASE/tfa/$HOSTNAME/log";
						fi
					done
				fi
			fi

			# Update CRS_HOME in tfa_setup
			if [ `$CAT $NEW_TFA_HOME/tfa_setup.txt | $GREP -c "^CRS_HOME="` -ge 1 ]
			then
				$PERL -p -i -e "s{^CRS_HOME=.*}{CRS_HOME=$CRSHOME}g" $NEW_TFA_HOME/tfa_setup.txt;
			else 
				$ECHO "CRS_HOME=$CRSHOME" >> $NEW_TFA_HOME/tfa_setup.txt;
			fi

			# Update ORACLE_BASE in tfa_setup
			if [ `$CAT $NEW_TFA_HOME/tfa_setup.txt | $GREP -c "^ORACLE_BASE="` -ge 1 ]
			then
				$PERL -p -i -e "s{^ORACLE_BASE=.*}{ORACLE_BASE=$ORACLE_BASE}g" $NEW_TFA_HOME/tfa_setup.txt;
			else 
				$ECHO "ORACLE_BASE=$ORACLE_BASE" >> $NEW_TFA_HOME/tfa_setup.txt;
			fi

			# Update INSTALL_TYPE in tfa_setup
			if [ `$CAT $NEW_TFA_HOME/tfa_setup.txt | $GREP -c "^INSTALL_TYPE="` -ge 1 ]
			then
				$PERL -p -i -e "s{^INSTALL_TYPE=.*}{INSTALL_TYPE=GI}g" $NEW_TFA_HOME/tfa_setup.txt;
			else 
				$ECHO "INSTALL_TYPE=GI" >> $NEW_TFA_HOME/tfa_setup.txt;
			fi

			# Update JAVA_HOME in tfa_setup
			OLD_JAVA_HOME=`$GREP '^JAVA_HOME=' $NEW_TFA_HOME/tfa_setup.txt | $AWK -F"=" '{print $2}'`;
			if [ -n "$OLD_JAVA_HOME" ]
			then
				if [ `$ECHO $OLD_JAVA_HOME | $GREP -c "$OLD_CRSHOME"` -eq 1 ]
				then
					$PERL -p -i -e "s{^JAVA_HOME=.*}{JAVA_HOME=$CRSHOME/jdk/jre}g" $NEW_TFA_HOME/tfa_setup.txt;
				fi
			else 
				$ECHO "JAVA_HOME=$CRSHOME/jdk/jre" >> $NEW_TFA_HOME/tfa_setup.txt;
			fi

			# Update PERL in tfa_setup
			if [ `$CAT $NEW_TFA_HOME/tfa_setup.txt | $GREP -c "^PERL="` -ge 1 ]
			then
				$PERL -p -i -e "s{^PERL=.*}{PERL=$CRSHOME/perl/bin/perl}g" $NEW_TFA_HOME/tfa_setup.txt;
			else 
				$ECHO "PERL=$CRSHOME/perl/bin/perl" >> $NEW_TFA_HOME/tfa_setup.txt;
			fi
			
			$PERL -p -i -e "s{^TFA_HOME=.*}{TFA_HOME=$NEW_TFA_HOME}g" $NEW_TFA_HOME/install/init.tfa;
			$PERL -p -i -e "s{^TFA_HOME=.*}{TFA_HOME=$NEW_TFA_HOME}g" $NEW_TFA_HOME/bin/tfactl;
			$PERL -p -i -e "s{^TFA_HOME=.*}{TFA_HOME=$NEW_TFA_HOME}g" $CRSHOME/tfa/bin/tfactl;

			$TOUCH "$ORACLE_BASE/tfa/.$HOSTNAME.shared";

			$CP -f $NEW_TFA_HOME/bin/tfactl $CRSHOME/bin;
			$CP -f $NEW_TFA_HOME/install/init.tfa $ID/;

			writelog "";
			writelog "Starting TFA on $HOSTNAME...";
			writelog "";
			$ID/init.tfa start > /dev/null 2>&1;

			# Repository Changes
			if [ "$OLD_ORACLE_BASE" != "$ORACLE_BASE" ]
			then

				if [ -f "$OLD_TFA_HOME/internal/.reposloc.dmp" ]
				then
					OLD_REPOSITORY=`$CAT $OLD_TFA_HOME/internal/.reposloc.dmp`;
				fi

				if [ -n "$OLD_REPOSITORY" ]
				then

					if [ "$OLD_REPOSITORY" = "$OLD_ORACLE_BASE/tfa/repository" ]
					then
						NEW_REPOSITORY="$ORACLE_BASE/tfa/repository";

						if [ ! -d "$NEW_REPOSITORY" ]
						then
							$MKDIR -p $NEW_REPOSITORY;
						fi

						if [ "$PLATFORM" = "SunOS" ]
						then
							SPACE=`$DF -k $NEW_REPOSITORY | $TAIL -1 | $AWK '{ print $4 }'`;
						else
							SPACE=`$DF -Pk $NEW_REPOSITORY | $TAIL -1 | $AWK '{ print $4 }'`;
						fi

						SPACE=`$EXPR $SPACE / 1024`;

						if [ "$SPACE" -ge "1024" ]
						then
							$NEW_TFA_HOME/bin/tfactl set repositorydir=$ORACLE_BASE/tfa/repository -force;
						fi
					fi
				fi
			fi

			$NEW_TFA_HOME/bin/tfactl print status;
			tfa_home="$NEW_TFA_HOME";
  			BASEDIR=`$ECHO $tfa_home | $SED -e "s/\/[^\/]*$//"`
			writelog "";
		fi

		# If Upgrade is in DSC from 12.2.0.1 (Instaled as collector) to later 
                # versions or C to R move, add RUN_MODE=reeciver
		if [ `$GREP -c "^RUN_MODE=" $tfa_home/tfa_setup.txt` -eq 0 ]
		then
			if [ -n "$CRSHOME" ] && [ `$GREP -c "CLUSTER_CLASS=DOMAINSERVICES" $CRSHOME/crs/install/crsconfig_params` -gt 0 ]
			then
				UPDATE_RUN_MODE="1";
			fi

			if [ -n "$MODE" ] || [ -n "$UPDATE_RUN_MODE" ]
			then
				$ECHO "RUN_MODE=receiver" >> $tfa_home/tfa_setup.txt;
			fi
                fi

		# Check Build Version
		if [ "$BUILD_VERSION" -ge "$INSTALLED_BLDVR" ]
		then
			# Upgrade only if :
			# 1. Build version is greater than installed build version
			# 2. Build date is greater than installed build date when build versions are same - Bug 25054308 
			if [ "$BUILD_VERSION" -eq "$INSTALLED_BLDVR" ] 
			then
				if [ "$BUILD_DATE" -le "$INSTALLED_BLDDT" ]
				then
					writelog "TFA-00022: TFA is already running latest version. No need to patch.";
					writelog "";

					# Start TFA if its not running
					if [ -n "$CRSHOME" ]
					then
						start_tfa;
					fi

					exit 0;
				fi
			fi
		else
			writelog "TFA-00023: TFA is already running latest version. No need to patch.";
			writelog "";

			# Start TFA if its not running
			if [ -n "$CRSHOME" ]
			then
				start_tfa;
			fi

			exit 0;

		fi   # END of CHECK BUILD_VERSION
	else
		#No TFA Binaries. Remove init.tfa and do fresh install.
		writelog "";
		writelog "TFA-00024: Not able to find TFA binaries in $tfa_home.";
		$RM -f $ID/init.tfa

  	fi  #END of tfactl

fi   #END of init.tfa

if [ "$TFA_INSTALLED" -eq "1" ]
then
	if [ -f "$READLINK" ]
	then
		TFA_INSTALLER=`$READLINK -f $SCRIPT`;		
	else
		if [ -f "$BASENAME" ] && [ -f "$DIRNAME" ]
		then
			SCRIPTNAME=`$BASENAME $SCRIPT`;
			cd `$DIRNAME $SCRIPT`;
			SCRIPTDIR=`$PWD_PATH -P`;
			TFA_INSTALLER="$SCRIPTDIR/$SCRIPTNAME";
		fi		
	fi

	if [ -n "$TFA_INSTALLER" ] && [ -f "$TFA_INSTALLER" ]
	then
		TFA_INSTALLER=$TFA_INSTALLER;
                export TFA_INSTALLER;
	fi

        writelog "TFA_INSTALLER $TFA_INSTALLER" "LOG"
fi

# archive required for patching and not patching
ARCHIVE=`$AWK '/^__ARCHIVE_BELOW__/ {print NR + 1; exit 0; }' $0`

if [ -n "$INSTDEBUG" ]
then
     writelog "TFA_INSTALLED : $TFA_INSTALLED";
fi

#Only do the basedir and crs_up checks if we are doing a new install.
if [ "$TFA_INSTALLED" -eq "0" ]
then
   # And more over don;t do them if we are passed the -crshome as CRS is unlikely to be up and we know where the BASE is .
   if [ -n "$CRSHOME" ] # CRSHOME was supplied 
   then
   	BASEDIR=$CRSHOME/tfa
        CRSHOMEFLAG="-crshome $CRSHOME"

	if [ -d "$CRSHOME/tfa" ]
	then
		writelog "Removing existing TFA $CRSHOME/tfa";
		writelog "";
		$RM -rf $CRSHOME/tfa;
	fi

	ORACLE_BASE=`$GREP '^ORACLE_BASE=' $CRSHOME/crs/install/crsconfig_params | $AWK -F"=" '{print $2}'`;

	if [ -n "$ORACLE_BASE" ] && [ -d "$ORACLE_BASE/tfa" ]
	then
		writelog "Removing existing $ORACLE_BASE/tfa";
		writelog "";
		$RM -rf $ORACLE_BASE/tfa;
	fi

	$MKDIR -p $BASEDIR;

   elif [ -n "$ORAHOME" ] ; then
        BASEDIR=$ORABASE/tfa
        CRSHOMEFLAG="-ohome $ORAHOME -obase $ORABASE"
        if [ ! -d "$BASEDIR" ]
        then
          $MKDIR -p $BASEDIR;
        fi 
   else
      # Check if CRS is up
      check_crs=`$PS -ef | $GREP -ic "d.bin"`
      if [ ${check_crs} -ge 4 ]
      then
        crs_up=1
      else
        crs_up=0
        if [ -n "$SILENT" ]
        then
          if [ ! -n "$TFABASE" ] || [ ! -n "$TFA_JAVA" ]
          then
            writelog "TFA-00013: CRS must be running for Silent Mode install. Exiting Installation";
            exit 1
          fi
        fi
      fi

      if [ -n "$TFABASE" ]; then # We supplied the install dir
         BASEDIR=$TFABASE
      elif [ -n "$EXTRACTTO" ]; then # Extraction dir was supplied
         BASEDIR=$EXTRACTTO
         TFA_INSTALLED=0
      else
         BASEDIR=`$PWD_PATH`
      fi

      # Check if default BASEDIR ends with /tfa. If not add /tfa to BASEDIR.
      tfa_dir="/tfa";
      root_dir="/";

      if [ `$ECHO $BASEDIR | $GREP -c "$tfa_dir$"` -eq 0 ] && [ `$ECHO $BASEDIR | $GREP -c $tfa_dir$root_dir$` -eq 0 ]
      then
              # Check if the BASEDIR ends with /
              if [ `$ECHO $BASEDIR | $GREP -c "$root_dir$"` -eq 1 ]
              then
                      BASEDIR=$BASEDIR"tfa";
              else
                      BASEDIR=$BASEDIR$tfa_dir;
              fi
      fi

      # Check for ODA or ask user for install location and cd to that location

      if [ -f "/opt/oracle/oak/bin/oakcli" ]; then
       BASEDIR="/opt/oracle/tfa"
      elif [ -d "/opt/exadata_ovm" ]; then
	BASEDIR="/opt/oracle.tfa"
      else
        if [ ! -n "$EXTRACTTO" ] && [ ! -n "$TFABASE" ]  && [ ! -n "$SILENT" ];then
          writelog "Enter a location for installing TFA (/tfa will be appended if not supplied) [$BASEDIR]: "
          read basedir
          if [ -n "$basedir" ]; then
            BASEDIR=$basedir;
          fi
        fi
      fi

      # Check if BASEDIR still ends with /tfa. If not add /tfa to BASEDIR.
      if [ `$ECHO $BASEDIR | $GREP -c "$tfa_dir$"` -eq 0 ] && [ `$ECHO $BASEDIR | $GREP -c $tfa_dir$root_dir$` -eq 0 ]
      then
    		# Check if the BASEDIR ends with /
		if [ `$ECHO $BASEDIR | $GREP -c "$root_dir$"` -eq 1 ] 
		then
			BASEDIR=$BASEDIR"tfa";
		else
			BASEDIR=$BASEDIR$tfa_dir;
   		fi
      fi
  
      # Create BASEDIR if it does not exist

      if [ ! -d "$BASEDIR" ]
      then
          	$MKDIR -p $BASEDIR;
      fi
   fi # End of if -n $CRSHOME 
fi # End of setup required just for a new install ie !TFA_INSTALLED

ZFILE=$BASEDIR/tfa_install.$$.zip

if [ "$PLATFORM" = "SunOS" ]
then
	tail +$ARCHIVE $0 > $ZFILE
else
	tail -n +$ARCHIVE $0 > $ZFILE
fi

export PERL;

# This will ensure TFA_HOME is valid just to avoid issues while patching
TFA_HOME="$tfa_home";
export TFA_HOME;

if [ -n "$INSTDEBUG" ]
then
     writelog "BASEDIR :  $BASEDIR"
     writelog "ZFILE :    $ZFILE"
     writelog "TFA_HOME : $TFA_HOME"
fi

cd $BASEDIR

if [ "$TFA_INSTALLED" -eq "1" ] ; then

  writelog "TFA is already installed. Patching $tfa_home...";
  PATCH_STATUS="1";
  TFAMAIN_STATUS="0";

  if [ -n "$CRSHOME" ] || [ -n "$ORAHOME" ] 
  then
	# Copy JRE and Perl for SI
	if [ -n "$ORAHOME" ]
	then
		$CP -rfL $ORAHOME/jdk/jre $tfa_home/;
		$CP -rfL $ORAHOME/perl $tfa_home/;
		JAVA_HOME="$tfa_home/jre";
		export JAVA_HOME;
		PERL="$tfa_home/perl/bin/perl";
		export PERL;

		# Update tfa_setup.txt
		if [ `$CAT $tfa_home/tfa_setup.txt | $GREP -c "^JAVA_HOME="` -ge 1 ]
		then
			$PERL -p -i -e "s{^JAVA_HOME=.*}{JAVA_HOME=$JAVA_HOME}g" $tfa_home/tfa_setup.txt;
		else
			$ECHO "JAVA_HOME=$JAVA_HOME" >> $tfa_home/tfa_setup.txt;
		fi

		if [ `$CAT $tfa_home/tfa_setup.txt | $GREP -c "^PERL="` -ge 1 ]
		then
			$PERL -p -i -e "s{^PERL=.*}{PERL=$PERL}g" $tfa_home/tfa_setup.txt;
		else
			$ECHO "PERL=$PERL" >> $tfa_home/tfa_setup.txt;
		fi
	fi

	#If the ZIP FILE Size > 0 use that else use release DIR.
	if [ -s "$ZFILE" ]
	then
                TMPLOC="$TMPLOCDIR/.$$.tfa"
                if [ -f "$TMPLOC" ] || [ -d "$TMPLOC" ]
                then
                    writelog "TFA-00024: temporary location for archive extract $TMPLOC exists - Install/upgrade cannot continue";  
                    exit 1;
                else
                    writelog "TFA patching CRS or DB from zipfile extracted to $TMPLOC"
		    $MKDIR $TMPLOC
		    cd $TMPLOC
		    $UNZIP -q $ZFILE
		    $TAR xfo tfahome_full.tar
		    $MV tfahome_full.tar tfa_home
                    writelog "TFA patching CRS or DB from zipfile is written to $BASEDIR/tfapatch.log"

		    # Check CRSHOME/ORAHOME JRE Version when JRE is not shipped with TFA
		    if [ ! -d "tfa_home/jre" ]
		    then
			JAVA_VERSION=`getJavaVersion "$TFA_JAVA/bin/java"`;
			JAVA_NUM=`$ECHO $JAVA_VERSION | $SED "s/\.//g"`;

			if [ "$JAVA_NUM" -lt "18" ]
			then
				writelog "Java Version : $JAVA_VERSION";
				writelog "";
				writelog "TFA-00008: Oracle Trace File Analyzer (TFA) requires Java version 1.8 or later";
				$RM -f $ZFILE;
				exit 1;
			fi
		    fi

		    tfa_home/bin/patchtfa.sh -local -silent | $TEE $BASEDIR/tfapatch.log
                fi
	
	elif [ -d "$REL_DIR" ]
	then
		cd $REL_DIR;
                writelog "TFA  patching CRS or DB from release directory is written to $BASEDIR/tfapatch.log"

		# Check CRSHOME/ORAHOME JRE Version
		JAVA_VERSION=`getJavaVersion "$TFA_JAVA/bin/java"`;
		JAVA_NUM=`$ECHO $JAVA_VERSION | $SED "s/\.//g"`;

		if [ "$JAVA_NUM" -lt "18" ]
		then
			writelog "Java Version : $JAVA_VERSION";
			writelog "";
			writelog "TFA-00008: Oracle Trace File Analyzer (TFA) requires Java version 1.8 or later";
			exit 1;
		fi

		$REL_DIR/tfa_home/bin/patchtfa.sh -local -silent | $TEE $BASEDIR/tfapatch.log
		cd $BASEDIR;
	else
		writelog "TFA-00014: Invalid TFA Installer. Please Contact Oracle Support for Help.";
		$RM -f $ZFILE;
		exit 1;
	fi
  else
	if [ -s "$ZFILE" ]  && [ -z "$EXTRACTTO" ]
	then
                TMPLOC="$TMPLOCDIR/.$$.tfa"
                if [ -f "$TMPLOC" ] || [ -d "$TMPLOC" ]
                then
                    writelog "TFA-00024: temporary location for archive extract $TMPLOC exists - Install/upgrade cannot continue";
                    exit 1;
                else
                    writelog "TFA patching CRS or DB from zipfile extracted to $TMPLOC"
		    $MKDIR $TMPLOC
  		    cd $TMPLOC
  		    $UNZIP -q $ZFILE
  		    $TAR xfo tfahome_full.tar
  		    $MV tfahome_full.tar tfa_home

                    writelog "TFA  patching typical install from zipfile is written to $BASEDIR/tfapatch.log"
  		    tfa_home/bin/patchtfa.sh $LOCALONLY $SILENT | $TEE $BASEDIR/tfapatch.log
                fi
        elif [ -s "$ZFILE" ] && [ -n "$EXTRACTTO" ]
        then
                writelog "About to upgrade the non-daemon installation ..." "LOG"
                tfa_home=$BASEDIR/tfa_home
                writelog "About to execute $UNZIP -q $ZFILE ..." "LOG"
                $UNZIP -q $ZFILE
                writelog "About to execute $TAR xfo tfahome_full.tar ..." "LOG"
                $TAR xfo tfahome_full.tar
                writelog "About to execute $MV tfahome_full.tar $tfa_home ..." "LOG"
                $MV tfahome_full.tar $tfa_home
                writelog "Running : $PERL tfa_home/bin/tfasetup.pl $SILENT $MODE $LOGFILE $DISCOVERYFILEFLAG" "LOG"
                writelog "Before calling tfasetup.pl ..." "LOG"
                writelog "SILENT $SILENT MODE $MODE LOGFILEPARAM $LOGFILEPARAM DISCOVERYFILEFLAG $DISCOVERYFILEFLAG" "LOG"
                $PERL tfa_home/bin/tfasetup.pl -extractto
                writelog "TFA patching non-daemon installation completed."
	else
		writelog "Unable to patch TFA...";
		writelog "";
		writelog "TFA-00014: Invalid TFA Installer. Please Contact Oracle Support for Help.";
		$RM -f $ZFILE;
		exit 1;
	fi
  fi

  #PATCH_STATUS=${PIPESTATUS[0]};

  if [ -f "/tmp/.tfa.patch" ]
  then
	PATCH_STATUS=`$CAT /tmp/.tfa.patch`;
        writelog "Patch Status : $PATCH_STATUS" "LOG"
	$RM -f "/tmp/.tfa.patch";
  fi

  # If its patched properly then generate cookie and copy buildid
  if [ "$PATCH_STATUS" -eq 0 ]
  then
        writelog "Patching OK : Running install_ext" "LOG"

        install_ext;

	cd $BASEDIR;

  	# We no longer support file for cookie.
  	# Remove it if its already present.
  	if [ -r "tfa_home/internal/.tfacookie" ]; then
    		$RM -f tfa_home/internal/.tfacookie
  	fi

  	# Check status of TFAMain
	$SH tfa_home/bin/tfactl -check > /dev/null 2>&1
	TFAMAIN_STATUS=$?;
  	
	if [ "$TFAMAIN_STATUS" -eq 1 ]
	then 
		#$ECHO "Setting TFA cookie clusterwide"
  		$SH tfa_home/bin/tfactl generatecookie

		# Sync TFA build files
		$SH tfa_home/bin/tfactl syncpatch
	
		#Start inventory after patch
		$SH tfa_home/bin/tfactl run inventory > /dev/null 2>&1
	fi

	# Copy tfactl to ORACLE_HOME/bin for DB Install
	if [ -n "$ORAHOME" ] && [ -f "$ORAHOME/suptools/tfa/release/tfa_home/bin/tfactl" ]
	then
		$CP -f $ORAHOME/suptools/tfa/release/tfa_home/bin/tfactl $ORAHOME/bin/tfactl;
	fi
  fi

  # Print Upgrade Status
  if [ ! -n "$PATCH" ]
  then
	$SH tfa_home/bin/tfactl print upgradestatus $BUILD_VERSION | $TEE -a $LOGFILE;
  fi

  $RM -rf $TMPLOC
  $RM -f $ZFILE
  $RM -f $BASEDIR/tfa_home/tfahome_full.tar

  if [ "$TFAMAIN_STATUS" -eq 1 ]
  then
  	exit 0;
  else
	exit 1;
  fi

else

  if [ -n "$INSTDEBUG" ]
  then
       writelog "No previous TFA installation."
  fi

  if [ -n "$CRSHOME" ] || [ -n "$ORAHOME" ] 
  then
	tfa_base=$BASEDIR/$HOSTNAME
	tfa_home=$tfa_base/tfa_home;
	if [ ! -d "bin" ]
	then
		$MKDIR bin;
		$CHMOD a+x bin;
	fi

	if [ ! -d "$tfa_base" ]
	then
		$MKDIR $tfa_base;
		$CHMOD a+x $tfa_base;
	fi

	cd $tfa_base

	# If the ZIP FILE Size > 0 use that else use release DIR.
	if [ -s "$ZFILE" ]
	then
		$UNZIP -q $ZFILE
		$TAR xfo tfahome_full.tar
		$MV tfahome_full.tar $tfa_home

	elif [ -d "$REL_DIR/tfa_home" ]
	then
		$CP -rfL $REL_DIR/tfa_home $tfa_base/
	else
		writelog "TFA-00014: Invalid TFA Installer. Please Contact Oracle Support for Help.";
		$RM -f $ZFILE;
		exit 1;
	fi

	# Check CRSHOME/ORAHOME Java Version
	if [ -d "$tfa_home/jre" ]
	then
		JAVA_HOME="$tfa_home/jre";
		export JAVA_HOME;
	else
		JAVA_VERSION=`getJavaVersion "$TFA_JAVA/bin/java"`;
		JAVA_NUM=`$ECHO $JAVA_VERSION | $SED "s/\.//g"`;

		if [ "$JAVA_NUM" -lt "18" ]
		then
			writelog "Java Version : $JAVA_VERSION";
			writelog "";
			writelog "TFA-00008: Oracle Trace File Analyzer (TFA) requires Java version 1.8 or later";
			$RM -f $ZFILE;
			$RM -rf $BASEDIR;
			exit 1;
		fi

		# Copy JRE for SI
		if [ -n "$ORAHOME" ]
		then
			$CP -rfL $ORAHOME/jdk/jre $tfa_home/;
			JAVA_HOME="$tfa_home/jre";
			export JAVA_HOME;
		fi
	fi

	# Copy Perl for SI
	if [ -n "$ORAHOME" ]
	then
		$CP -rfL $ORAHOME/perl $tfa_home/;
		PERL="$tfa_home/perl/bin/perl";
		export PERL;
	fi

 	writelog "Running : $PERL $tfa_home/bin/tfasetup.pl $SILENT $CRSHOMEFLAG $DEFERDISCOVERY $LOCALONLY $MODE $LOGFILE $DISCOVERYFILEFLAG" "LOG"
 	$PERL $tfa_home/bin/tfasetup.pl $SILENT $CRSHOMEFLAG $DEFERDISCOVERY $LOCALONLY $MODE $LOGFILEPARAM $DISCOVERYFILEFLAG

  elif [ -n "$EXTRACTTO" ]
  then
        if [ -n "$INSTDEBUG" ]
        then
             writelog "Switch -extractto used, doing the extraction ..."
        fi
        tfa_home=$BASEDIR/tfa_home
        $UNZIP -q $ZFILE
        $TAR xfo tfahome_full.tar
        $MV tfahome_full.tar $tfa_home
        writelog "Running : $PERL tfa_home/bin/tfasetup.pl $SILENT $MODE $LOGFILE $DISCOVERYFILEFLAG" "LOG"
        if [ -n "$INSTDEBUG" ]
        then
             writelog "Before calling tfasetup.pl ..."
             writelog "SILENT $SILENT MODE $MODE LOGFILEPARAM $LOGFILEPARAM DISCOVERYFILEFLAG $DISCOVERYFILEFLAG"
        fi
        $PERL tfa_home/bin/tfasetup.pl -extractto
        writelog "Completed : $PERL tfa_home/bin/tfasetup.pl -extractto" "LOG"
  elif [ ! -f "/opt/oracle/oak/bin/oakcli" ] && [ ! -d "/opt/exadata_ovm" ]
  then 
  	tfa_base=$BASEDIR/$HOSTNAME
  	tfa_home=$tfa_base/tfa_home
  	
	if [ ! -d "bin" ]
	then
		$MKDIR bin;
	  	$CHMOD a+x bin;
	fi

	if [ ! -d "$tfa_base" ]
	then
		$MKDIR $tfa_base;
		$CHMOD a+x $tfa_base;
	fi

	cd $tfa_base
	if [ -s "$ZFILE" ]
	then
		$UNZIP -q $ZFILE
		$TAR xfo tfahome_full.tar
		$MV tfahome_full.tar $tfa_home
		#$MV $tfa_home/bin/tfactl $BASEDIR/bin
		writelog "Running : $PERL $tfa_home/bin/tfasetup.pl $SILENT $CRSHOMEFLAG $DEFERDISCOVERY $LOCALONLY $MODE $LOGFILE $DISCOVERYFILEFLAG" "LOG"
		$PERL $tfa_home/bin/tfasetup.pl $SILENT $CRSHOMEFLAG $DEFERDISCOVERY $LOCALONLY $MODE $LOGFILEPARAM $DISCOVERYFILEFLAG
	else
		writelog "TFA-00014: Invalid TFA Installer. Please Contact Oracle Support for Help.";
		$RM -f $ZFILE;
		exit 1;
	fi
  else # This is an ODA so we never provide CRSHOME
  	tfa_home=$BASEDIR/tfa_home
	$UNZIP -q $ZFILE
	$TAR xfo tfahome_full.tar
	$MV tfahome_full.tar $tfa_home
	writelog "Running : $PERL $tfa_home/bin/tfasetup.pl $SILENT $MODE $LOGFILE $DISCOVERYFILEFLAG" "LOG"
	$PERL $tfa_home/bin/tfasetup.pl $SILENT $MODE $LOGFILEPARAM $DISCOVERYFILEFLAG
	writelog "Completed : $PERL $tfa_home/bin/tfasetup.pl $SILENT $MODE $LOGFILE $DISCOVERYFILEFLAG" "LOG"
  fi

fi

EXIT_STATUS=$?;
writelog "Exist status from tfasetup.pl : $EXIT_STATUS" "LOG"
TEMP_LOG_DIR="/tmp/tfa/`$DATE '+%Y%m%d_%H%M%S'`";

if [ $EXIT_STATUS -eq 0 ] ; then

  writelog "Running install_ext" "LOG"
  install_ext;
  writelog "Completed install_ext" "LOG"
  writelog "TFA is successfully installed...";
  
  # manuegar_extract_tfa_03
  if [ -n "$INSTDEBUG" ] && [ -n "$EXTRACTTO" ]
  then
        $PERL $tfa_home/bin/tfactl.pl -m 0x4008000 -v screen -setupnd -help
  elif [ -n "$EXTRACTTO" ]
  then
        $PERL $tfa_home/bin/tfactl.pl -setupnd -help
  else
	#Do not print help when called from root script or GI install
	SCRIPT_NAME=`basename $0`
	if [ "$SCRIPT_NAME" != "tfa_setup" ]; then
          $PERL $tfa_home/bin/tfactl -help
	fi
  fi

  if [ -n "$ORAHOME" ]
  then 
     if [ "$PLATFORM" = "Linux" ]
        then
            ohome_owner=`$STAT -L -c "%U" $ORAHOME/bin/oracle`
            ohome_group=`$STAT -L -c "%G" $ORAHOME/bin/oracle`
        else
            ohome_owner=`$LS -l $ORAHOME/bin/oracle|$AWK '{print $3}'`
            ohome_group=`$LS -l $ORAHOME/bin/oracle|$AWK '{print $4}'`
     fi

     if [ "$TFA_INSTALLED" -eq "1" ]  # We patched
     then
	 writelog "Writing $tfa_home and $BUILDID to $ORAHOME/install/.tfa.patched" "LOG"
         $ECHO TFA_HOME=$tfa_home > $ORAHOME/install/.tfa.patched
         $ECHO BUILD_VERSION=$BUILD_VERSION >> $ORAHOME/install/.tfa.patched
         $ECHO BUILD_DATE=$BUILD_DATE >> $ORAHOME/install/.tfa.patched
	 writelog "Changing owner $ohome_owner:$ohome_group for $ORAHOME/install/.tfa.patched" "LOG"
         $CHOWN $ohome_owner:$ohome_group $ORAHOME/install/.tfa.patched
     else
         writelog "Writing $tfa_home and $BUILDID to $ORAHOME/install/.tfa.configured" "LOG"
         $ECHO TFA_HOME=$tfa_home > $ORAHOME/install/.tfa.configured
         $ECHO BUILD_VERSION=$BUILD_VERSION >> $ORAHOME/install/.tfa.configured
         $ECHO BUILD_DATE=$BUILD_DATE >> $ORAHOME/install/.tfa.configured
	 writelog "Changing owner $ohome_owner:$ohome_group for $ORAHOME/install/.tfa.configured" "LOG"
         $CHOWN $ohome_owner:$ohome_group $ORAHOME/install/.tfa.configured
     fi

     # Copy tfactl to ORACLE_HOME/bin
     if [ -f "$ORAHOME/suptools/tfa/release/tfa_home/bin/tfactl" ]
     then
	$CP -f $ORAHOME/suptools/tfa/release/tfa_home/bin/tfactl $ORAHOME/bin/tfactl;
     fi 
  fi

  #Remove Old TFA Home if present
  if [ "$REMOVE_OLD_TFA" -eq 1 ]
  then
        if [ -d "$OLD_TFA_HOME" ]
        then
		writelog "Removing Old TFA $OLD_TFA_HOME...";
                $RM -rf $OLD_TFA_HOME;
        fi
  fi

  if [ -n "$CRSHOME" ]
  then # Copy credential file to tfa_home/internal/receiverconfig.xml
    if [ `$GREP -c "CLUSTER_CLASS=MEMBER" $CRSHOME/crs/install/crsconfig_params` -gt 0 ]
    then
      credfile_copied=0
      for credfile in `$GREP _CREDENTIALS= $CRSHOME/crs/install/crsconfig_params|$SED 's/.*_CREDENTIALS=//'`
      do
        if [ -f "$credfile" ]
        then
          $CP $credfile $tfa_home/internal/receiverconfig.xml
          $CHMOD 600 $tfa_home/internal/receiverconfig.xml
          credfile_copied=1
          break
        fi
      done
    fi
    if [ `$GREP -c "CLUSTER_CLASS=DOMAINSERVICES" $CRSHOME/crs/install/crsconfig_params` -gt 0 ]
    then
      $ECHO "CLUSTER_CLASS=DOMAINSERVICES" >> $tfa_home/tfa_setup.txt;
      $GREP CDATA_BACKUP_DISK_GROUP= $CRSHOME/crs/install/crsconfig_params | $TAIL -1 >> $tfa_home/tfa_setup.txt
      $TOUCH $tfa_home/internal/receiverconfig_self.xml
      $CP -f $tfa_home/bin/scripts/tfar_crs.sh $CRSHOME/tfa/bin/tfa_crs.sh
      $CP -f $tfa_home/bin/scripts/tfar_crs.sh $CRSHOME/tfa/bin/tfar_crs.sh
      $CHMOD 755 $CRSHOME/tfa/bin/tfa_crs.sh
      $CHMOD 755 $CRSHOME/tfa/bin/tfar_crs.sh
    fi
  fi
else
  writelog "Removing TFA Setup files..."

  #Move the TFA logs to /tmp

  LOG_DIR="$tfa_home/log";

  if [ -f "$tfa_home/tfa_setup.txt" ]
  then
	CRS_HOME=`$GREP '^CRS_HOME=' $tfa_home/tfa_setup.txt | $AWK -F"=" '{print $2}'`;
	ORACLE_BASE=`$GREP '^ORACLE_BASE=' $tfa_home/tfa_setup.txt | $AWK -F"=" '{print $2}'`;

	if [ -n "$CRS_HOME" ]
	then
		#Check if TFA_HOME is under CRS_HOME [ GI Install ]
		if [ `$ECHO $tfa_home | $GREP -ic "$CRS_HOME"` -eq 1 ]
		then
			if [ -n "$ORACLE_BASE" ]
			then
				LOG_DIR="$ORACLE_BASE/tfa/$HOSTNAME/log";
				BDB_DIR="$ORACLE_BASE/tfa/$HOSTNAME/database";
				OUT_DIR="$ORACLE_BASE/tfa/$HOSTNAME/output";
				SHARED_FILE="$ORACLE_BASE/tfa/.$HOSTNAME.shared";
				REPO_DIR="$ORACLE_BASE/tfa/repository";
			fi
		fi
	fi
  fi

  writelog "";

  LOG_COUNT=`$LS -1 $LOG_DIR | $WC -l`;

  if [ -d "$LOG_DIR" ] && [ -n "$LOG_COUNT" ] && [ "$LOG_COUNT" -ge "1" ]
  then

	for LOG_FILE in `$FIND $LOG_DIR -type f`
	do
		if [ -f "$LOG_FILE" ]
		then
			$ECHO "" >> $LOGFILE
			$ECHO "TFA Log File : $LOG_FILE" >> $LOGFILE
			$ECHO "" >> $LOGFILE
			$CAT $LOG_FILE >> $LOGFILE
		fi
	done

	if [ ! -d "$TEMP_LOG_DIR" ]
	then
		$MKDIR -p "$TEMP_LOG_DIR";
	fi

	writelog "Copying TFA logs to $TEMP_LOG_DIR";
	$CP -f $LOG_DIR/* $TEMP_LOG_DIR
	writelog "";
  else
	writelog "No TFA Logs in $LOG_DIR to copy to /tmp.";
	writelog "";
  fi

  # Shutdown TFA if it is running
  if [ `$PS -ef | $GREP "$ID/init.tfa" | $GREP -v grep | $WC -l` -ge 1 ]
  then
	writelog  "Shutting down TFA now...";
        writelog "";
        $ID/init.tfa shutdown
	writelog "";
  fi
   
  # CD to home directory before removing TFA files
  cd;

  # Do not remove TFA if we supply the -debug option
  if [ -n "$INSTDEBUG" ]
  then 
    	writelog "Leaving TFA_HOME in tact due to -debug flag: $tfa_home...";
  else
    	# Remove init.tfa
    	if [ -f "$ID/init.tfa" ]
    	then
  		writelog "Removing $ID/init.tfa...";
    		$RM -f $ID/init.tfa
    	fi

	# Remove TFA Log Directory
    	if [ -d "$LOG_DIR" ]
    	then
		writelog "Removing $LOG_DIR...";
		$RM -rf $LOG_DIR;
    	fi

	# Remove Shared File 
	if [ -n "$SHARED_FILE" ] && [ -f "$SHARED_FILE" ]
	then
		writelog "Removing $SHARED_FILE...";
		$RM -rf $SHARED_FILE;
	fi

	# Remove BDB in ORACLE_BASE
	if [ -n "$BDB_DIR" ] && [ -d "$BDB_DIR" ]
	then
		writelog "Removing $BDB_DIR...";
		$RM -rf $BDB_DIR;
	fi

	# Remove Output Directory in ORACLE_BASE
	if [ -n "$OUT_DIR" ] && [ -d "$OUT_DIR" ]
	then
		writelog "Removing $OUT_DIR...";
		$RM -rf $OUT_DIR;
	fi

	# Remove <ORACLE_BASE>/tfa/<NODE>
	if [ -n "$CRS_HOME" ]
	then
		OB_TFA="$ORACLE_BASE/tfa/$HOSTNAME";

		if [ -d "$OB_TFA" ]
		then
			writelog "Removing $OB_TFA...";
			$RM -rf $OB_TFA;
		fi
	fi

	# Remove TFA_HOME
    	if [ -d "$tfa_home" ]
    	then
  		writelog "Removing TFA_HOME $tfa_home...";
  		$RM -rf tfa_home/
    	fi

	# Remove TFA_BASE/HOSTNAME
    	if [ -d "$BASEDIR/$HOSTNAME" ]
    	then
		writelog "Removing $BASEDIR/$HOSTNAME...";
		$RM -rf $BASEDIR/$HOSTNAME
    	fi

	# Remove GIHOME/bin/tfactl
	if [ -n "$CRS_HOME" ] && [ -f "$CRS_HOME/bin/tfactl" ]
	then
		writelog "Removing $CRS_HOME/bin/tfactl...";
		$RM -f $CRS_HOME/bin/tfactl;
	fi
  fi

  # Remove repository if its empty.
  if [ -n "$CRSHOME" ]
  then
	if [ -n "$ORACLE_BASE" ]
	then
		REPO_DIR="$ORACLE_BASE/tfa/repository";
	else
		if [ -f "$tfa_base/ora_stack_status.out" ]
		then
			ORACLE_BASE=`$CAT $tfa_base/ora_stack_status.out | grep "^ORACLE_BASE" | awk -F"=" '{ print $2 }'`
			REPO_DIR="$ORACLE_BASE/tfa/repository";
		fi
	fi
  elif [ -f "/opt/oracle/oak/bin/oakcli" ] || [ -d "/opt/exadata_ovm" ]
  then
	REPO_DIR="$tfa_home/repository";
  else
	REPO_DIR="$BASEDIR/repository";
  fi

  if [ -d "$REPO_DIR" ]
  then
	if [ "$($LS -A $REPO_DIR)" ]; then
		writelog "Repository[$REPO_DIR] is not Empty to delete.";
	else
		writelog "Removing Repository $REPO_DIR";
		$RM -rf $REPO_DIR;
	fi
  fi 
fi

$RM -f $ZFILE
$RM -f $tfa_home/tfa_install*
$RM -f $tfa_home/tfahome_full.tar

writelog "";

dir="$tfa_home/log";
if [ -f "$tfa_home/tfa_setup.txt" ]
then
  INSTALL_TYPE=`$GREP '^INSTALL_TYPE=' $tfa_home/tfa_setup.txt | $AWK -F"=" '{print $2}'`;

  if [ "$INSTALL_TYPE" == "GI" ] || [ "$INSTALL_TYPE" == "DB" ]
  then
    ORACLE_BASE=`$GREP '^ORACLE_BASE=' $tfa_home/tfa_setup.txt | $AWK -F"=" '{print $2}'`;
    dir="$ORACLE_BASE/tfa/$HOSTNAME/log";
  fi
fi

if [ ! -d "$dir" ] 
then
  if [ ! -d "$TEMP_LOG_DIR" ]
  then
    $MKDIR -p "$TEMP_LOG_DIR";
  fi
  dir=$TEMP_LOG_DIR;
fi

$ECHO "Moving Install log file to $dir";
$MV $LOGFILE $dir;
$ECHO

exit $EXIT_STATUS

__ARCHIVE_BELOW__

OHA YOOOO