#!/bin/sh
# name : /usr/sbin/updfstab
# this script is automatically called by hotplug at each plug/unplug event of an usb-storage device. (usb keys, mp3 jukeboxes, digital cameras...)

# Originally writen 
# Dirty hacked by Gurvan Huiban to fit his needs. Thanks to Rene Mages, Christophe Combelles and Daniel Déchelotte for their help.

# The way it works:
# - First, detects all registred usb-storage devices
# - For each device:
#   + Checks if is plugged. If not, unregister device
#   + If the device plugged is registred, check the fstab entry, and modify it if needed
#   + If the device plugged is not registred, do nothing
#   + Automatically mount external hard drive

# be sure the driver is loaded
modprobe /dev/scsi

# list devices
list=`find /proc/scsi -type f|grep usb-storage`

# echo ${list}

# Cleaning devices
for i in $list; do

    # for each device, get infos
    guid=`cat $i |grep GUID:|sed 's/ *GUID: *//g'|sed 's/ /_/g'`
    host=`cat $i |grep Host|cut -c13`
    attached=`cat $i |grep Attached:|cut -c16`

    # if not attached, remove !
    if [ "${attached}" = "N" ]; then
        echo "scsi remove-single-device ${host}" # > /proc/scsi/scsi
    else
        harddrive="04b46830ffffffffffffffec"
        pendrive="ffff13000000aa1234567890"

        # Dirty hack
        # Is there a better way to do WITHOUT using devfs (obsolete)???
        device="sd$(echo ${host} | tr "1-9" "a-i")"
#        if [ "${host}" = "1" ]; then
#            device="sda";
#        fi
#        if [ "${host}" = "2" ]; then
#            device="sdb";    
#        fi
#        if [ "${host}" = "3" ]; then
#             device="sdc";    
#        fi
#        if [ "${host}" = "4" ]; then
#            device="/sdd";    
#        fi
#
        # Check registred devices
        if [ "${guid}" = "${harddrive}" ]; then

#            echo "THIS IS THE HARD DRIVE"

            # This is the external USB hard drive
            # Identify what to which device it is plugged

            olddevice=`cat /etc/fstab | grep /mnt/rip | cut -d1 -f1 | cut -d/ -f3`
#            echo "OLD DEVICE: ${olddevice}"

            # If the device is different
            if [ "${device}" != "${olddevice}" ]; then
                # Modify fstab
#                echo "MODIFYING FSTAB"
#                echo "sed -i /rip/,/+4/s/${olddevice}/${device}/ /etc/fstab"
                sed -i /rip/,/+4/s/${olddevice}/${device}/ /etc/fstab
            fi

            # Mount partition
            mount /mnt/rip
            mount /mnt/windows
            mount /mnt/donnees
            mount /mnt/swap
        fi

        if [ "${guid}" = "${pendrive}" ]; then

#            echo "THIS IS THE PENDRIVE"

            # This is the external USB hard drive
            # Identify what to which device it is plugged
            olddevice=`cat /etc/fstab | grep /pendrive | cut -d1 -f1 | cut -d/ -f3`
#            echo "OLD DEVICE: ${olddevice}"

            # If the device is different
            if [ "${device}" != "${olddevice}" ]; then
                # Modify fstab
#                echo "MODIFYING FSTAB"
#                echo "sed -i /pendrive/s/${olddevice}/${device}/ /etc/fstab"
                sed -i /pendrive/s/${olddevice}/${device}/ /etc/fstab
            fi
        fi
    fi
done
