#!/bin/sh

#--------------------------------------------------------
#
#This script detects the crs/rac based oracle home and invokes
# auto_patch with necessary paramenters
#
#---------------------------------------------------------

autopatch=0;
autohelp=0;

argCount=$#

if [ "$1" = "auto" ]; then
  autopatch=1
fi

if [ $autopatch = 1 ]; then
  if [ $argCount -gt 1 ]; then
    if [ "$2" = "-h" ]; then
      autohelp=1
    elif [ "$2" = "-help" ]; then
      autohelp=1
    fi
  fi
fi

i=1
CMD_ID_EXIST=
while [ -n "`echo "$PATH" | cut -d: -f${i}`" ] ; do
        if [ -s "`echo "$PATH" | cut -d: -f${i}`/id" ] ; then
                CMD_ID_EXIST="`echo "$PATH" | cut -d: -f${i}`/id"
                break
        fi
        i=`expr $i + 1`
done


if [ -z "$CMD_ID_EXIST" ]; then
        if [ $autopatch = 1 ]; then
                echo id command not avaliable!
                echo opatch auto can not proceed without id command!
                exit 1
        fi
fi

UID_INFO=`id`
IS_ROOT=`echo $UID_INFO | grep "^uid=0("`
if [ ! "$IS_ROOT" = "" ]; then
   if [ "$1" = "-h" ]; then
      autohelp=1
   elif [ "$1" = "-help" ]; then
      autohelp=1
   fi
fi

if [ $autohelp = 1 ]; then
  $BASE/crs/auto_patch.pl -h
  exit
fi

if [ $autopatch = 1 ]; then
  # it must be 
  # 'opatch auto'
  # 'opatch auto <patch loc>'
  # 'opatch auto -rollback'
  # 'opatch auto -rollback <patch loc>' 

  if [ $argCount -eq 1 ]; then
     # this must be just 'opatch auto'
     givenLoc=$PWD
     patchdir=`dirname $givenLoc`
     id=`basename $givenLoc`

     # Check to make sure <patch_loc>/custom/server exist
     # if [ ! -d $givenLoc/custom/server ]; then
     #    echo "$givenLoc is not a valid CRS bundle patch location."
     #    $BASE/crs/auto_patch.pl -h
     #    exit -1;         
     # fi

     shift
     # echo $BASE/crs/auto_patch.pl -patchdir $patchdir -patchn $id
     $BASE/crs/auto_patch.pl -patchdir $patchdir -patchn $id
     exit

  elif [ $argCount -gt 1 ] && [ $2 != "-rollback" ]; then
     # either 'opatch auto [other-args]' or 'opatch auto <patch_loc> [other-args]'

     givenLoc=$2
     if [ -d $givenLoc ]; then
        # must be 'opatch auto <patch_loc> other-args'
        # echo "opatch auto <patch_loc> [other-args]"
        givenLoc=$2
        patchdir=`dirname $givenLoc`
        id=`basename $givenLoc`

        # Check to make sure <patch_loc>/custom/server exist
        # if [ ! -d $givenLoc/custom/server ]; then
        #   echo "$givenLoc is not a valid CRS bundle patch location."
        #   $BASE/crs/auto_patch.pl -h
        #   exit -1;
        # fi

        shift
        shift
        # echo $BASE/crs/auto_patch.pl -patchdir $patchdir -patchn $id $@
        $BASE/crs/auto_patch.pl -patchdir $patchdir -patchn $id $@
        exit
    else
        # must be 'opatch auto [other-args]' or bad patch loc
        # just let auto_patch decide, assume it is 'opatch auto [other-args]
        # echo "opatch auto [other-args]"
        givenLoc=$PWD
        patchdir=`dirname $givenLoc`
        id=`basename $givenLoc`

        # Check to make sure <patch_loc>/custom/server exist
        # if [ ! -d $givenLoc/custom/server ]; then
        #   echo "$givenLoc is not a valid CRS bundle patch location."
        #   $BASE/crs/auto_patch.pl -h
        #   exit -1;
        # fi

        shift
        # echo $BASE/crs/auto_patch.pl -patchdir $patchdir -patchn $id $@
        $BASE/crs/auto_patch.pl -patchdir $patchdir -patchn $id $@
        exit
     fi

   elif [ $argCount -gt 1 ] && [ $2 = "-rollback" ]; then
     givenLoc=$2
     # either 'opatch auto -rollback [other-args] or 'opatch auto -rollback <patchloc> [other-args]'
     if [ $argCount -eq 2 ]; then
        # must be just 'opatch auto rollback' 
        givenLoc=$PWD
        patchdir=`dirname $givenLoc`
        id=`basename $givenLoc`

        # Check to make sure <patch_loc>/custom/server exist
        # if [ ! -d $givenLoc/custom/server ]; then
        #   echo "$givenLoc is not a valid CRS bundle patch location."
        #   $BASE/crs/auto_patch.pl -h
        #   exit -1;
        # fi
 
        shift
        shift
        # echo $BASE/crs/auto_patch.pl -rollback -patchdir $patchdir -patchn $id $@
        $BASE/crs/auto_patch.pl -rollback -patchdir $patchdir -patchn $id $@
        exit
     
     elif [ -d $3 ]; then
        # must be 'opatch auto -rollback <patch_loc> [other-args]
        # echo "opatch auto -rollback <patch_loc> [other-args]"
        givenLoc=$3
        patchdir=`dirname $givenLoc`
        id=`basename $givenLoc`

        # Check to make sure <patch_loc>/custom/server exist
        # if [ ! -d $givenLoc/custom/server ]; then
        #  echo "$givenLoc is not a valid CRS bundle patch location."
        #   $BASE/crs/auto_patch.pl -h
        #   exit -1;
        # fi

        shift
        shift
        shift
        # echo $BASE/crs/auto_patch.pl -rollback -patchdir $patchdir -patchn $id $@
        $BASE/crs/auto_patch.pl -rollback -patchdir $patchdir -patchn $id $@
        exit
     else
        # must be 'opatch auto -rollback [other-args]' or bad patch loc
        # just let auto_patch decide, assume it is 'opatch auto -rollback [other-args]
        # echo "opatch auto -rollback [other-args]"
        givenLoc=$PWD
        patchdir=`dirname $givenLoc`
        id=`basename $givenLoc`

        # Check to make sure <patch_loc>/custom/server exist
        # if [ ! -d $givenLoc/custom/server ]; then
        #   echo "$givenLoc is not a valid CRS bundle patch location."
        #   $BASE/crs/auto_patch.pl -h
        #   exit -1;
        # fi

        shift
        shift
        # echo $BASE/crs/auto_patch.pl -rollback -patchdir $patchdir -patchn $id $@
        $BASE/crs/auto_patch.pl -rollback -patchdir $patchdir -patchn $id $@
        exit

     fi
  else

    echo "Please run 'opatch auto [-rollback] <patch location>'"
    # echo "           'opatch auto [-rollback]' if you are in the patch location." 
    exit -1
  fi
fi

