#!/bin/ksh
# IBM_PROLOG_BEGIN_TAG 
# This is an automatically generated prolog. 
#  
# 61haes_r721 src/43haes/usr/sbin/cluster/cspoc/utilities/clgetvg.sh 1.9 
#  
# Licensed Materials - Property of IBM 
#  
# COPYRIGHT International Business Machines Corp. 1996,2016 
# 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 
# @(#)56	1.9 src/43haes/usr/sbin/cluster/cspoc/utilities/clgetvg.sh, hacmp.cspoc, 61haes_r721, 1628B_hacmp721 7/8/16 07:38:06

# usage:
# clgetvg {-l <lv_name> | -f <fs_name>}
#
# returns:
# prints out the owning volume group name, if the give logical volume or file
# system is found

#USAGE="clgetvg {-l <lv_name> | -f <fs_name>}"
USAGE=$(/bin/dspmsg  -s 2 cspoc.cat 1 "clgetvg {-l <lv_name> | -f <fs_name>}\n")


FS=""		#	File system
LV=""		#	Logical volume
integer LV_count=0

(( $# != 2 )) && {
    print $USAGE 1>&2
    exit 1
}

case $1 in
    "-l" )	#	Given the logical volume name
	    LV=$2
	;;

    "-f" )	#	Derive the logical volume name from the file system name
	    FS=$2 
	    LV=$(LC_ALL=C odmget -q "attribute = label and value = $FS" CuAt | \
			grep 'name =' | cut -f2 -d'"')
	    [[ -z $LV ]] && {
		/bin/dspmsg  -s 2 cspoc.cat  2 "FS %s not found\n" "$FS" >&2
		exit 1
	    }
 
            #
            :   Check for uniqueness
            #
            LV_count=$( print -- "$LV" | wc -w )
            if (( $LV_count > 1 ))
            then
                #
                :  Various file system migration scenarios can temporarily
                :  have two logical volumes in the cluster with the same
                :  mount point.  If there is a unique entry in /etc/filesystems,
                :  the situation is salvageable.
                #
                egrep -p "^${FS}:[[:space:]]*\$" /etc/filesystems | grep -w "dev[[:space:]]*=" | read skip_dev skip_eq LV_path_name
                [[ -n $LV_path_name ]] && LV=${LV_path_name##'/dev/'}
                [[ -z $LV ]] && {
                       /bin/dspmsg  -s 2 cspoc.cat  2 "FS %s not found\n" "$FS" >&2
                       exit 1
                }
           fi
	;;

    *) 		#	only if flag is not -l or -f
	    print "$USAGE" 1>&2
	    exit 1
	;;
esac

#
#	Now that the logical volume name is known, determine the volume group
#	name, and print it out
#
VG=$(LC_ALL=C odmget -q "name = $LV" CuDv | grep 'parent =' | cut -f2 -d'"')
if [[ -z $VG ]] ; then
    /bin/dspmsg  -s 2 cspoc.cat  13 "LV %s not found\n" "$LV" >&2
    exit 1
else
    print $VG
fi
