#!/bin/sh
#LGPL 2007 Puppy Linux www.puppyos.com
#based on probedisk3 written by Dougal.
#small mods by BK 16 june 2007
# 21 Jun 2007 BK: force /proc update for usb drives.
#v3.93 10 dec 2007 BK: updated for 2.6.24 kernel, no /dev/hd*.
#v3.97 31jan2008 BK: refinement for detecting kernels with /dev/hd* drives.
#v4.01 10may2008 BK: bugfix for detecting if usb drive.
#v4.01 10may2008 BK: new 2nd-param categories. named 'probedisk2'.
#v4.01 21may2008 BK: zip and ls120 now categorized as 'floppy'.
#v4.02 9jun08 BK: correct detection of usb floppy.
#v403 fixed excessive spaces in description field.
#v406 support for old kernel, /proc/ide, /dev/hd*.
#110126 no longer using SATADRIVES variable in PUPSTATE.
#130201 this script no longer named 'probedisk2' (had probedisk symlink to it). now named 'probedisk'.

. /etc/rc.d/PUPSTATE
#110126 ATADRIVES is all ide/pata/sata drives (not usb, not optical).

if [ -f /root/.usb-drive-log-probedisk ];then #force /proc upate mechanism
 for ONEUSBDRV in `cat /root/.usb-drive-log-probedisk | tr '\n' ' '`
 do
  #disktype /dev/$ONEUSBDRV > /dev/null 2>&1
  dd if=/dev/$ONEUSBDRV of=/dev/null bs=512 count=1 >/dev/null 2>&1 #v4.01 faster.
 done
fi

#mounted drives/partitions...
MNTDDEVS="`mount | cut -f 1 -d ' ' | cut -f 3 -d '/' | grep -E '^hd|^sd|^scd|^sr|^mmc' | tr '\n' ' '`"

#crap, right now, /sys/block does not show my hdb cd/dvd drive, but it is in
#/proc/ide. pathetic kernel! oh well...
if [ ! -e /proc/ide ];then #v3.97
 ALLDRVS="`ls -1 /sys/block | grep -E '^scd|^sd|^mmc|^sr' | tr '\n' ' '`"
else
 ALLDRVS="`ls -1 /sys/block | grep -E '^scd|^sd|^mmc|^sr' | tr '\n' ' '``ls -1 /proc/ide | grep '^hd' | tr '\n' ' '`"
fi
#note: after further testing, the 2.6.21.5 kernel exhibits inconsistent behaviour for hd devices, best to avoid.

for ONEDRV in $ALLDRVS
do
  case $ONEDRV in
   hd*) # ide device, look in /proc/ide for info
     MEDIA="`cat /proc/ide/$ONEDRV/media`"
     [ "$MEDIA" = "disk" ] && MEDIA="drive"
     [ "$MEDIA" = "cdrom" ] && MEDIA="optical"
     INFO="`cat /proc/ide/$ONEDRV/model`"
   ;;
   sd*) # scsi devices, look in /sys/block (all appear as Direct-Access)
     MEDIA="drive"
     VENDOR="`cat /sys/block/$ONEDRV/device/vendor | tr -s ' '`"
     MODEL="`cat /sys/block/$ONEDRV/device/model | tr -s ' '`"
     INFO="$VENDOR$MODEL"
     DRVNAMES="$DRVNAMES `echo -n "$ONEDRV" | cut -b 1-3` "
     
     #log if usb drive (not a ata drive)... v4.01... 110126...
     if [ "`echo "$ATADRIVES" | grep "$ONEDRV"`" = "" ];then
      MEDIA="usbdrv" #v4.01
      echo "$ONEDRV" >> /root/.usb-drive-log-probedisk
      sort -u /root/.usb-drive-log-probedisk > /tmp/usb-drive-log-probedisk-tmp
      mv -f /tmp/usb-drive-log-probedisk-tmp /root/.usb-drive-log-probedisk
      #find out if a usb floppy drive...
      if [ -e /sys/block/${ONEDRV}/size ];then
       [ "`cat /sys/block/${ONEDRV}/size`" = "2880" ] && MEDIA="floppy"
      fi
      #if the floppy diskette not inserted, try this fallback test...
      #some examples: Vendor: NEC Model: USB UF000x Rev: 1.50, Sony USB Floppy Drive, rev 1.10/5.01,
      # MITUMI USB FDD, VenDor: TEAC Model: FD-05PUB, Vendor: COMPAQ Model: USB EXT FLOPPY
      if [ -e /sys/block/${ONEDRV}/device/model ];then
       [ "`grep -E ' FDD| UF000x|Floppy|USB\-FDU|^FD\-|FLOPPY' /sys/block/${ONEDRV}/device/model`" != "" ] && MEDIA="floppy"
      fi
     else
      #find out if it is a removable internal drive (zip, ls120, etc)...
      if [ -e /sys/block/${ONEDRV}/removable ];then
       [ "`cat /sys/block/${ONEDRV}/removable`" = "1" ] && MEDIA="floppy"
      fi
     fi

   ;;
   scd*|sr*) #  scsi cdroms
     MEDIA="optical"
     VENDOR="`cat /sys/block/$ONEDRV/device/vendor | tr -s ' '`"
     MODEL="`cat /sys/block/$ONEDRV/device/model | tr -s ' '`"
     INFO="$VENDOR$MODEL"
   ;;
   mmc*) #/dev/mmcblk0
     MEDIA="card"
     INFO="MMC/SD: `cat /sys/block/$ONEDRV/device/name`"
   ;;
   *)
     continue
   ;;
  esac
  echo "/dev/$ONEDRV|$MEDIA|$INFO"

done

#find out if a mounted device has been unplugged...
#for hotplug drives, remove it and it will disappear from /sys/block, however
#still shows up in 'mount' if hasn't been unmounted.
for ONEMNTD in $MNTDDEVS
do
 case $ONEMNTD in
  hd*|sd*|sr*)
   MNTDDRVs="`echo -n "$ONEMNTD" | cut -b 1-3` "
   ;;
  scd*)
   MNTDDRVs="`echo -n "$ONEMNTD" | cut -b 1-4` "
   ;;
  mmc*)
   MNTDDRVs="`echo -n "$ONEMNTD" | cut -b 1-7` "
   ;;
 esac
 #prints to system log and to stderr...
 [ "`echo "$ALLDRVS" | grep "$MNTDDRVs"`" = "" ] && logger -s "PROBEDISK ERROR: MOUNTED UNPLUGGED $ONEMNTD"
done

###END###
