#!/bin/bash
function cleanup()
{

if [ -f $TEMPFILE ]; then
   rm -f $TEMPFILE
   rm -f $TEMPFILE1
fi
exit 0
}

function list_valid_children()
{
 device=$1
 match="no"
    while read line ; do
       case "$line" in
	 *Config\ Status:*avail=yes*)
		avail=yes
		if [ "$match" == "yes" ]; then
			break
		fi
		;;
	 *Parent\ ID:\ *)
  	      parentid="${line##*Parent ID: }"
              ;;
	 *Device\ File:* )
		dev="${line##*Device File: }"
		dev="${dev%% (*}"
                if [ "$parentid" == "$USB_CHILD" ]; then
		         match="yes"
	        fi
		;;
        esac  
    done < "$TEMPFILE1"

        
	if [ "$match" == "yes" ]; then
              chkfstab $dev
              if [ "$?" == "$GOOD" ]; then 
	             if [ "$dev" == "$device" ]; then
			    match="yes"
	  	     fi
                     echo "$dev;$USB_DISK_DEV;"
                      match="no"
                      parentid=""

              fi
              break
	fi
}


function chkfstab()
{
 DEV=`grep $1 /etc/fstab | awk {'print $1'}`
 if [ "$1" == "$DEV" ]; then
    return $GOOD
 else 
    return $BAD 
 fi 
} 

function get_type()
{
	type=""
	uniqueid=""
	device="$1"
	avail="no"
	match="no"
	busid=""
	while read line ; do
                case "$line" in
			 *Config\ Status:*avail=yes*)
				avail=yes
				if [ "$match" == "yes" ]; then
					break
				fi
				;;
			 *Unique\ ID:\ *)
				uniqueid="${line##*Unique ID: }"
				;;
			 *Hardware\ Class:\ *)
				type="${line##*Hardware Class: }"
				;;
			 *SysFS\ BusID:\ *)
				busid="${line##*SysFS BusID: }"
				busid="${busid%%:*}"
				grep -q -s "Floppy" /proc/scsi/usb-storage/$busid
				if [ $? -eq 0 ]; then
				   type=usbfloppy
				fi
				;;
			 *Device:*Memory\ Key* )
				type="memorykey"
				;;
			 *Driver:*usb-storage* )
				if [ -f /proc/scsi/usb-storage/$busid ]; then
				  grep -q -s "Floppy" /proc/scsi/usb-storage/$busid
				  if [ $? -eq 0 ]; then
				     type=usbfloppy
				  fi
				fi
				if [ "$type" == "disk" ]; then
					type="usbdisk"
				fi
				if [ "$type" == "memorykey" ]; then
					type="usbdisk"
				fi
				;;	
			 *Driver:*ide-disk* )
				if [ "$type" == "disk" ]; then
				    type="idedisk"
				fi
				;;
			 *Driver:*ide-cdrom* )
				if [ "$type" == "cdrom" ]; then
				    type="idecdrom"
				fi
				;;
 			 *Driver:*ata_piix* | *Driver:*ahci* )
				if [ "$type" == "disk" ]; then
				    type="satadisk"
				fi
				;;
			 *Device\ File:* )
                               
				dev="${line##*Device File: }"
				dev="${dev%% (*}"
				if [ "$dev" == "/dev/fd0" ]; then
				   type="idefloppy"
				fi
				if [ "$dev" == "$device" ]; then
				    match="yes"
				fi
				;;
		esac
	done < "$TEMPFILE"


	if [ "$avail" == "yes" ]; then

           if [ "$type" == "usbdisk" ]; then
              # In fstab?  Some usb keys may have child devices
                 chkfstab $dev
                 if [ "$?" == "$BAD" ]; then
                    USB_CHILD=$uniqueid
                    /usr/sbin/hwinfo --partition > $TEMPFILE1
                    zz=`grep "Device File:" $TEMPFILE1 2>/dev/null| cut -d':' -f2 | cut -d' ' -f2`
                    for i in $zz
                    do
                       list_valid_children $i
                    done
                    type="" #fall through
                 fi
           fi

	   case "$type" in 
		idedisk | satadisk )
		    if [ "$2" == "--all" ]; then
		       echo "$dev;$HARDDISK_DEV;"
		    fi
		    ;;
		idecdrom )
		   echo "/dev/cdrom;$CD_DEV;"
		   ;;
		usbfloppy )
		   echo "$dev;$USB_FLOPPY_DEV;"
		   ;;
		usbdisk )
		   echo "$dev;$USB_DISK_DEV;"
		   ;;
		idefloppy )
		   echo "$dev;$FLOPPY_DEV;"
		   ;;
	   esac
	fi
}
trap "cleanup" 1 2 3 4 5 11 13 15
CD_DEV=1
FLOPPY_DEV=2
USB_DISK_DEV=3    #includes memory keys
USB_FLOPPY_DEV=4
UNKNOWN_DEV=5
HARDDISK_DEV=6
GOOD=1
BAD=0
OUTSTRING=""
USB_CHILD=""
TEMPFILE="/tmp/__hwinfo.out__"
TEMPFILE1="/tmp/__hwinfo1.out__"
# Nothing specified get all possible media info
if [ "$2" == "" ]; then
  /usr/sbin/hwinfo --cdrom > $TEMPFILE
  x=`grep "Device File:" $TEMPFILE 2>/dev/null| cut -d':' -f2 | cut -d' ' -f2`
  for i in $x
  do
    t=`get_type $i`
    OUTSTRING=$OUTSTRING"$t"
  done
  /usr/sbin/hwinfo --floppy > $TEMPFILE
  x=`grep "Device File:" $TEMPFILE 2>/dev/null| cut -d':' -f2 | cut -d' ' -f2`
  for i in $x
  do
    t=`get_type $i`
    OUTSTRING=$OUTSTRING"$t"
  done
  /usr/sbin/hwinfo --disk > $TEMPFILE
  x=`grep "Device File:" $TEMPFILE 2>/dev/null| cut -d':' -f2 | cut -d' ' -f2`
  for i in $x
  do
    if [ "$1" == "--all" ]; then
       t=`get_type $i --all`
    else
       t=`get_type $i`
    fi
    OUTSTRING=$OUTSTRING"$t"
  done
else
 case $2 in
   usbflashkey)
     /usr/sbin/hwinfo --disk > $TEMPFILE
     x=`grep "Device File:" $TEMPFILE 2>/dev/null| cut -d':' -f2 | cut -d' ' -f2`
     for i in $x
     do
       t=`get_type $i`
       OUTSTRING=$OUTSTRING"$t"
     done
      ;;
   usbfloppy)
      /usr/sbin/hwinfo --floppy > $TEMPFILE
      x=`grep "Device File:" $TEMPFILE 2>/dev/null| cut -d':' -f2 | cut -d' ' -f2`
      for i in $x
      do
        if [ "$i" != "/dev/fd0" ]; then
          t=`get_type $i`
          OUTSTRING=$OUTSTRING"$t"
	fi
      done
      ;;
   *) echo "Unknown option"
      exit 1;;
 esac
fi
echo $OUTSTRING
cleanup
