#!/bin/ksh93
# IBM_PROLOG_BEGIN_TAG 
# This is an automatically generated prolog. 
#  
# 61haes_r714 src/43haes/lib/ksh93/db2/vg/KLIB_DB2_VG_node_pvid_compare.sh 1.4 
#  
# Licensed Materials - Property of IBM 
#  
# Restricted Materials of IBM 
#  
# COPYRIGHT International Business Machines Corp. 2005,2011 
# 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 
# @(#)80	1.4 src/43haes/lib/ksh93/db2/vg/KLIB_DB2_VG_node_pvid_compare.sh, hacmp, 61haes_r714 11/28/11 15:02:07
#
#=head1 NAME
#
# KLIB_DB2_VG_node_pvid_compare - Compare to nodes copies of a volume group
#                                 for differences in PVIDs
#
#=head1 SYNOPSIS
#
# . /usr/es/sbin/cluster/sa/db2/etc/db2.disc
# KLIB_DB2_VG_node_pvid_compare "nodeA" "nodeB" "vg1"
#
#=head1 DESCRIPTION
#
# Compare to nodes copies of a volume group for differences in PVIDs
#
#=head1 ARGUMENTS
#
#   1: [scalar] first node in comparison
#   2: [scalar] second node in comparison
#   3: [scalar] volume group name
#
#=head1 RETURN
#
#   0: volume group matches between the nodes (same set of pvids)
#   1: volume group does not match
#   2: neither node contains the specified volume group
#
#=head1 COPYRIGHT
#
#(C) COPYRIGHT International Business Machines Corp. 2005
#All Rights Reserved
#
#=cut
#
function KLIB_DB2_VG_node_pvid_compare
{
	. /usr/es/lib/ksh93/func_include

	typeset nodeA=$1
	typeset nodeB=$2
	typeset vg=$3
	typeset pvidsA
	typeset pvidsB

	if [[ -z "${AIX_DISKS[*]}" ]]; then
		return 2
	fi

	pvidsA=$(KLIB_DB2_VG_get_pvids $nodeA $vg)
	pvidsB=$(KLIB_DB2_VG_get_pvids $nodeB $vg)

	if [[ -z $pvidsA && -z $pvidsB ]]; then
		return 2
	fi
	if [[ -z $pvidsA ]]; then
		pvidsA=$(KLIB_DB2_VG_get_pvids $nodeA "None")
	fi
	if [[ -z $pvidsB ]]; then	
		pvidsB=$(KLIB_DB2_VG_get_pvids $nodeB "None")
	fi
	
	# Is the pvid list the same ?
	if [[ "$pvidsA" == "$pvidsB" ]]; then
		return 0
	fi

	typeset a
	typeset b
	typeset a_count=0
	typeset m_count=0

	for a in $pvidsA; do
		(( a_count=$a_count+1 ))
		for b in $pvidsB; do
			if [[ "$a" == "$b" ]]; then
				(( m_count=$m_count+1 ))
			fi
		done
	done

	if (( $a_count == $m_count )); then
		return 0
	fi

	# PVIDs don't match
	return 1
}
