#!/bin/sh

BASE=`dirname $0`
OPATCHAUTO_DIR=`dirname $BASE`
OPATCH_DIR=`dirname $OPATCHAUTO_DIR`

JAVA=
OH=${ORACLE_HOME}

# if [ "$OH" = "" ]; then
#  OH=`dirname $BASE`
# fi

# Scan for the cmdLine options
ohOpt=0;
jreOpt=0;
help=0;

for arg in "$@"; do
  FIRST_CHAR=$arg | sed 's/\(-\).*/\1/'

  if [ $ohOpt = 1 ]; then
         if [ "$FIRST_CHAR" = "-" ]; then
                echo "Provide a valid value for '-oh' option"
                echo "opatchauto returns with error code = 2"
                exit 2
        fi
        OH=$arg
        ohOpt=0
  fi

  if [ $jreOpt = 1 ]; then
         if [ "$FIRST_CHAR" = "-" ]; then
                echo "Provide a valid value for '-jre' option"
                echo "opatchauto returns with error code = 2"
                exit 2
        fi
        JAVA=$arg/bin/java
        jreOpt=0
  fi

  if [ $arg = "help" ]; then
    help=1
  fi

  if [ $arg = "-help" ]; then
    help=1
  fi

  if [ $arg = "-oh" ]; then
        ohOpt=1
  fi

  if [ $arg = "-jre" ]; then
        jreOpt=1
  fi

done

if [ $ohOpt -eq 1 ]; then
    echo "Provide a valid value for '-oh' option"
    echo "opatchauto returns with error code = 2"
    exit 2
fi

if [ $jreOpt -eq 1 ]; then
    echo "Provide a valid value for '-jre' option"
    echo "opatchauto returns with error code = 2"
    exit 2
fi

#if [ $help -eq 1 ]; then
#       echo "TO DO: Print out help "
#      exit 0
#fi

if [ "$1" != "apply" ]; then
        if [ "$1" != "rollback" ]; then
		if [ $help -ne 1 ]; then
                	echo "ERROR : Select either apply or rollback session."
                	exit 2
		fi
        fi
fi

if [ "$OH" = "" ]; then
    echo "ERROR : Provide a valid value for Oracle Home using '-oh' option"
    exit 3
fi

OPATCH_OH=$OH/OPatch

CP=$OH/oui/jlib

if [ "$JAVA" = "" ]; then # from -jre
  JAVA=$OH/jdk/jre/bin/java
fi


$JAVA -cp "$OPATCH_DIR/opatchauto-dir/opatchautocore/jlib/*:$OPATCH_DIR/jlib/*:$OPATCH_DIR/ocm/lib/emocmclnt.jar:$OH/oracle_common/modules/*:$CP/OraInstaller.jar:$CP/OraPrereq.jar:$CP/share.jar:$CP/orai18n-mapping.jar:$CP/xmlparserv2.jar:$CP/emCfg.jar:$CP/ojmisc.jar" -DOPatchauto.ORACLE_HOME=$OH oracle/opatchauto/core/OPatchAutoCore $@

exit $?
