#!/bin/sh
# CRS deconfiguration script. Must be run after running rootdelete.sh 
# on all nodes of the cluster.
# This script will wipe out OCR device used for Oracle Cluster Ready Services.
# USAGE: rootdeinstall [downgrade [ -version <version>] ]
# The option downgrade will downgrade the ocr from current 
# version to specified version
# For downgrading to 9.2.x versions, use 9.2
# For downgrading to 10.1.x versions, use 10.1

ORACLE_OWNER=%OwnerId%
DBA_GROUP=%s_usergroup%
ORA_CRS_HOME=%s_OracleHome%

if [ -z "$CP" ]; then CP=/bin/cp; fi
if [ -z "$ECHO" ]; then ECHO=/bin/echo; fi
if [ -z "$SLEEP" ]; then SLEEP=/bin/sleep; fi
if [ -z "$CHOWN" ]; then CHOWN=/bin/chown; fi
if [ -z "$CHGRP" ]; then CHGRP=/bin/chgrp; fi
if [ -z "$CHMOD" ]; then CHMOD=/bin/chmod; fi
if [ -z "$RM" ]; then RM=/bin/rm; fi
if [ -z "$CAT" ]; then CAT=/bin/cat; fi
if [ -z "$GREP" ]; then GREP=/bin/grep; fi
if [ -z "$AWK" ]; then AWK=/bin/awk; fi
if [ -z "$DD" ]; then DD=/bin/dd; fi
if [ -z "$ID" ]; then ID=/usr/bin/id; fi

usage()
{
   printf "Usage: "
   printf "rootdeinstall [-help] [downgrade [-version <version>]]\n"
   printf "\-help: print this message\n"
   printf "downgrade: Downgrade OCR from 10gR2\n"
   printf "\-version <version>: Downgrade OCR to a specified version, default: 10.1\n"
}

# This script is intended to be run by root.

RUID=`$ID|$AWK -F\( '{print $2}'|$AWK -F\) '{print $1}'`
if [ ${RUID} != "root" ];then
  $ECHO "You must be logged in as root to run $0."
  $ECHO "Log in as root and restart $0 execution."
  exit 1
fi

USER_ARGS=$*

DOWNGRADE=FALSE
VERSION="10.1"

if [ $# -gt 0 ]; then
 if [ $1 = "-help" ];
 then
   usage;
   exit 2;
 fi
fi

while [ $# -gt 0 ];
do
  if [ $1 = "downgrade" ]; then
    DOWNGRADE=TRUE
    if [ $# -ge 2 ];then
      if [ $2 = "-version" ]; then
        if [ $# -ge 3 ]; then
	  VERSION=$3;
	  shift;

          if [ "$VERSION" != "10.1" -a "$VERSION" != "9.2" ]; then
            $ECHO "Wrong version ($VERSION) number specified as an input. You can downgrade cluster repository to either 10.1 or 9.2 versions"
	    exit 2;
          fi
        else
          $ECHO "Specify a target version number for downgrade"
          exit 2
        fi
        shift;
      fi
    fi
# Commenting out the following shift as per bug 5034967
# shift
  else
    usage;
    exit 2;
  fi
  shift;
done

CH=$ORA_CRS_HOME
ORACLE_HOME=$CH
export ORACLE_HOME
export ORA_CRS_HOME

OCRCONFIGDIR=/var/opt/oracle
SRVCONFIG=$OCRCONFIGDIR/srvConfig.loc
OCRCONFIG=$OCRCONFIGDIR/ocr.loc

UNAME=/bin/uname
PLATFORM=`$UNAME`

case $PLATFORM in
Linux) OCRCONFIGDIR=/etc/oracle
       OCRCONFIG=/etc/oracle/ocr.loc
       SRVCONFIG=/var/opt/oracle/srvConfig.loc
       ;;
HP-UX) OCRCONFIGDIR=/var/opt/oracle
       SRVCONFIG=/var/opt/oracle/srvConfig.loc
       OCRCONFIG=/var/opt/oracle/ocr.loc
       ;;
SunOS) OCRCONFIGDIR=/var/opt/oracle
       SRVCONFIG=/var/opt/oracle/srvConfig.loc
       OCRCONFIG=/var/opt/oracle/ocr.loc
       ;;
AIX)   OCRCONFIGDIR=/etc/oracle
       SRVCONFIG=/var/opt/oracle/srvConfig.loc
       OCRCONFIG=/etc/oracle/ocr.loc
       ;;
