#!/bin/ksh93
# IBM_PROLOG_BEGIN_TAG 
# This is an automatically generated prolog. 
#  
# 61haes_r714 src/43haes/usr/sbin/cluster/cspoc/utilities/cl_ls_free_lvs.sh 1.6 
#  
# Licensed Materials - Property of IBM 
#  
# COPYRIGHT International Business Machines Corp. 2009,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 
# @(#)86        1.6  src/43haes/usr/sbin/cluster/cspoc/utilities/cl_ls_free_lvs.sh, hacmp.cspoc, 61haes_r714 11/28/11 15:18:44
###############################################################################
#
#   COMPONENT_NAME: CSPOC Command Line Interface
#
# Name:
#   cl_ls_free_lvs
#
# Description:
#   Given the type of logical volume (jfs or jfs2), find all logical volumes
#   in the cluster of that type which do not have file systems defined on them.
#
# Arguments:
#   Logical volume type, 'jfs' or 'jfs2'
#
# Return Values:
#   Available log logical volumes of the given type (jfs or jfs2)
#   written to stdout along with the owning volume group, resource group and
#   node list.
#
################################################################################

PROGNAME=${0##*/}
export PATH="$(/usr/es/sbin/cluster/utilities/cl_get_path all)"

[[ $VERBOSE_LOGGING == high ]] && set -x
[[ $VERBOSE_LOGGING == high ]] && version='1.6 $Source: 61haes_r711 43haes/usr/sbin/cluster/cspoc/utilities/cl_ls_free_lvs.sh 1$'

ODMDIR=/etc/objrepos

    lv_type=$1
    header='false'
    no_free_lvs_msg=$(/bin/dspmsg -s 44 cspoc.cat 3 'No free logical volumes of type %s found' $lv_type)
    #
    :	Find all the free logical volumes across the cluster, and list them with owning resource group
    #
    /usr/es/sbin/cluster/sbin/cl_lsfreelvs -V $lv_type 2>&1 | while read ls_line 
    do
	if [[ $ls_line == $no_free_lvs_msg ]]
	then
	    break			# bail if nothing found
	fi

	#
	:	Did not just get an error message back
	#
	print $ls_line | read lv_name node_list

	if [[ -n $lv_name ]]
	then
	    #
	    :	Given a logical volume name, find the owning volume group
	    #
	    vg_name=$(odmget -q "name = $lv_name and PdDvLn = logical_volume/lvsubclass/lvtype " CuDv | sed -n '/parent =/s/.*"\(.*\)".*/\1/p')
	    if [[ -n $vg_name ]]
	    then
		#
		:   If the owning volume group was found, use that to find the owning resource group
		#
		rg_name=$(odmget -q "name = VOLUME_GROUP and value = $vg_name" HACMPresource | sed -n '/group =/s/.*"\(.*\)".*/\1/p')
	    fi
	    if [[ -z $rg_name ]]
	    then
		#
		:   If for some reason an owning resource group was not found - such as, this
		:   volume group has not yet been added to one - put out "<None>"
		#
		rg_name='<'$(dspmsg -s 109 cspoc.cat 156 'None')'>'
	    fi
	    if [[ $header != 'true' ]]
	    then
		#
		:   If this is the first time through, print out an appropriate header
		#
		LV_header=$(dspmsg -s 109 cspoc.cat 157 'Logical Volume')
		VG_header=$(dspmsg -s 109 cspoc.cat 120 'Volume Group')
		RG_header=$(dspmsg -s 109 cspoc.cat 140 'Resource Group')
		NL_header=$(dspmsg -s 109 cspoc.cat 155 'Node List')
		printf '#%-17s %-17s %-17s %s\n' "$LV_header" "$VG_header" "$RG_header" "$NL_header"
		header='true'
	    fi
	    node_list=${node_list%%,}		    # trim trailing comma from cl_lsfreelvs
	    #
	    :	Print out the information on this logical volume
	    #
	    printf ' %-17s %-17s %-17s %s\n' $lv_name $vg_name $rg_name $node_list
	fi
    done

    #
    :	Deal with the case where none were found
    #
    if [[ $header != 'true' ]]
    then
	print -u2 $(dspmsg -s 44 cspoc.cat 5 'There are no free shared %s logical volumes' $lv_type)
	exit 1
    fi
