#!/bin/bash
#
# Name: lssg
# Copyright: (c)Copyright 2004-2005 Hewlett-Packard Development Company, L.P.
#
# Description: lists all sg devies and a description
#              of each device
#
# Modification History
#
# Chad Dupuis   08/26/04 Initial Development
# Chad Dupuis   08/23/05 Added command line options to determine whether to print
#                        the world wide name and UUID of each device; Use
#                        /sys/block to get a listing of sd devices
#                        on the system

#
# functions
#

# This function prints a help message

print_help () {
 echo "Usage: $0 [-w|l|h]"
 echo ""
 echo "Prints all bound /dev/sg* devices"
 echo ""
 echo "-w: print world wide node name of devices"
 echo "-l: print inquiry page 0x83 UUID of devices"
 echo "-h: print help message"
 exit 0
}

# This function parses parameters passed in from the command line

read_args () {

 WWIDFLAG=0
 LUNIDFLAG=0
 HELPFLAG=0

 if [ $# -gt 0 ]
 then
        # parse command line into arguments

        getopt wlh $* 1>/dev/null 2>/dev/null

        # check result of parsing

        if [ $? != 0 ]
        then
                echo "Bad argument or missing argument"
                exit 1
        fi

        set -- `getopt wlh $*`

        while [ $1 != -- ]
        do
                case $1 in
                        -w) WWIDFLAG=1;;
                        -h) HELPFLAG=1;;
			-l) LUNIDFLAG=1;;
                        *) echo "$1 is an illegal argument"
                           exit 1;;
                esac
                shift   # next flag
        done

        shift   # skip --

        if [ $HELPFLAG -eq 1 ]
        then
                print_help
                exit 0
        fi
 fi
}

space_str () {
 STRINGSIZE=$1
 STR=$2 
 SPACESTR=""
 
 SIZE=`echo $STR | wc -c`
 NUM_OF_SPACES=`expr $STRINGSIZE - $SIZE`

 COUNT=$NUM_OF_SPACES

 while [ $COUNT -ne 0 ]
 do
  SPACESTR=$SPACESTR" "
  COUNT=`expr $COUNT - 1`
 done
}

# 
# defines
#

SCSI_INFO="/opt/hp/hp_fibreutils/scsi_info.26"

# check to make sure that scsi_info exists

ls $SCSI_INFO 1>/dev/null 2>/dev/null

if [ $? -ne 0 ]
then
 echo "$SCSI_INFO does not exist"
 exit 1
fi

# parse argument list

read_args $*

# loop through devices and query the devices directly
# to obtain information

DONE=0
i=0

while [ $DONE -eq 0 ]
do
 # check to see if device is bound 

 $SCSI_INFO /dev/sg${i} 1>/dev/null 2>/dev/null

 if [ $? -ne 0 ]
 then
	exit 0
 fi

 # get device info from scsi_info

 OUTPUT=`$SCSI_INFO /dev/sg${i}` 
 CONN=`echo $OUTPUT | awk 'BEGIN {FS=":"} {print $1}' | awk 'BEGIN {FS="="} {print $2}' | sed 's/"//g'`
 VENDOR=`echo $OUTPUT | awk 'BEGIN {FS=":"} {print $2}' | awk 'BEGIN {FS="="} {print $2}' | sed 's/"//g'`
 MODEL=`echo $OUTPUT | awk 'BEGIN {FS=":"} {print $3}' | awk 'BEGIN {FS="="} {print $2}' | sed 's/"//g'`
 FWREV=`echo $OUTPUT | awk 'BEGIN {FS=":"} {print $4}' | awk 'BEGIN {FS="="} {print $2}' | sed 's/"//g'`
 WWID=`echo $OUTPUT | awk 'BEGIN {FS=":"} {print $5}' | awk 'BEGIN {FS="="} {print $2}' | sed 's/"//g'`
 LUNID=`echo $OUTPUT | awk 'BEGIN {FS=":"} {print $6}' | awk 'BEGIN {FS="="} {print $2}' | sed 's/"//g'`
 
 # get the number of spaces to pad after the connection address

 space_str 5 "sd${i}"
 DEVICESPACE=$SPACESTR 
 space_str 16 $CONN
 CONNSPACE=$SPACESTR
 
 # format world wide ID string

 if [ "$WWID" != "" ]
 then
  	WWID="`echo $WWID | cut -c 1-4`"-"`echo $WWID | cut -c 5-8`"-"`echo $WWID | cut -c 9-12`"-"`echo $WWID | cut -c 13-16`"
 fi 
 
 # print out information for that device
 
 echo -n "sg${i}${DEVICESPACE} ${CONN}${CONNSPACE} $VENDOR $MODEL $FWREV"
 if [ $WWIDFLAG -eq 1 ]
 then
 	echo -n "  $WWID"
 fi
 if [ $LUNIDFLAG -eq 1 ]
 then
 	echo -n "  $LUNID"
 fi
 echo ""

 # increment counter
 
 i=`expr $i + 1` 
done
