#!/bin/ksh 
# this command extracts the lun that the lvm has used to varyon the volume group.
# it operates on a dump file.

if [ -f ./dump ];then
    echo "Ok this is extracting from a dump file"
    ./kdb -script ./dump ./unix |&
else
    echo "Ok this is getting the backing devices from live kdb"
    kdb -script  |&
fi

set -o noglob
#set -x

#236 37 lvm
#240 4 lvm.rootvg
#243 2 lvm
#286 4 lvm.P10backupvg
#290 4 lvm.P10pagingvg
#296 4 lvm.caavg_private
#306 5 lvm.P10db2logdir
#314 5 lvm.P10localapp
#324 5 lvm.P10sapdat01
#343 5 lvm.P10sapdat02
#359 5 lvm.P10sapdom01
#401 5 lvm.storagevg

# create my temp data collection directory
if [ ! -d ./vg_data ];then
    mkdir -p ./vg_data
else
    rm -rf ./vg_data
    mkdir -p vg_data
fi

print -p "volgrp "
while read -p line; do

    echo ${line} |egrep "WARNING:|vscsi_scsi_ptrs|Executing|VG@|(.)>" > /dev/null 2>&1
    if [ $? -ne 0 ];then
        # drain the kdb pipe.  This is slow but required so the next read wont be wrong.
        echo $line |egrep  "End"  >/dev/null ;rc=$?
        if [ $rc -eq 0 ];then
            break
        fi
        vg_ptr="`echo $line |awk '{print $1}'` ${vg_ptr}"
    fi

done
#echo ${vg_ptr}


for vg in ${vg_ptr}; do

   print -p "volgrp "${vg}
   while read -p line ;do
       echo $vg $line |egrep "vg_name" >> ./vg_data/$vg
       echo $vg $line |awk '{
                vg = $1;
                ptr = "";
		if (/pvol/) {
                    if (!/opn_pin/) {
                        ptr = $5;
                        pvol_array[ptr] = ptr;
                    }
                 } 
                 if (/lv_name/) {
                      lv_name = $3;
                      printf("lv_name............ %s, \n",lv_name) >> "./vg_data/"vg;
                 }
            } END { 
                
                for  ( pv_addr in pvol_array) {
                    # now build the command and send it to my tmp file
                    printf("\tpvol %s \n",pvol_array[pv_addr]) >> "./vg_data/"vg;
                }
            }'
       # drain the kdb pipe.  This is slow but required so the next read wont be wrong.
       echo $line |egrep  "End"  >/dev/null ;rc=$?
       if [ $rc -eq 0 ];then
            break
       fi
    done

done

cd vg_data

# now read the created files and run the pvol command collecting which device the lvm used to boot up on.

for vgs in `ls`;do
   while read line ;do
       echo $line |grep pvol > /dev/null
       if [ $? -eq 0 ];then
          print -p $line
          while read -p output; do
              echo $output |egrep  "End"  >/dev/null ;rc=$?
              if [ $rc -eq 0 ];then
                 break
              fi
              echo  $output |awk '{
                   if(/pv_name/)
                         printf("\t%s",$0);
              }'
          done
       else
           echo $line |grep lv_name > /dev/null
           if [ $? -eq 0 ];then
               echo "        $line"
           else
               echo "$line"
            fi
       fi
   done < $vgs
   echo 
done
rm -rf ./vg_data
