#!/bin/sh
#
# Copyright (c) 2001, 2004, Oracle. All rights reserved.  
#
#    DESCRIPTION
#    This is a wrapper script to opatch perl scripts to save the 
#    customer needing to find a version of perl.
#
#    NOTES: 1. This does make the assumption that Apache was installed
#              as part of Oracle and $ORACLE_HOME is set.
#           2. Assumes the version of perl is installed as "perl". If not
#              either edit this file or call it manually.
#           3. I'd have prefered /sbin/sh but it's not on all versions of 
#              linux and  AIX.
#           4. This _won't_ work on MicroSoft OSs (NT, W2k, etc.).
#           5. Instantiation of CLASSPATH is duplicated in the perl code.
#
#    BUGS
#    * <if you know of any bugs, put them here!>
#
#    MODIFIED   (MM/DD/YY)
#    phnguyen    10/07/04 - Touch up err msg if OH not set 
#    shgangul    10/07/03 - Iron out -oh and ORACLE_HOME issues
#    phnguyen    10/02/04 - If ORACLE_HOME not set but -oh is given,
#                              do not error out.
#    phnguyen    11/25/03 - Fix "perl not found" error message
#    shgangul    09/19/03 - Take care of ADE problem in opatch 
#    shgangul    09/12/03 - provide execute permission 
#    phnguyen    08/28/03 - phnguyen_installable 
#    phnguyen    07/24/03 - 
#    phnguyen    06/13/03 - v39: check error code 140 for new flag
#                               -error_on_conflict
#    phnguyen    06/06/03 - v38: return exit code to caller since
#                                EM patch script calls opatch now
#    phnguyen    05/21/03 - v33: add error code checking for arg. prob3l
#    phnguyen    05/16/03 - V 31: trivial wording change Cluster to RAC
#    phnguyen    05/14/03 - Version 30: more error code check
#                           for PATH and CLUSTER issues 
#    phnguyen    05/04/03 - Version 29: consolidate launching point
#                           of opatch.pl
#    phnguyen    04/29/03 - Version 28: check specific error code
#                           returned by opatch.pl
#                           Introduce ERROR_CODE 100 (inventory) and
#                             1 (Perl problem)
#    phnguyen    04/18/03 - Pump to version 1.0.0.0.26 
#    phnguyen    04/15/03 - Give 1st priority to $ORACLE_HOME/perl
#                             if it exists (being shipped with EM)
#    daevans     03/20/03 - Update for better instantiation at the 
#                           shell level.
#    daevans     10/28/02 - Fix for bug 2616267.
#    daevans     06/18/02 - Candidate release check-in.
#    daevans     05/31/02 - Initial source control check-in.
#    daevans     31/05/02 - Inital code.
########################################################################

BASE=`dirname $0`;

# Get ORACLE_HOME from environment variable "ORACLE_HOME"
OH=${ORACLE_HOME}

# Look for OPATCH_DEBUG env variable
DEBUG=${OPATCH_DEBUG}

# Look for OPATCH_PLATFORM_ID
PLATFORM=${OPATCH_PLATFORM_ID}

# If -oh is specified, use it to over-ride env. var. ORACLE_HOME
getOH=0

# If -jre or -jdk are specified, use it to launch opatch,
#   with -jdk > -jre.  And we expect there is a "bin/java" underneath
#   the value supplied
getJRE=0
getJDK=0

JDK=""
JRE=""

for arg in "$@"; do
  if [ $getOH = 1 ]; then
     OH=$arg
     getOH=0
  fi
  if [ $getJRE = 1 ]; then
     JRE=$arg
     getJRE=0
  fi
  if [ $getJDK = 1 ]; then
     JDK=$arg
     getJDK=1
  fi

  if [ "$arg" = "-oh" ]; then
     getOH=1;
  fi
  if [ "$arg" = "-jre" ]; then
     getJRE=1;
  fi
  if [ "$arg" = "-jdk" ]; then
     getJDK=1;
  fi
done

# Error out if OH is not set
if [ "$OH" = "" ]; then
    echo "Oracle Home is not set.  Set ORACLE_HOME or use -oh option."
    echo "OPatch returns with error code = 1"
    exit 1
fi

# Export the OH to be used in child scripts
ORACLE_HOME=$OH
export ORACLE_HOME

# Update for LD_LIBRARY_PATH.
echo "${LD_LIBRARY_PATH}" | grep "$OH/oui/lib" 2>&1 >/dev/null

if [ $? -eq 1 ] ; then
    LD_LIBRARY_PATH=$OH/oui/lib:${LD_LIBRARY_PATH}
    export LD_LIBRARY_PATH
fi

# Is there a version of perl that's usable?
# The default is to use the one with Apache so if it's not there check
# to see what's happening.
LIBRARIES="";
PERL_BIN="";

if [ -f $OH/perl/bin/perl ] ; then

    PERL_BIN=$OH/perl/bin/perl
    PERL5LIB=$OH/perl/lib/5.6.1:$BASE/perl_modules
    export PERL5LIB
    
elif [ -f $OH/Apache/perl/bin/perl ] ; then

    LIBRARIES="$OH/Apache/perl/lib/5.00503"
    PERL_BIN="$OH/Apache/perl/bin/perl"
    PERL5LIB=$LIBRARIES:$BASE/perl_modules
    export PERL5LIB

else

    # This should handle spaces or new lines for multiple entries.
    PERL_BIN=`\which perl | \grep perl | \cut -d" " -f1 | \head -1`

    if [ "${PERL_BIN}" = "no" ] ; then
        echo "\n\n"
        echo "You have to invoke the patch tool manually."
        echo "The syntax is:"
        echo "    perl <path to patch tool>/opatch.pl <patch command>"
        echo "If everything is installed correctly you should be able to run"
        echo "    perl <path to patch tool>/opatch.pl"
        echo "    and see the basic help message."

        exit 1;
    fi

    # If perl is in a non-standard place setting $LIBRARIES makes no sense
    # as if it's a symbolic link it doens't give the real location without
    # following the link. At that point this script would be much more 
    # more complex than it should be. The user is better off invoking
    # the script manually. It appears that in most cases they will only
    # need to supply PERL5LIB.
fi

    echo "PERL5LIB=$PERL5LIB; export PERL5LIB"
    echo "$PERL_BIN $BASE/opatch.pl $@"
    $PERL_BIN $BASE/opatch.pl $@

    RESULT=$?
    echo "";
    exit ${RESULT};
