#!/bin/ksh93
# IBM_PROLOG_BEGIN_TAG 
# This is an automatically generated prolog. 
#  
# 61haes_r714 src/43haes/usr/sbin/cluster/events/reconfig_udresources.sh 1.1 
#  
# Licensed Materials - Property of IBM 
#  
# COPYRIGHT International Business Machines Corp. 2010 
# All Rights Reserved 
#  
# US Government Users Restricted Rights - Use, duplication or 
# disclosure restricted by GSA ADP Schedule Contract with IBM Corp. 
#  
# IBM_PROLOG_END_TAG 
# @(#)82	1.1  src/43haes/usr/sbin/cluster/events/reconfig_udresources.sh, hacmp, 61haes_r714 11/28/11 15:25:11
#########################################################################
#
#   COMPONENT_NAME: EVENTS
#
#   DESCRIPTION: The functions will be used during DARE event to acquire
#                or release userdefined resources
#
#   USED IN:     release_vg_fs.sh,get_disk_vg_fs.sh
#                node_down_local.sh,node_up_local.sh
#                node_up_local_complete.sh
#
#   FUNCTIONS: getudrestypes_after
#              acquire_udresources
#              release_udresources
#
#########################################################################

#########################################################################
#                                                                       #
#       Name:           getudrestypes_after                             #
#                                                                       #
#       Description:    List the resources types need to be processed   #
#                       after the resource type specified by $1         #
#                                                                       #
#       Called by:      acquire_udresources,release_udresources         #
#                                                                       #
#       Calls to:       None                                            #
#                                                                       #
#       Arguments:      RESOURCETYPE                                    #
#                                                                       #
#       Returns:        List of userdefined resource types              #
#                                                                       #
#########################################################################
function getudrestypes_after
{
   typeset PS4_FUNC="getudrestypes_after"
   [[ "$VERBOSE_LOGGING" = "high" ]] && set -x

   INPUT_RESTYPE=$1
   [[ -z $INPUT_RESTYPE ]] && return
   RTLIST=$(cludrestype -l -h | awk ' { print $1 }')

   cludrestype -l -h | awk ' { 
if ( $1 == RES || "FIRST" == RES ) { start=1; }
if ( inudloop == 1 && $2 == "STANDARD_RESOURCE" ) { start=0; exit; }
if ( start == 1 && $2 != "STANDARD_RESOURCE" ) { print $1; inudloop=1;}
} ' start=0 inudloop=0 RES=$INPUT_RESTYPE

}

#########################################################################
#                                                                       #
#       Name:           acquire_udresources                             #
#                                                                       #
#       Description:    acquires Userdefined resources found            #
#                       after the type specified by $1                  #
#                                                                       #
#       Called by:      get_disk_vg_fs.sh,node_up_local.sh              #
#                       node_up_local_complete.sh                       #
#                                                                       #
#       Calls to:       getudrestypes_after,clcallev                    #
#                                                                       #
#       Arguments:      AFTER_<RESOURCETYPE>                            #
#                                                                       #
#       Returns:        0-Success, 2 failure                            #
#                                                                       #
#########################################################################

function acquire_udresources
{
   typeset PS4_FUNC="acquire_udresources"
   [[ "$VERBOSE_LOGGING" = "high" ]] && set -x

   REF_RESTYPE=${1##AFTER_}
   RTLIST=$(getudrestypes_after $REF_RESTYPE)

   [[ -z $RTLIST ]] && return 0

   #for each resource type, process the resources of that type
   for each_rt in $RTLIST
   do
      RES=\$"$each_rt"

	set +u
	UDRES_LIST="$(eval echo $RES)"
	set -u

      [[ -z $UDRES_LIST ]] && return 0

      for each_udr in $UDRES_LIST
      do
       : Acquiring userdefined resource $each_udr :
      done

      clcallev start_udresource "$UDRES_LIST"
      RC=$?
      : exit status of start_server $APPLICATIONS is: $RC
      if [ $RC -ne 0 -a $STATUS -eq 0 ] ; then
         STATUS=2
      fi
   done
}


#########################################################################
#                                                                       #
#       Name:           release_udresources                             #
#                                                                       #
#       Description:    releases Userdefined resources found            #
#                       before the type specified by $1                 #
#                                                                       #
#       Called by:      release_vg_fs.sh,node_down_local.sh             #
#                                                                       #
#       Calls to:       getudrestypes_after,clcallev                    #
#                                                                       #
#       Arguments:      BEFORE_<RESOURCETYPE>                           #
#                                                                       #
#       Returns:        0-Success, 2 failure                            #
#                                                                       #
#########################################################################

function release_udresources
{
   typeset PS4_FUNC="release_udresources"
   [[ "$VERBOSE_LOGGING" = "high" ]] && set -x

   #get the resource type
   REF_RESTYPE=${1##BEFORE_}

   #find the resourcetypes need to be processed after 
   # resource type spefified by REF_RESTYPE
   RTLIST=$(getudrestypes_after $REF_RESTYPE)

   # Resources need to be released in revers order
   TMPLIST=""
    while [[ -n "$RTLIST" ]] ; do
	print $RTLIST | read first_one RTLIST
	TMPLIST=$first_one${TMPLIST:+" "$TMPLIST}
    done

    RTLIST="${TMPLIST}"	    # reversed order now

   [[ -z $RTLIST ]] && return 0

   #for each resource type, process the resources of the type
   for each_rt in $RTLIST
   do
      RES=\$"$each_rt"

	set +u
	UDRES_LIST="$(eval echo $RES)"
	set -u

      [[ -z $UDRES_LIST ]] && return 0

      #reverse the resources list
      TMPLIST=""
      while [[ -n "$UDRES_LIST" ]] ; do
	   print $UDRES_LIST | read first_one UDRES_LIST
         TMPLIST=$first_one${TMPLIST:+" "$TMPLIST}
      done

      UDRES_LIST="${TMPLIST}"	    # reversed order now

      for each_udr in $UDRES_LIST
      do
         : Releasing userdefined resource $each_udr
      done

      clcallev stop_udresource "$UDRES_LIST"
      RC=$?
      : exit status of start_server $APPLICATIONS is: $RC
      if [ $RC -ne 0 -a $STATUS -eq 0 ] ; then
         STATUS=2
      fi
   done
}
