#!/bin/sh 

. ./srm_keys.sh

tmp_file_name=/tmp/GetVxvmDiskChunkInfo.$$
tmp_out_file_name=/tmp/GetVxvmDiskChunkInfo.out.$$
tmp_file_vollist=/tmp/GetVxvmDiskChunkVolList.$$
tmp_curdg_file=/tmp/GetVeritasVolumeInfo.curdg.$$
tmp_curvol_file=/tmp/GetVeritasVolumeInfo.curvol.$$
tmp_file_name_vx=/tmp/GetVeritasVolumeInfoVx.$$
tmp_file_scratch=/tmp/GetVeritasVolumeInfoScratch.$$
tmp_file_err=/tmp/ESM_SVM_Err.$$

ESMID_VER=1
ESM_VXVM_VOL_TAG=$ESMID_VER~$ID_SRM_KEY_VOLUME~veritas~vxvm~custom

hostedby=`./GetESMIdInfo.sh $ID_SRM_KEY_HOSTNAME`

slice_char="s"

dg_list=$@

sid=$SRMAGT_SCAN_ID
if [ "$sid" = "" ]
then
        sid="no_sid"
fi
vx_encl_file="/tmp/GetVxvmEnclosures.vxvmdisks.$sid"
if [ ! -f $vx_encl_file -a "$sid" = "no_sid" ]
then
	./GenerateVxvmDiskNameMap.sh
fi


rm -f $tmp_file_err
touch $tmp_file_err

rm -f $tmp_file_name
touch $tmp_file_name

vxprint -htQq > $tmp_file_name

rm -f $tmp_out_file_name
touch $tmp_out_file_name
rm -f $tmp_file_vollist
touch $tmp_file_vollist

# Ignore Sub volumes
svList=/tmp/GetVxvmDiskChunkInfo.svList.$$
vxprint -qQrstvL | grep "^sv " |  awk '{ print $4 }' > $svList


# get the list of all volumes and chunks in them, along with the disk groups id's 
# and rid.
grep "^[dg,v,sd]" $tmp_file_name | grep -v "PRIVATE" > $tmp_out_file_name
while read j
do
    id=`echo $j | awk '{print $1}'`

    if	[ "$id" = "dg" ]
    then
        next_cur_dg=`echo $j | awk '{print $2}'`
        rm -f $tmp_curdg_file
        vgid=`echo $j | awk '{print $6}'`
        echo $next_cur_dg $vgid > $tmp_curdg_file
        id=""
        continue
    fi

    if	[ "$id" = "v" ]
    then
        volname=`echo $j | awk '{print $2}'`
        if grep "^$volname$" $svList > /dev/null 2>&1; then
            continue
        fi
        
        next_cur_vol=`echo $j | awk '{print $2}'`
        dg=`cat $tmp_curdg_file | cut -d' ' -f1`
        dgid=`cat $tmp_curdg_file | cut -d' ' -f2`

        volrid=`vxprint -m -g $dg -v $next_cur_vol 2>/dev/null | \
                awk '$1 ~ /^rid=/ {print substr($1,index($1,"=")+1)}'`          
        echo "volume" $next_cur_vol $vgid"~"$volrid >> $tmp_file_vollist
        
        id=""
        continue
    fi
    
    if [ "$id" = "sd" ]
    then
        next_sd=`echo $j | awk '{print $2}'`
        echo "subdisk" $next_sd >> $tmp_file_vollist
    fi
done < $tmp_out_file_name

rm -f $tmp_out_file_name



