#!/bin/bash
#
# show generic devices for CDROM SCSI devices
#
# needs a mounted /proc filesystem, echo and grep
#
# when invoked with a number, the n-th cdrom device name is
# printed.
#
# example: 'scan_scsi 2' will print the device name for
#          the second cdrom device, namely '/dev/sgc' in my
#          system.
#

NUMBER=0
while read ID; do
	if [ ".${ID%%' '}" = ".Attached devices:" ]; then
		continue;
	fi
	read ignore;
	read ignore type ignore;
	echo $ID '  ' $type ' -> ' /dev/sg`printf "\x$[$NUMBER + 61]"` or /dev/sg$NUMBER
	NUMBER=$(($NUMBER + 1))
#done <test_scanscsi | grep "WORM\|CD-ROM" |
done </proc/scsi/scsi | grep "WORM\|CD-ROM" | \
(DRIVES=1
while read CD_DRIVE; do
	if test $# -ne 0; then
		if test $DRIVES -eq $1; then
			CD_DRIVE=${CD_DRIVE#*-> }
			CD_DRIVE=${CD_DRIVE% or /dev/*}
			echo ${CD_DRIVE}
			break;
		fi
	else
			echo ${CD_DRIVE}
	fi
	DRIVES=$(($DRIVES + 1))
done)
