#!/bin/sh
#
# Name: sysfs_scandisk.sh
# Copyright: (c)Copyright 2006, 2008 Hewlett-Packard Development Company, L.P.
#
# Description: Given a particular SCSI host, generate a list
#              of SCSI devices attached to that SCSI host
#
# Revision History
#
# CD	04/19/06 Initial Development
# CD	08/29/06 Changes to scan rport directories for scsi devices
# CD	04/29/08 Add capability to scan "device" directory directly

#
# Functions
#

# Name: scan_and_print_target_devices
# Description: Prints out scsi devices for a particular scsi host that does
#              not use rports
# In: None
# Out: None
# Returns: None
scan_and_print_target_devices ()
{
	# Get the list of targets for this SCSI host

	TARGETS=`ls -d target* 2>/dev/null`

	# If we have not targets, then just exit
	
	if test "$TARGETS" = ""
	then
 		exit 0
	fi

	# For each target, get the list of SCSI devices, print it 
	# along with the vendor and model IDs

	for i in $TARGETS
	do
		OLDDIR=`pwd`
		cd $i
 		DEVICES=`ls |  grep -v [a-z]`
 		for j in $DEVICES
 		do
			echo "$j   `cat $j/vendor` `cat $j/model`"
 		done
 	cd $OLDDIR
	done
}

# Name: scan_and_print_rport_devices
# Description: Prints out scsi devices for a particular scsi host that do
#              use rports
# In: None
# Out: None
# Returns: None
scan_and_print_rport_devices () {

        # Get a list of rports

        RPORTS=`ls $SCSIHOSTDIR/device | grep rport`

        # Loop through all the rports and if any have scsi devices associated
        # with them, print the vendor and model ID for that device
        for i in $RPORTS
        do
                TARGETS=`ls -d $SCSIHOSTDIR/device/$i/target* 2>/dev/null`
                for j in $TARGETS
                do
                        DEVICES=`ls $j | grep -v [a-z]`
                        for k in $DEVICES
                        do
                                VENDOR=`cat $j/$k/vendor`
                                MODEL=`cat $j/$k/model`
                                echo "$k   $VENDOR $MODEL"
                        done
                done
        done
}

# Name: scan_and_print_devices
# Description: Prints out scsi devices for a particular scsi host that has
#              the device listing directly in the "device directory"
# In: None
# Out: None
# Returns: None
scan_and_print_devices () {
	DEVICES=`ls | grep -v [a-z]`
	
	for i in $DEVICES
	do
		VENDOR=`cat $i/vendor`
		MODEL=`cat $i/model`
		echo "$i   $VENDOR $MODEL"
	done 
}

#
# Script Main
#

# argument 1 should be the instance number of the device

INSTANCE=$1

if test "$INSTANCE" = ""
then
 echo "A SCSI host instance number needs to be specified"
 exit 1
fi

SCSIHOSTDIR=/sys/class/scsi_host/host${INSTANCE}

# Do a sanity check to make sure that the directory exists

if test -d $SCSIHOSTDIR
then
 cd /sys/class/scsi_host/host${INSTANCE}/device
else
 echo "$SCSIHOSTDIR does not exist"
 exit 1
fi

# Now check to see which type of scan we should do

if [ "`ls $SCSIHOSTDIR/device | grep target`" != "" ]
then
	# We are not using rports, directly scan the target directories
	scan_and_print_target_devices
elif [ "`ls $SCSIHOSTDIR/device | grep rport`" != "" ]
then
	# We are using rports, scan each rport for targets and devices
	scan_and_print_rport_devices
else
	# Try the "device" directory directly
	scan_and_print_devices
fi

exit 0
