#!/bin/sh

######################################################################
# Copyright (c) 2004, 2013, Oracle and/or its affiliates. All rights reserved. 
#
#  zhhe       05/08/13   Fix bug 16766557
#  fisong     01/04/13   Add opatchauto query command
#  vganesan   10/09/12   Fix help text, Bug #14731347
#  vganesan   09/04/12   Revamp the CLI for RUP5, later fine tune it for
#			 OMS-AUTO.
#  fisong     05/16/12   Initial Creation
#  zhhe      10/23/12  sync with OPATCH_11.1.0.9.10_GENERIC opatchauto script
#
#  cawan     5/15/12   Create and support
#                          opatchauto apply, rollback
#  cawan     8/20/12   Fix Bug #14512559
######################################################################

BASE=`dirname $0`
PWD=`pwd`

applyAuto=0
rollbackAuto=0
getDeploy=0
getUnDeploy=0
helpArg=0

OP=$1
OPATCH_ARGS=

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

# Check for OPATCH_DEBUG
DEBUGVAL=false
if [ "$DEBUG" = "TRUE" ] || [ "$DEBUG" = "true" ]; then
    DEBUGVAL=true;
fi


# Set based on OP
if [ "$OP" = "apply" ]; then
  applyAuto=1
elif [ "$OP" = "rollback" ]; then
  rollbackAuto=1
fi

shift

for arg in "$@"; do

    if [ "$arg" = "-help" -o "$arg" = "-h" ]; then
        helpArg=1
    fi
    if [ "$arg" = "-deploy" ]; then
	getDeploy=1

        if [ $applyAuto = 1 ]; then
          continue
        else
          echo "Syntax Error..Wrong option combination. Please check 'opatchauto -h/-help'."
          exit 1;
        fi
    fi
    if [ "$arg" = "-undeploy" ]; then
	getUnDeploy=1
 
        if [ $rollbackAuto = 1 ]; then
         continue;
        else
         echo "Syntax Error..Wrong option combination. Please check 'opatchauto -h/-help'."
         exit 1;   
        fi  
    fi
    if [ "$arg" = "-auto" ]; then
        echo "Syntax Error..'auto' option is not allowed for this command. Please check 'opatchauto -h/-help'."
        exit 1;       
    fi

    # Append the rest of the args
    OPATCH_ARGS="$OPATCH_ARGS $arg"

done

OP=`echo $OP| tr '[:upper:]' '[:lower:]'`
PREFIX=
if [ $applyAuto = 1 ]; then
  if [ $getDeploy = 0 ]; then 
    PREFIX="apply -auto"
  elif [ $getDeploy = 1 ]; then
    PREFIX="deploy"
  else
    echo "Syntax Error..Please check 'opatchauto -h/-help'."   
    exit 1;    
  fi  
elif [ $rollbackAuto = 1 ]; then
  if [ $getUnDeploy = 0 ]; then
    PREFIX="rollback -auto"
  elif [ $getUnDeploy = 1 ]; then
    PREFIX="undeploy"
  else
    echo "Syntax Error..Please check 'opatchauto -h/-help'."
    exit 1;
  fi 
elif [ "$OP" = "lspatches" ]; then
    PREFIX="$OP -all"
elif [ "$OP" = "checkapplicable" ]; then
    PREFIX="prereq"
elif [ "$OP" = "query" -o "$OP" = "version" -o "$OP" = "commit" -o "$OP" = "saveconfigurationsnapshot" -o "$OP" = "-help" -o "$OP" = "-h" ]; then
    PREFIX="$OP";
elif [ -z "$OP" ]; then
    echo "Syntax Error... No command specified."
    echo "Please run opatchauto -help | -h to get more details"
    echo ""
    echo "opatchauto failed with error code = 1"
    exit 1
else
    echo "Syntax Error... Unrecognized command \"$OP\"."
    echo "Please run opatchauto -help | -h to get more details"
    echo ""
    echo "opatchauto failed with error code = 1"
    exit 1
fi

if [ $helpArg = 1 -a $applyAuto = 1 ]; then
  OPATCH_ARGS="$PREFIX$OPATCH_ARGS -fmw -opatchautoscript"
elif [ $helpArg = 1 -a $rollbackAuto = 1 ]; then
  OPATCH_ARGS="$PREFIX$OPATCH_ARGS -fmw -opatchautoscript"
else
  OPATCH_ARGS="$PREFIX$OPATCH_ARGS -opatchautoscript"
fi

if [ "$DEBUGVAL" = "true" ]; then
   echo "Command to execute: $BASE/opatch $OPATCH_ARGS"
fi
# execute opatch command
$BASE/opatch $OPATCH_ARGS

RESULT=$?

exit $RESULT
