#!/bin/sh
# This script reads /proc/scsi/scsi and for each device it creates a
# special file /dev/sg/sc<BUS>d<SCSI ID> with the
# minor number = the index of the device in /proc/scsi
#
# It also creates the files /dev/st/sc<BUS>d<SCSI ID> and
#                           /dev/st/sc<BUS>d<SCSI ID>n
# again with the minor number  = the index of the device in /proc/scsi for
# the rewind and norewind tape devices.
#
# This script is neccessary because the linux /dev/st and /dev/sg devices
# change as devices are added to or subtracted from a bus
#
# The script should normally be run at boot time or if the scsi
# configuration changes

# st = scsi_tape
TAPEDIR=/dev/st
# sg = scsi_generic
SCSIDIR=/dev/sg

if [ ! -d $TAPEDIR ]            # create the dirs if they
then                            #    don't exist
  mkdir $TAPEDIR
fi

if [ ! -d $SCSIDIR ]
then
  mkdir $SCSIDIR
fi

# remove old files

rm    ${TAPEDIR}/sc*    ${SCSIDIR}/sc* 2> /dev/null

G=-1      # index of generic devices in proc/scsi, minor # of sg device
T=-1      # index of tape devices in proc/scsi, # used in sg#

# read each record in proc scsi

cat /proc/scsi/scsi | while read a b c d e f g h 
do
   if [ ! $a = "Attached" ]
   then
      if [ \"$a\" = \"Host:\" ]
      then
         let G=G+1                      # increment index on first line
         bus=`echo $b|tr -d \"[a-z]\"`  #  in /proc/scsi/scsi
	 # force it to use base16 to allow numbers greater than 08
         let id=16#$f			# get the bus and id (w/o leading 0)
	 let ln=16#$h			# get the lun number
         TPDRV=$TAPEDIR/"sc"$bus"d"$id
         SCDRV=$SCSIDIR/"sc"$bus"d"$id
      fi
      if [ \"$c\" = \"Model:\" ]        # just interesting info
      then
         vend=$b
         mod=$d
	 # The Winchester Systems Flash Disk (www.winsys.com)
	 # uses the lun numbers to partition the RAID array
	 # into a collection of disks
	 if [ \"$mod\" = \"FlashDisk\" ]
	 then
	     SCDRV=${SCDRV}"l"$ln
	 fi
      fi
      if [ $a = "Type:" ]
      then
         if [ \"$b\" = \"Sequential-Access\" ]    # identify tape device
         then
            let T=T+1
            let N=T+128
            mknod $TPDRV    c 9 $T	# make the rewind device
            mknod ${TPDRV}n c 9 $N	# make the non-rewind device
            mknod $SCDRV    c 21 $G	#	the scsi passthru device
         else				# for non tape
            mknod $SCDRV    c 21 $G	#	just make scsi device
         fi
      fi
    fi
done

