#!/bin/ksh93
# IBM_PROLOG_BEGIN_TAG 
# This is an automatically generated prolog. 
#  
# 61haes_r714 src/43haes/lib/ksh93/aix/KLIB_AIX_get_vg_by_path.sh 1.7 
#  
# 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 
# @(#)73      1.7 src/43haes/lib/ksh93/aix/KLIB_AIX_get_vg_by_path.sh, hacmp, 61haes_r714 11/28/11 15:26:04
#
#=head1 NAME
#
# KLIB_AIX_get_vg_by_path - Get the volume group name for the specified path
#
#=head1 SYNOPSIS
#
# vg=$(KLIB_AIX_get_vg_by_path "/tmp")
# if (( $? == 0 )); then
#	echo $vg
# fi
#
#=head1 DESCRIPTION
#
# Obtains the name of the volume group for the specified file system path 
#
#=head1 ARGUMENTS
#
#	1: [scalar] Filesystem path
#
#=head1 RETURN
#
#   0: success, stdout contains the name of the volume group
#   1: failed, unable to determine the volume group name for
#      the specified path
#
#=head1 COPYRIGHT
#
#(C) COPYRIGHT International Business Machines Corp. 2005
#All Rights Reserved
#
#=cut
#
function KLIB_AIX_get_vg_by_path
{
	. /usr/es/lib/ksh93/func_include

        typeset path=$1
        typeset filesystem
        typeset tmp
        typeset free_mb
        typeset pcnt_used
        typeset inode_used
        typeset pcnt_iused
        typeset mounted

	[[ -z $path ]] && return 1

        typeset logical_volume=
        if (( $(df -m $path 2>/dev/null | wc -l 2>/dev/null) > 2 )); then
            df -m $path 2>/dev/null | tail -n  2 | head -n 1 | read -r filesystem tmp free_mb pcnt_used inode_used pcnt_iused mounted
        else
            df -m $path 2>/dev/null | tail -n 1 | read -r filesystem tmp free_mb pcnt_used inode_used pcnt_iused mounted
        fi
        odmget -q value=$mounted CuAt 2>/dev/null | while IFS='=' read name value; do
                value=$(eval echo $value)    # strip the "" from any values in CuAt
                name=$(eval echo $name)      # strip any whitespace
                if [[ "$name" == "name" ]]; then
                        logical_volume=$value
                        break
                fi
        done 2>/dev/null

        if [[ -z $logical_volume ]]; then
                return 1
        fi

        odmget -q name=$logical_volume CuDv | while IFS='=' read name value; do
                value=$(eval echo $value)    # strip the "" from any values in CuAt
                name=$(eval echo $name)      # strip any whitespace
                if [[ "$name" == "parent" ]]; then
                        echo $value
                        return 0
                fi
        done 2>/dev/null

        return 1
}