OSF1)  OCRCONFIGDIR=/var/opt/oracle
       SRVCONFIG=/var/opt/oracle/srvConfig.loc
       OCRCONFIG=/var/opt/oracle/ocr.loc
       ;;
*)     $ECHO "ERROR: Unknown Operating System"
       exit -1
       ;;
esac

if [ "$DOWNGRADE" != "TRUE" ];
then
   if [ -f "$SRVCONFIG" ];
   then
     $ECHO "There may be 9i RAC databases in OCR. Downgrading OCR will downgrade database configurations to 9i RAC format. Do you want to downgrade OCR configuration?[y]:[y/n]"
     while :
     do
        read RDVAR
        case $RDVAR in
          "")     RDVAR="n" ; break ;;
          !*)     ${SHELL-/bin/sh} ; echo ;;
          *)      break ;;
        esac
     done 

     if [ "$RDVAR" = "y" ] || [ "$RDVAR" = "Y" ]; then
        DOWNGRADE=TRUE
     fi
   fi
fi

if [ "$DOWNGRADE" = "TRUE" ]; 
then

   #Downgrading databases
   $ECHO "Downgrading database configuration to version $VERION"
   #srvconfig -downgrade $VERSION

   #Downgrading OCR 
   #If OCR has any database configurations, then 
   #You must downgrade each databases manually, 
   #and downgrade its OCR configuration using 'srvconfig -downgrade' 
   #before downgrading OCR

   $ECHO "Database configurations in OCR should be downgraded using 'srvconfig' before downgrading OCR. Continue?[n]:[y/n]"
   while :
   do
     read RDVAR
     case $RDVAR in
        "")     RDVAR="n" ; break ;;
        !*)     ${SHELL-/bin/sh} ; echo ;;
        *)      break ;;
     esac
   done 

   if [ "$RDVAR" = "n" -o "$RDVAR" = "N" ]; then
     exit 1
   fi

   #Remove 10.2 specific OCR keys and additional voting devices
   if [ "$VERSION" = "10.1" ]; then
     CRS_VER=$VERSION.0.0.0
     $ECHO  $CH/bin/clscfg -downgrade -version $CRS_VER
     $CH/bin/clscfg -downgrade -version $CRS_VER
     status=$?
     if [ $status != 0 ];
     then
        $ECHO "CLSCFG failed to downgrade OCR keys"
        exit $status
     else
	VOTE_DISK=`$CH/bin/crsctl query css votedisk -force | $AWK '{ if (NR==1) print }' | $AWK '{ print $3}'`
        if [ -f $VOTE_DISK ];
	then
	    $CHOWN $ORACLE_OWNER $VOTE_DISK
	    $CHGRP $DBA_GROUP $VOTE_DISK
	    $CHMOD 644 $VOTE_DISK
	fi
     fi
   fi

   if [ "$VERSION" = "9.2" ]; then
    $ECHO "Downgrading ocr configuration to 9.2"
    #Check existence of srvConfig.loc
    if [ ! -f "$SRVCONFIG" ];
    then
      #CASE 1: srvConfig.loc file does not exist. So creating one
      OCRLOC=`$AWK -F= '/ocrconfig_loc/ {print $2}' < $OCRCONFIG`
      $ECHO srvconfig_loc=$OCRLOC > $SRVCONFIG 

      $CHGRP $DBA_GROUP $SRVCONFIG
      $CHMOD 644 $SRVCONFIG

      $ECHO "downgrading ocr configuration now"
      $CH/bin/ocrconfig -downgrade -version $VERSION

    else 
      #CASE 2:srvConfig.loc does exist. So lets see the OCR location
      SRV_OCRLOC=`$AWK -F= '/srvconfig_loc/ {print $2}' < $SRVCONFIG`
      OCRLOC=`$AWK -F= '/ocrconfig_loc/ {print $2}' < $OCRCONFIG`

      #CASE 2.1: srvconfig_loc = /dev/null
      if [ "$SRV_OCRLOC" = "/dev/null" ];
      then
        # srvConfig.loc has /dev/null that was invalidated during 10g install
	# Copying the location from ocr.loc to srvConfig.loc for downgrade
        $ECHO srvconfig_loc=$OCRLOC > $SRVCONFIG 

        $ECHO "downgrading ocr configuration now"
        $CH/bin/ocrconfig -downgrade -version $VERSION

      elif [ "$SRV_OCRLOC" != "$OCRLOC" ];
      then
        #CASE 2.2 srvconfig_loc!=ocrconfig_loc

        $ECHO "downgrading ocr configuration now"
        $CH/bin/ocrconfig -downgrade -version $VERSION

	#OCR device contents will be wiped out
        $DD if=/dev/zero skip=25 bs=4k count=2560 of=$OCRLOC

      else 

        #CASE 2.3: srvconfig_loc = ocrconfig_loc
        $ECHO "downgrading ocr configuration now"
        $CH/bin/ocrconfig -downgrade -version $VERSION

      fi

      #Remove ocr.loc file
      $RM $OCRCONFIG
    fi
   elif [ "$VERSION" = "10.1" ]; then
      $ECHO "Downgrading ocr configuration to 10.1"
      $CH/bin/ocrconfig -downgrade -version $VERSION
   fi
