#!/bin/ksh 
#******************************************************************************
#* PROGRAM 	: listpvlv.ksh						      *
#* AUTHOR  	: James M. Santos					      *
#* DESCRIPTION	: Displays summary information about physical volumes (PV)    *
#*		  together with more detailed information about each logical  *
#*		  volume (LV) defined in each PV.			      *
#* PARAMETERS	: 1) -p 	 - display output			      *
#*		  2) -o filename - send the output of the script to the	      *
#*				   filename specified.			      *
#*		  3) hdisk(s) 	 - PV name				      *
#******************************************************************************
USAGE="Usage: $0 [-p] [-o file] [hdisk...]"

UOM='m'
TMPFILE=/tmp/@@listpvlv.tmp

trap 'rm -f $TMPFILE; exit' 1 2 15	# Remove temp file when signals
					#   1, 2 or 15 is received.

while getopts ":po:" OPT		# Use getopts to get command line
do					#   options and then parse it.  Set 
  case $OPT in				#   the unit of measure (UOM) to 'p'
    p  ) UOM='p' ;;			#   if the -p option is given. Set
    o  ) exec 1>$OPTARG ;;		#   the output of the script to the
    *  ) echo "\nERROR : $USAGE\n"	#   file given with the -o option.
         exit 1 ;;			
  esac
done 
shift $(($OPTIND - 1))

lspv | awk '{print $1}' > $TMPFILE	# Field 1 contains the hdisk names.

if [ $# -eq 0 ]				# If there are no arguments, process
then					#   all the existing PVs (PVLIST
   PVLIST=`cat $TMPFILE`		#   would contain the names of all
else					#   PVs that would be processed
   PVLIST=$*				#   later in the script).
   while test $# -gt 0
   do
     if grep $1 $TMPFILE >/dev/null 	# Verify if the hdisk(s) that the user
     then :				#   entered are valid PVs. If 
     else				#   they are, do nothing (then :).
       echo "\nERROR: $0: $1 is not a valid hdisk.\n"
       exit 1				#   If there is an error, exit the
     fi					#   pgm with a message specifying
     shift				#   the invalid PV. 
   done
fi

echo Host : `hostname`
echo Date : `date +%D%t%T`
echo

for PV in $PVLIST
do
   echo Information for $PV		# Print the specific fields that are
   lspv $PV | tee $TMPFILE | awk '	#   needed for the report.
   NF == 6 && /^PHY/ {
      printf("%s %s\n",    "Part of Volume Group :", $6);
   }
   NF == 7 && /^PP/ {
      printf("%s %s %s\n", "PP Size              :", $3, $4);
   }
   NF == 8 && /^TOT/ {
      printf("%s %s %s %s\n", "Total PPs            :", $3, $4, $5);     
   }
   NF == 5 && /^FRE/ {
      printf("%s %s %s %s\n", "Free PPs             :", $3, $4, $5);
   }' -

   if [ "$UOM" = 'm' ]
   then 
      PPSIZEMULT=`grep "PP SIZE" $TMPFILE | awk '{print $3}'`
   else
      PPSIZEMULT=1
   fi

   lspv -l $PV | tail +3 > $TMPFILE 

   awk '	
   BEGIN {
     if ("'"$UOM"'" == "m")
     { 
       printf("%-20s %44s\n",\
       "            ","                         Size(MB)  Size(MB)");
       printf("%-20s %44s %s\n",\
       "C D  LV Name","Distribution             LPxPP Sz  PPxPP Sz","FS Name");
     }
     else
     {
        printf("%-20s %44s\n",\
        "            ","                         Size (No  Size (No");
        printf("%-20s %44s %s\n",\
        "C D  LV Name","Distribution             of LPs)   of PPs) ","FS Name");
     } 
   }
   {
      LPSIZE = $2 * '$PPSIZEMULT';	# LPSIZE and PPSIZE would contain the
      PPSIZE = $3 * '$PPSIZEMULT';	#   size in megabytes if user entered
					#   "-m" as a parameter to the script.
      "lslv "$1" | grep COPIES" | getline COPY_STR_LINE;
      split(COPY_STR_LINE,CSLARRAY);
      COPY=CSLARRAY[2];
      if (CSLARRAY[5] ~ /^str/)
        STRIPED="S";
      else
        STRIPED=" ";
      "lslv -l "$1" | tail +3 | wc -l " | getline NO_OF_DISKS;
      printf("%d %d%s %-16s %-23s %9d %9d %s\n",\
             COPY,NO_OF_DISKS,STRIPED,$1,$4,LPSIZE,PPSIZE,$5);
   }' $TMPFILE

   echo
done
rm $TMPFILE