# get a list of disk groups and subdisks
grep "^[dg,sd]" $tmp_file_name | grep -v "PRIVATE" > $tmp_out_file_name
cur_dg=""
for i in `awk '{printf("%s:%s:%s:%s:%s:%s\n",$1,$2,$4,$5,$6,$7);}' $tmp_out_file_name`
do
	fname=`echo $i | cut -d":" -f1`
	if	[ "$fname" = "dg" ]
	then
		cur_dg=`echo $i | cut -d":" -f2`
		continue
	fi
	cname=`echo $i | cut -d":" -f2`
	log_dname=`echo $i | cut -d":" -f3`

	# have to make sure there are no disks with the same name in different dg's
	dname=`vxprint -g $cur_dg | grep "^dm" | grep "$log_dname" | awk '{print $3}'`
	#dname=`grep "^dm" $tmp_file_name | grep $log_dname | awk '{print $3}'`

	if [ "$dname" = "" ]
	then
		continue
	fi

        tst1=`echo $dname | sed -n '/c[0-9].*t*d[0-9].*s[0-9]/p'`
        tst2=`echo $dname`

        if [ "$tst1" = "" ]
        then
                dname=$tst2
        else
                tst1=`echo "$tst1" | sed 's/s[0-9]$//'`
                dname=$tst1
        fi


        # there is a chance that the current record may have an exceptional
        # state in which case we will want to skip the processing of this disk or
        # record.  The exceptional states in question are:
        #   NODEVICE - underlying  disk  could  not  be  found
        #   REMOVED  - underlying  disk is  in  the  removed state
        #   IOFAIL   - unrecovered I/O failure caused plex  to  be  detached
        #   RECOVER  - disk replacement left the plex in need of recovery
        # lets test for these possible bad states and skip if necessary
        if [ "$fname" = "dm" ]; then
            admin_state=`echo $i | cut -d":" -f6`
            if [ "$admin_state" = "NODEVICE" ]; then
                continue
            elif [ "$admin_state" = "REMOVED" ]; then
                continue
            elif [ "$admin_state" = "IOFAIL" ]; then
                continue
            elif [ "$admin_state" = "RECOVER" ]; then
                continue
            fi
        fi


	st=`echo $i | cut -d":" -f4`
	slen=`echo $i | cut -d":" -f5`
        if [ "-" = "$slen" ] ; then
            continue
        fi

	len=`echo "((512*$slen)/1024)" | bc`

	found=1
	if	[ "$dg_list" != "" ]
	then
		found=0
		for dg_ind in $dg_list
		do
			if	[ "$dg_ind" = "$cur_dg" ]
			then
				found=1
				break
			fi
		done
	fi 
	if	[ $found -eq 1 -a $dname != "-" ]
	then
		# check to see if last 3 chars = "-B0" for which we can ignore the entry.
		cname_chars=`echo -n "$cname" | wc -c | sed 's/ //g' `
		lastseg=`expr $cname_chars - 3`
		chkB0=`echo -n "$cname" | cut -b $lastseg-$cname_chars`
		if 	[  "$chkB0" != "-B0" ] 
		then 	

                rm -f $tmp_file_scratch
                touch $tmp_file_scratch

                # get the esm id of the volume the sub disk is on
                while read k
                do
                    id=`echo $k | awk '{print $1}'`

                    if	[ "$id" = "volume" ]
                    then
                        cur_vol=`echo $k | awk '{print $3}'`
                        
                        # if sv, continue
                        if grep "^$cur_vol$" $svList > /dev/null 2>&1; then
                            continue
                        fi
                         
                        rm -f $tmp_file_scratch
                        cur_vol_id=$cur_vol
                        echo $cur_vol_id > $tmp_file_scratch
                        continue
                    fi
    
                    if [ "$id" = "subdisk" ]
                    then
                        diskid=`echo $k | awk '{print $2}'`
                        if [ "$diskid" = "$cname" ]
                        then
                            # found it
                            break    
                        fi        
                    fi
                done < $tmp_file_vollist
                
                read volid < $tmp_file_scratch

                rm -f $tmp_file_scratch

                if [ "$volid" != "" ]
                then
                    # Check if the disk name is in enclosure format
                    # and convert to c#t#d#
                    if [ -f $vx_encl_file ]; then
                        ctdName=`grep "^$dname " $vx_encl_file | cut -d" " -f2`
                        if [ "$ctdName" ]; then
                            dname=$ctdName
                        fi
                    fi
                    
                    # get the disk id
                    dskesmid=`./GetESMGiduInfo.sh $dname`
                    if [ $? -ne 0 ]
                    then
                        # there was an error
                        echo $dskesmid >> $tmp_file_err
                    else      
                        esmid=$ESMID_VER"~"$ID_SRM_KEY_DISK_CHUNK"~"$ESM_VXVM_VOL_TAG"~"$volid"~"$cname

                        echo "\"$ID_SRM_KEY_TYPE=$ID_SRM_KEY_DISK_CHUNK\",\"$ID_SRM_KEY_ID=$esmid\",\c"
                        echo "\"$ID_SRM_KEY_DISK=$dskesmid\",\"$ID_SRM_KEY_START=$st\",\c"
                        echo "\"$ID_SRM_KEY_LENGTH=$len\",\"$ID_SRM_KEY_DISK_CHUNK=$hostedby":"$cname\",;"
                    fi    
                fi    
		fi
	fi
done

cat $tmp_file_err

# Now remove the tmp files
rm -f $tmp_file_name
rm -f $tmp_out_file_name
rm -f $tmp_file_vollist
rm -f $tmp_curdg_file
rm -f $tmp_curvol_file
rm -f $tmp_file_scratch
rm -f $tmp_file_name_vx
rm -f $tmp_file_err
rm -f $svList

exit 0
