#!/bin/bash
# 
# This executable automount map handles removable devices. 
# 
# It reads a configuration file which maps symbolic names to device identifiers.
# Removable devices are identified by their serial number, while "static"
# devices (such as DVD drives) are identified by their device path.
#
# The first example creates a "stick" entry for partition 1 of USB mass storage
# device DEF107679C83. The second mounts each partition under a directory
# named after its partition number.
#
#    stick  scsi-usb  DEF107679C83:1  -fstype=auto,sync
#    rstick  scsi-usb  DEF107679C83  -fstype=auto,sync
#
# This example creates a "dvd" entry for a SCSI DVD drive:
#
#    dvd  dev  /dev/scd0  -fstype=iso9660,ro
# 

CONFIG="/etc/autofs/auto.media.conf"

echo "$1" | grep -q / && exit 0
fdisk -l > /dev/null 2>&1

# Get USB mass storage devices
grep -v '^#' $CONFIG | awk "{if (\$1 == \"$1\") print \$0}" | while read name type identifier options; do
	case "$type" in
		scsi-usb)
			cd /proc/scsi/usb-storage
			serial=${identifier%:*}
			host=`grep -l "Serial Number: $serial" * | awk '{printf "%s", $1}'`
			if [ -n "$host" ]; then
				partition=${identifier#*:}
				if [ "$serial" = "$partition" ]; then
					(echo "$options"
					for i in /dev/scsi/host${host}/bus0/target0/lun0/part*; do
						echo "/`basename $i | tr -d a-z` :$i"
					done) | awk 'BEGIN {line=""} {if (line != "") print line, "\\"; line=$0} END {print line}'
				else
					echo "$options :/dev/scsi/host${host}/bus0/target0/lun0/part${partition}"
				fi
			fi
		;;
		dev)
			echo "$options	:$identifier"
		;;
	esac
done
