#!/bin/sh
# Node deconfiguration script. Must be run from a surviving node of 
# the cluster *after* stopping all of the Cluster Ready Services daemons
# on the nodes to be deinstalled/deleted.
# USAGE: rootdeletenode node,node-number[,nodei,nodei-number]
# Note: To get node numbers for the nodes to be deleted, 
# run the following command:
#     $ORA_CRS_HOME/bin/olsnodes -n

if [ -z "$RM" ]; then RM="/bin/rm -f"; fi
if [ -z "$ECHO" ]; then ECHO=/bin/echo; fi
if [ -z "$SED" ]; then SED=/bin/sed; fi
if [ -z "$AWK" ]; then AWK=/bin/awk; fi
if [ -z "$GREP" ]; then GREP=/bin/grep; fi
if [ -z "$ID" ]; then ID=/usr/bin/id; fi

# 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

USE_FORCE=false
NODES_TO_DELETE=$1

if [ "$NODES_TO_DELETE" = "" ];then
  $ECHO "Specify node(s) to delete from Oracle Cluster Ready Services"
  $ECHO "Usage: $0 node,node-number[,..,nodei,nodei-number]"
  exit 1
fi

ORA_CRS_HOME=%s_OracleHome%
export ORA_CRS_HOME
CH=$ORA_CRS_HOME
ORACLE_HOME=$CH
export ORACLE_HOME

verifyCRSResources()
{
  VIP_SUFFIX=".vip"
  GSD_SUFFIX=".gsd"
  ONS_SUFFIX=".ons"
  LSNR_SUFFIX=".lsnr"
  DB_SUFFIX=".db"

  ##if CRS stack is up and running
  if $ORA_CRS_HOME/bin/crs_stat > /dev/null 2>&1
  then
     NODE_NAME=$1

     ### dependent resources other than VIP, GSD, and ONS
     DEP_RES=`$ORA_CRS_HOME/bin/crs_stat -l | $AWK '/NAME=/ {print $0}' | $GREP -v $VIP_SUFFIX | $GREP -v $GSD_SUFFIX | $GREP -v $ONS_SUFFIX`

     ##Node specific Listener resources
     LSNR_RES=`$ORA_CRS_HOME/bin/crs_stat -l | $AWK '/NAME=/ {print $0}' | $GREP $NODE_NAME | $GREP $LSNR_SUFFIX`

     ##The database resources
     DB_RES=`$ORA_CRS_HOME/bin/crs_stat -l | $AWK '/NAME=/ {print $0}' | $GREP $DB_SUFFIX | $AWK -F'.' '{print $2}'`

     if [ ! -z "$DEP_RES" ];
     then
       for i in $DB_RES
       do
         ##Find the database instances configured to run on the node
         INSTS=`$ORA_CRS_HOME/bin/srvctl config database -d $i | $AWK "/^$NODE_NAME / { print \\$2 }"`
         if [ ! -z "$INSTS" ];
         then
           for j in $INSTS
           do
             INST_RES="ora.$i.$j.inst"
             if $ORA_CRS_HOME/bin/crs_stat $INST_RES;
             then
               $ECHO "The database instance is still configured on node $NODE_NAME."
                if [ "$USE_FORCE" != "true" ];
               then
                 $ECHO "Delete the database instance using srvctl or DBCA before removing the node"
                 return 1
               fi
             fi
           done
         fi
       done

       if [ ! -z "$LSNR_RES" ]; then
         $ECHO "CRS resources for listeners are still configured on node $NODE_NAME."
         if [ "$USE_FORCE" != "true" ];
         then
                    $ECHO "You must remove the listeners before removing the node.  Use srvctl or NETCA to remove them."
           return 1
         fi
       fi
     fi
  fi

  return 0
}

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

case $PLATFORM in
Linux) LD_LIBRARY_PATH=$ORA_CRS_HOME/lib
       export LD_LIBRARY_PATH
       ;;
HP-UX) MACH_HARDWARE=`/bin/uname -m`
       if [ "$MACH_HARDWARE" = "ia64" ]; then
          SO_EXT=so
          NMAPIDIR_64=/opt/nmapi/nmapi2/lib/hpux64
       else
          SO_EXT=sl
          NMAPIDIR_64=/opt/nmapi/nmapi2/lib/pa20_64
       fi 
       LD_LIBRARY_PATH=$ORA_CRS_HOME/lib:$NMAPIDIR_64:/usr/lib:$LD_LIBRARY_PATH
       export LD_LIBRARY_PATH
       ;;
SunOS) LD_LIBRARY_PATH=$ORA_CRS_HOME/lib:/opt/ORCLcluster/lib:/usr/lib:/usr/ucblib:$LD_LIBRARY_PATH
       LD_LIBRARY_PATH_64=$ORA_CRS_HOME/lib:/opt/ORCLcluster/lib:/usr/lib:/usr/ucblib:$LD_LIBRARY_PATH_64
       export LD_LIBRARY_PATH
       export LD_LIBRARY_PATH_64
       ;;
AIX)   LIBPATH=$ORA_CRS_HOME/lib:/opt/ORCLcluster/lib:$ORA_CRS_HOME/lib:/opt/ORCLcluster/lib:/usr/lib:$LIBPATH
       LD_LIBRARY_PATH=$LIBPATH:$LD_LIBRARY_PATH
       export LIBPATH
       export LD_LIBRARY_PATH
       ;;
OSF1)  LD_LIBRARY_PATH=$ORA_CRS_HOME/lib:/shlib:/usr/lib
       export LD_LIBRARY_PATH
       ;;
*)     /bin/echo "ERROR: Unknown Operating System"
       exit -1
       ;;
esac

## extract node names from node1,node1-number,node2,node2-number,...
NODES_LIST=`$ECHO $NODES_TO_DELETE | $AWK '{
	nElems = split($1, nodeList, ",");
	for (i = 1; i < nElems;)
	{
	    print nodeList[i];
	    i += 2;
	}
      }'`

for node in $NODES_LIST
do
        # See if ORACLE_HOME resources exist
        verifyCRSResources $node || { $ECHO $?; exit 1; }
done

for i in $NODES_LIST
do
        $CH/bin/srvctl stop nodeapps -n $i
done

SRVCTL_STATUS=0
for i in $NODES_LIST
do
        $CH/bin/srvctl remove nodeapps -n $i -f
	status=$?

	if [ "$status" != "0" ]; then
	   $ECHO "Failed to delete the nodeapp for the node: $i"
	   SRVCTL_STATUS=1
	   break
	fi
done

if [ $SRVCTL_STATUS ];
then
  $ECHO "CRS nodeapps are deleted successfully"
else
  $ECHO "Deleting CRS nodeapps failed"
  exit 1
fi

CLSCFG=$CH/bin/clscfg

# clscfg will modify the current configuration stored in the
# Oracle Cluster Repository. Any ocssd daemons that were not stopped
# by running the previous root script will notice the deconfiguration
# actions of clscfg and terminate ungracefully. This will cause their
# nodes to reboot, and not start the cluster software ever again.

$CLSCFG -delete -nn $NODES_TO_DELETE
STATUS=$?
if [ $STATUS -eq 0 ]; then
  $ECHO "'$NODES_TO_DELETE' deleted successfully"
else
  $ECHO "Failed in deleting nodes '$NODES_TO_DELETE'"
fi
