#!/bin/bash
# $Id: scsi_device.agent 269 2004-08-16 13:07:06Z hare $
#set -x
#
# block hotplug policy agent for Linux kernels
#
# Kernel BLOCK hotplug params include:
#	
#	ACTION=%s [add or remove]
#	DEVPATH=%s
#
# bash required due to echo | while read line ; do : ; done
#


SYSFS=needed
cd /etc/hotplug
. ./hotplug.functions
# DEBUG=yes export DEBUG

if [ "$DEVPATH" = "" ]; then
    mesg Bad SCSI_DEVICE invocation: \$DEVPATH is not set
    exit 1
fi

case $ACTION in
add)
        # work around a race in the scsi layer,
        # type doesnt exist sometimes
        for i in 1 2 3 4 5 ; do
            if [ -f $SYSFS$DEVPATH/device/type ] ; then
                break
            fi
            sleep 1
        done
    
	if [ -f $SYSFS$DEVPATH/device/type ]; then
	    read type < $SYSFS$DEVPATH/device/type
	    case $type in
		0)
		    modprobe sd_mod
		    ;;
		3|4|5)
		    modprobe sr_mod
		    ;;
		*)
		    debug_mesg SCSI_DEVICE "unknown scsi type $type"
		    ;;
	    esac
	fi
	modprobe sg
    ;;

remove)
    :
    ;;
*)
    debug_mesg SCSI_DEVICE $ACTION event not supported
    exit 1 ;;

esac