fi

OCRMIRRORLOC=
if [ ! -f "$OCRCONFIG" ];
then
  # ocr.loc file does not exist. Take ocr location of srvconfig.loc for setting
  # file permissions
  OCRLOC=`$AWK -F= '/srvconfig_loc/ {print $2}' < $SRVCONFIG`
else 
  OCRLOC=`$AWK -F= '/ocrconfig_loc/ {print $2}' < $OCRCONFIG`
  OCRMIRRORLOC=`$AWK -F= '/ocrmirrorconfig_loc/ {print $2}' < $OCRCONFIG`

  if [ -z "$OCRMIRRORLOC" ];
  then
     #OCR mirror device is not specified.
     $ECHO ""
  elif [ "$OCRMIRRORLOC" = "/dev/null" ]; then
     #OCR mirror location is disabled
     $ECHO ""
  else
     $ECHO "Removing contents from OCR mirror device"
     $DD if=/dev/zero skip=25 bs=4k count=2560 of=$OCRMIRRORLOC
  fi

  if [ "$DOWNGRADE" != "TRUE" ];
  then
     $ECHO "Removing contents from OCR device"
     $DD if=/dev/zero skip=25 bs=4k count=2560 of=$OCRLOC

     $RM $OCRCONFIG
  fi
fi

$CHOWN $ORACLE_OWNER $OCRLOC
$CHGRP $DBA_GROUP $OCRLOC
$CHMOD 644 $OCRLOC

if [ ! -z "$OCRMIRRORLOC" ];
then
  $CHOWN $ORACLE_OWNER $OCRMIRRORLOC
  $CHGRP $DBA_GROUP $OCRMIRRORLOC
  $CHMOD 644 $OCRMIRRORLOC
fi

#Reset the permissions on OCRCONFIGDIR
$CHOWN $ORACLE_OWNER $OCRCONFIGDIR 
$CHGRP $DBA_GROUP $OCRCONFIGDIR 
$CHMOD 755 $OCRCONFIGDIR 

$CHOWN -R $ORACLE_OWNER $CH
