#!/bin/ksh93
#  ALTRAN_PROLOG_BEGIN_TAG
#  This is an automatically generated prolog.
#
#  Copyright (C) Altran ACT S.A.S. 2017,2021.  All rights reserved.
#
#  ALTRAN_PROLOG_END_TAG
#
# IBM_PROLOG_BEGIN_TAG 
# This is an automatically generated prolog. 
#  
# 61haes_r721 src/43haes/usr/sbin/cluster/cspoc/utilities/clfiltlsvg.sh 1.6 
#  
# 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 
# @(#)  7d4c34b 43haes/usr/sbin/cluster/cspoc/utilities/clfiltlsvg.sh, 726, 2147A_aha726, Feb 05 2021 09:50 PM

typeset VG_LIST			    #   list of volume groups
typeset O_FLAG			    #   -o (on line only) specified
typeset A_FLAG			    #   -a (off line only) specified
typeset LSVG_FLAGS		    #   flags passed through to lsvg

#
#   Pick up flags and volume group names from caller.  Checks for incompatible
#   operands have been done by the caller.
#
while getopts ":aolMp" flag ; do
    case $flag in
    a)				    #   list only inactive volume groups
	A_FLAG="TRUE"
	;;
    o)				    #   list only active volume groups
	O_FLAG="TRUE"
	;;
    l|M|p)			    #   lsvg options
	LSVG_FLAGS=${LSVG_FLAGS:+"$LSVG_FLAGS "}"-${flag}"
	;;
    * ) 
	dspmsg scripts.cat 6555 "Option \"-${OPTARG}\" is not valid\n" "-${OPTARG}"
	return 1
	;;
    esac
done
shift $(($OPTIND - 1))		    #	skip past the flags
VG_LIST=$*			    #	and pick up any volume group names

#
#   List the volume groups vary'd on
#
if [[ -n $O_FLAG ]] ; then
    lsvg -o | egrep -vw 'rootvg|caavg_private|altinst_rootvg|old_rootvg'     #   vary'd on volume groups, less rootvg,caavg_private,altinst_rootvg,old_rootvg

#
#   List the volume groups not vary'd on
#
elif [[ -n $A_FLAG ]] ; then
    lsvg | grep -vw "$(lsvg -o)"    #	volume groups, less vary'd on ones

#
#   List given volume groups with given options
#
elif [[ -n $VG_LIST ]] ; then
    ON_LIST=$(print $(lsvg -o))	    #	ones vary'd on (space separated list)
    for VG in $VG_LIST ; do	    #	for each give one
	if [[ $ON_LIST == @(?(* )$VG?( *)) ]] ; then	#   if in on list
	    if [[ -n $LSVG_FLAGS ]] ; then
		lsvg $LSVG_FLAGS $VG  #	list it with given options
	    else		    #	else no options specified
		print "${VG}:"	    #	flag start of output
		lsvg $VG	    #	list w/o options
	    fi
	fi
    done

#
#   If given only options, list with those options all volume groups except
#   rootvg
#   
elif [[ -n $LSVG_FLAGS ]] ; then
    ON_LIST=$(lsvg -o | egrep -vw 'rootvg|caavg_private|altinst_rootvg|old_rootvg')    #	ones vary'd on, less rootvg,caavg_private,altinst_rootvg,old_rootvg
    if [[ -n $ON_LIST ]] ; then	    #	If there are some vgs vary'd on
	lsvg $LSVG_FLAGS $ON_LIST   #	display them with the given option
    fi

#
#   Otherwise, just list them all
#
else
    lsvg | egrep -vw 'rootvg|caavg_private|altinst_rootvg|old_rootvg'        #   all volume groups, less rootvg,caavg_private,altinst_rootvg,old_rootvg
fi

exit 0
