#!/bin/sh 

if [ $# -lt 1 ]
then
	exit 0
fi

ESMID_VER=1

. ./srm_keys.sh

dkrpm_cmd=./dkrpm

tmp_file_name=/tmp/GetDiskInfo.$$
tmp_out_file_name=/tmp/GetDiskInfo.out.$$
tmp_swap_file_name=/tmp/GetVolumeInfo.swp.$$
tmp_file_err=/tmp/GetDiskInfoErr.$$
tmp_cmd_file_name=/tmp/GetDiskInfo.cmd.$$
tmp_format_buffer=/tmp/GetDiskInfo.buf.$$

#=================== Functions =======================================

#---------------------------------------------------------------------
create_format_buffer() {
	    touch $tmp_cmd_file_name
	    touch $tmp_format_buffer
	    echo "inquiry" > $tmp_cmd_file_name
	    inquiry=`cat $tmp_cmd_file_name | format -e -d $ldid 2> /dev/null > $tmp_format_buffer`
}

#========================= Main =======================================

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


# Agent configuration file. Check if in an env variable first..
if [ "%${SRMAGT_CONFIG_PATH}" = "%" ]
then
    AGENT_CONFIG_FILE="/opt/SUNWstm/sscr/agent/etc/uagent.cfg"
else
    AGENT_CONFIG_FILE=${SRMAGT_CONFIG_PATH}/uagent.cfg
fi

# *** In ESM20 we do not currently report on disk free space. Therefore doing the 
# fstyp commands to find out whether there is a file system on a partition is
# inconsequential. The flag below can be used to turn it back on if and when requried.
# When switched off, no free space is reported.

# This significantly improves performance.
#    *** ALWAYS SKIP fstyp ***
skip_fstyp=true


# check if we should skip grown defects, disk details, disk rpms. This options are put into the uagent.cfg file
# SKIP_DEFECTS true/false
# SKIP_DETAILS true/false
# SKIP_RPMS true/false
#    **** ALWAYS SKIP DEFECTS ****
#skip_defects=`grep SKIP_DEFECTS $AGENT_CONFIG_FILE | awk '{print $2}' | tr '[A-Z]' '[a-z]'`
skip_defects=true
skip_details=`grep SKIP_DETAILS $AGENT_CONFIG_FILE | awk '{print $2}' | tr '[A-Z]' '[a-z]'`
skip_rpms=`grep SKIP_RPMS $AGENT_CONFIG_FILE | awk '{print $2}' | tr '[A-Z]' '[a-z]'`
samdisk_files=/tmp/StartScan.samdisks.$sid
vxdisk_list_file=/tmp/StartScan.vxvmdisks.$sid

rm -f $tmp_swap_file_name
touch $tmp_swap_file_name

dskpath=/dev/rdsk/

rm -f $tmp_file_err
touch $tmp_file_err
swap -l | grep "/" > $tmp_swap_file_name

# Get the hostname. To be used in the public id of the file system
hostname=`./GetESMIdInfo.sh $ID_SRM_KEY_HOSTNAME`


make_fs_zero=0
for i in $@
do
    # check in enclosure list file...
	test_enc=`grep "$i$" $vx_encl_file 2> /dev/null`

	if [ "$test_enc" != "" ]
	then
		newlid=`echo "$test_enc" | awk '{print $2}'`
		newgid=`echo "$test_enc" | awk '{print $1}'`
		i="$newlid=$hostname:$newgid"
	fi

	rm -f $tmp_file_name
	touch $tmp_file_name
	
	# Get the local disk name if passed in as LID=GID format
	ldid=`echo $i | cut -d"=" -f1`
	gdid=`echo $i | cut -d"=" -f2`

	dev_name=`echo "/dev/rdsk/"$ldid"s2"`

	# Sig generation algorithm handles MPXIO and T3 in fabic mode. 
	# uses GID for sig if enclosure name
	if [ "$ldid" = "$gdid" ]
	then	
		sig1=`echo $ldid | cksum | awk '{print $1}'`
		sig2=`echo $ldid | sum | awk '{print $1}'`
		sig=`expr $sig1 / $sig2`
	else
		sig1=`echo $gdid | cksum | awk '{print $1}'`
                sig2=`echo $gdid | sum | awk '{print $1}'`
                sig=`expr $sig1 / $sig2`
	fi


	tmp_siglen=`echo $sig | wc -m`
	tmp_siglen=`expr $tmp_siglen - 1`	
	
# Determine the disk type using "format" and device driver path information.
# Disk type values:
# 0 = Unknown
# 1 = IDE
# 2 = SCSI
# 3 = FC
# 4 = SSA
# 5 = Serial
# 6 = Other

# This variable holds the disk type strings that are searched for using
# the "giduadm -d" command.  Add further types as needed for support.
	dt_string="SCSI IDE"
	dt="0"
        
	for dtstr in $dt_string
	do
    	dtcount=`giduadm -d $ldid | grep -c "$dtstr"`
    	if [ $dtcount -ne 0 ]
    	then
            if [ "$dtstr" = "SCSI" ];then
                dt="2"
                break
            fi
            if [ "$dtstr" = "IDE" ];then
                dt="1"
                break
            fi
    	fi
	done

# try to get optional disk information
	if [ "$skip_details" = "true" ]
	then
	    vend="Disabled"
	    prod="Disabled"
	    rev="----"
	    if [ "$dt" = "2" ]
	    then
		sn="Disabled"
	    else
		sn="N/A"
	    fi
	else
	    
            # If we have a SCSI disk, try giduadm for the serial number and any 
            # attributes that format may not have provided
	    if [ "$dt" = "2" ]
                then
                rm -f $tmp_file_name
                touch $tmp_file_name
                giduadm -d $ldid > $tmp_file_name

                inq_vendor=`grep "VENDOR=" $tmp_file_name | awk -F= '{print $2}'`
                inq_model=`grep "MODEL=" $tmp_file_name | cut -d"=" -f2`
                inq_rev=`grep "REVISION=" $tmp_file_name | cut -d"=" -f2`
            
                esn=`grep "VPD80=" $tmp_file_name | awk -F= '{print $2}'`
                if [ "$esn" != "" ]
                then
                    sn=`echo $esn`
                else
                    sn="Unknown"
                fi

                rm -f $tmp_file_name
                touch $tmp_file_name
            fi
	    

	    # Get everything we can from format no matter what type of disk
	    if [ "$inq_vendor" ]; then :
            else
                create_format_buffer
                #inquiry=`cat $tmp_cmd_file_name | format -e -d $ldid 2> /dev/null > $tmp_format_buffer`
                inq_vendor=`grep "Vendor:" $tmp_format_buffer | awk -F: '{print $2}'` 
            fi

            if [ "$inq_model" ]; then :
            else
                if [ ! -f $tmp_format_buffer ]; then
                    create_format_buffer
                fi
                    inq_model=`grep "Product:" $tmp_format_buffer | cut -d":" -f2` 
            fi
            if [ "$inq_rev" ]; then :
            else
                if [ ! -f $tmp_format_buffer ]; then
                    create_format_buffer
                fi
                    inq_rev=`grep "Revision:" $tmp_format_buffer | cut -d":" -f2` 
            fi
        
            # if we didn't get the serial number, try retrieving from luxadm
            if [ "$sn" ]; then :
            else
                sn=`luxadm inq $dev_name | grep "^Serial Number" | awk '{print $3}'`
            fi

	    vend=`echo $inq_vendor`
	    prod=`echo $inq_model`
	    rev=`echo $inq_rev`

	    if [ "$vend" = "" ] 
	    then
		vend="Unknown"
	    fi

	    if [ "$prod" = "" ] 
	    then
		prod="Unknown"
	    fi


	    rm -f $tmp_cmd_file_name
	    rm -f $tmp_format_buffer
	    touch $tmp_cmd_file_name		
	fi      

	prtvtoc $dev_name > $tmp_file_name
	if [ $? -ne 0 ]
	then
	   continue
	fi

	ebps=`grep "bytes/sector" $tmp_file_name | awk '{print $2}'`
	bps=`echo $ebps`

	espc=`grep "sectors/cylinder" $tmp_file_name | awk '{print $2}'`
	spc=`echo $espc`

	eac=`grep "accessible" $tmp_file_name | awk '{print $2}'`
	ac=`echo $eac`

	capacity_kb=`echo "(($bps*$spc*$ac)/1024)" | bc`
	sec_per_kb=`echo 1024/$bps | bc`

	rpm="0"
	
	if [ "$skip_rpms" = "true" ]
	then
	    rpm="-1"
	else
	    if [ -f "$dkrpm_cmd" ]
	    then
		rpm=`$dkrpm_cmd $ldid`
	    fi
	fi

	if [ "$skip_defects" = "true" ]
        then
	    pd="-2"
	    gd="-2"
	else
	    touch $tmp_cmd_file_name
	    echo "defect" >> $tmp_cmd_file_name
	    echo "primary" >> $tmp_cmd_file_name
	    pd_out=`cat $tmp_cmd_file_name | format -e -d $ldid 2>/dev/null | grep "Defect List"`
	    pd=`echo $pd_out | awk '{print $7}'`
	    if [ "$pd" = "" ]
	    then
		pd="-1"
	    fi

	    rm -f $tmp_cmd_file_name
	    touch $tmp_cmd_file_name
	    echo "defect" >> $tmp_cmd_file_name
	    echo "grown" >> $tmp_cmd_file_name
	    gd_out=`cat $tmp_cmd_file_name | format -e -d $ldid 2>/dev/null | grep "Defect List"`
	    gd=`echo $gd_out | awk '{print $7}'`
	    if [ "$gd" = "" ]
	    then
		gd="-1"
	    fi
	fi

	rm -f $tmp_cmd_file_name
	touch $tmp_out_file_name
	grep -v "^*" $tmp_file_name > $tmp_out_file_name
	space_taken="0"
	fs="0"
	for j in `awk '{if (NF == 6) printf("%s:%s\n",$1,$5);}' $tmp_out_file_name`
	do
		pid=`echo $j | cut -d":" -f1`
		if [ "$pid" = "2" ]
		then
			continue
		fi
		cname=`echo "/dev/rdsk/"$ldid"s$pid"`
		scname=`echo "/dev/dsk/"$ldid"s$pid"`

#Check if it is swap
		is_swap=`grep -c $scname $tmp_swap_file_name`
		if [ "$is_swap" != "0" ]
		then
			continue
		fi

#If we know there is a fs there we skip it.
                if [ "$skip_fstyp" = "true" ];then
                    fsThere=1
                else    
                    if [ -d /usr/lib/fs ];then
                            DIR=/usr/lib/fs
                    else
                            DIR=/etc
                    fi

                    fsThere=0 
                    for f in $DIR/*/fstyp
                    do
                        if [ "$f" = "$DIR/udfs/fstyp" ];then
                            continue
                        else
                            $f $cname > /dev/null 2>&1
                            if [ $? -eq 0 ];then
                                fsThere=1
                                break
                            fi
                        fi
                    done
                fi        

                if [ $fsThere -eq 1 ];then
                    continue
                fi
    
		scnt=`echo $j | cut -d":" -f2`
		ispace_taken=`echo "($scnt*$bps)/1024" | bc`
		fs=`expr $ispace_taken + $space_taken`
	done

#remove the temp out file
rm -f $tmp_out_file_name

	if	[ -f $samdisk_files ]
	then
		cnt=`grep -c $i $samdisk_files`
		if [ $cnt -gt 0 ]
		then
			make_fs_zero=1
		fi
	fi

	if	[ -f $vxdisk_list_file ]
	then
		cnt=`grep -c $i $vxdisk_list_file`
		if [ $cnt -gt 0 ]
		then
			make_fs_zero=1
		fi
	fi

	if	[ $make_fs_zero -eq 1 ]
	then
		fs=0
	fi


        # check if you need to get the esmid. It might already be supplied in the 
        # arg list. Or a display name might be supplied in the arg list.
		 esmid=""
		 displayname=$hostname:$dskpath$ldid

        if [ "$gdid" != "$ldid" ] 
        then
			  # check if the gdid is a esmid
			  cnt=`echo $gdid | grep -c $ESMID_VER"~disk~"`
			  if [ $cnt -gt 0 ]
			  then
	            esmid=$gdid
			  else
				 displayname=$gdid
				 esmid=""
			  fi
		 fi

        if [ "$esmid" = "" ]
		 then
            # get the ESMID for this using the esmgidu utility (which uses gidu)
            esmid=`./GetESMGiduInfo.sh $ldid`
            if [ $? -ne 0 ]
            then
                # there was an error. Skip this disk and continue
				  echo $esmid >> $tmp_file_err	
                esmid=""
            fi    
        fi

        if [ "$esmid" != "" ]
        then
            echo "\"$ID_SRM_KEY_TYPE=$ID_SRM_KEY_DISK\",\"$ID_SRM_KEY_ID=$esmid\",\c"
            echo "\"$ID_SRM_KEY_MAKE=$vend\",\"$ID_SRM_KEY_REVISION=$rev\",\c"
            echo "\"$ID_SRM_KEY_SERIAL_NUMBER=$sn\",\"$ID_SRM_KEY_MODEL=$prod\",\c"
            echo "\"$ID_SRM_KEY_CAPACITY=$capacity_kb\",\"$ID_SRM_KEY_RPM=$rpm\",\c"
            echo "\"$ID_SRM_KEY_FACTORY_DEFECTS=$pd\",\"$ID_SRM_KEY_GROWN_DEFECTS=$gd\",\c"
            echo "\"$ID_SRM_KEY_LOCAL_ID=$ldid\",\c"
            echo "\"$ID_SRM_KEY_FREESPACE=$fs\",\"$ID_SRM_KEY_AWRE_FLAG=-1\",\c"
            echo "\"$ID_SRM_KEY_DISK_TYPE=$dt\",\"$ID_SRM_KEY_NAME=$displayname\",\c"
            echo "\"$ID_SRM_KEY_SIGNATURE=$sig\",;"
        fi    

        make_fs_zero=0
done

cat $tmp_file_err

#remove the temp out file
rm -f $tmp_file_name
rm -f $tmp_out_file_name
rm -f $tmp_swap_file_name
rm -f $tmp_file_err
rm -f $tmp_cmd_file_name
rm -f $tmp_format_buffer
exit 0
