#!/bin/bash

DRIVESDIR=/tmp/drives
HOTPLUGDIR=/tmp/drives/.hotplug
#
# Module location
#

KERNEL=`uname -r`
MODULE_DIR=/lib/modules/$KERNEL

USB_MODMAP=$MODULE_DIR/modules.usbmap
LTSP_USB_MODMAP=/etc/usblist


mount_device_num()
{
  DEVNUM=$1
  mkdir ${HOTPLUGDIR}/host${DEVNUM}

  SCSI_DIR=/dev/scsi/host${DEVNUM}/bus0/target0/lun0

  PRODUCT_NAME=`grep Product /proc/scsi/usb-storage-${DEVNUM}/${DEVNUM} | \
                sed -e 's/.*: //' | tr ' ' '_'`

  MODEL=`grep -h -s "^${PRODUCT_NAME}" /etc/devname_map.local /etc/devname_map|\
         head -1 | sed 's/	/ /g' | tr -s ' ' | cut -d' ' -f 2`

  [ -n "${MODEL}" ] && PRODUCT_NAME="${MODEL}"

  COUNT=1
  while [ -L ${DRIVESDIR}/${PRODUCT_NAME}_${COUNT} ]; do
    COUNT=`expr $COUNT + 1`
  done

  LINK_NAME=${PRODUCT_NAME}_${COUNT}

  if [ -b $SCSI_DIR/part1 ] ; then
     echo "${DRIVESDIR}/${LINK_NAME}" >${HOTPLUGDIR}/host${DEVNUM}/LINK_NAME
     mount -t supermount -o fs=auto,dev=$SCSI_DIR/part1,--,rw none \
       $HOTPLUGDIR/host${DEVNUM}
     cd $HOTPLUGDIR/host${DEVNUM}
     ln -s $HOTPLUGDIR/host${DEVNUM} "${DRIVESDIR}/${LINK_NAME}"
     return
  fi
  if [ -b $SCSI_DIR/cd ] ; then
     echo "${DRIVESDIR}/${LINK_NAME}" >${HOTPLUGDIR}/host${DEVNUM}/LINK_NAME
     mount -t supermount -o fs=auto,dev=$SCSI_DIR/cd,--,ro none \
       $HOTPLUGDIR/host${DEVNUM}
     cd $HOTPLUGDIR/host${DEVNUM}
     ln -s $HOTPLUGDIR/host${DEVNUM} "${DRIVESDIR}/${LINK_NAME}"
     return
  fi
  if [ -b $SCSI_DIR/disc ] ; then
     echo "${DRIVESDIR}/${LINK_NAME}" >${HOTPLUGDIR}/host${DEVNUM}/LINK_NAME
     mount -t supermount -o fs=auto,dev=$SCSI_DIR/disc,--,rw none \
       $HOTPLUGDIR/host${DEVNUM}
     cd $HOTPLUGDIR/host${DEVNUM}
     ln -s $HOTPLUGDIR/host${DEVNUM} "${DRIVESDIR}/${LINK_NAME}"
     return
  fi
}

#
# If we've activated hotplugging, then this directory should be here, if not,
# exit quitely.  Simple way to "disable" hotplugging.
#

if [ ! -d $HOTPLUGDIR ] ; then
  exit 0
fi

#
# ADD event
#

if [ $ACTION = "add" ] ; then

  #
  # USB add event
  #

  if [ $DEVFS = "/proc/bus/usb" ] ; then

     #
     # Get vendor and product ID, and find them in the module map
     #

     ID_VENDOR=`echo $PRODUCT | cut -d'/' -f 1`
     ID_PRODUCT=`echo $PRODUCT | cut -d'/' -f 2`
     MODULE=`grep -h $ID_VENDOR $USB_MODMAP $LTSP_USB_MODMAP | \
                grep $ID_PRODUCT | cut -d' ' -f 1 | head -1`

     #
     # If we got a module, load it, then figure out what to do
     #

     if [ ! -z $MODULE ] ; then
       lsmod | grep $MODULE > /dev/null
       if [ $? -eq 1 ] ; then
         modprobe $MODULE
       fi
     fi
        
     if [ $MODULE = "usb-storage" ] ; then
       lsmod | grep "s[dr]_mod" > /dev/null
       if [ $? -eq 1 ] ; then
         modprobe sd_mod && modprobe sr_mod
       fi

       #
       # OK, we've got our hotplug device plumbed.  Lets do some mounting
       #

       #
       # This needs to be generalized
       #

       for i in 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19; do
         USBDEV="/proc/scsi/usb-storage-${i}/${i}"
         if [ -f ${USBDEV} ] ; then
           grep "Attached: Yes" $USBDEV > /dev/null
           if [ $? -eq 0 ] ; then
             if [ ! -d ${HOTPLUGDIR}/host${i} ] ; then
               mount_device_num ${i}
             fi
           fi
         fi
       done
     fi  # module = usbstorage
   fi    # devfs = procusb
#
# REMOVE action
#

elif [ $ACTION = "remove" ] ; then

  if [ $DEVFS = "/proc/bus/usb" ] ; then

    #
    # Scan and see if all devices are disconnected.  If they are, then
    # we can unload the "usb-storage" module
    #

    for i in 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19; do
       USBDEV="/proc/scsi/usb-storage-${i}/${i}"
       if [ -f ${USBDEV} ] ; then
       grep "Attached: No" $USBDEV > /dev/null
       if [ $? -eq 0 ] ; then
         if [ -d ${HOTPLUGDIR}/host${i} ] ; then
            umount -l ${HOTPLUGDIR}/host${i}
            if [ -f ${HOTPLUGDIR}/host${i}/LINK_NAME ]; then
	      rm -f `cat ${HOTPLUGDIR}/host${i}/LINK_NAME`
              rm ${HOTPLUGDIR}/host${i}/LINK_NAME
            fi
            rmdir ${HOTPLUGDIR}/host${i}
         fi
       fi
      fi
    done

    fi
  fi
