#!/bin/sh
#
# mount/unmount script for usb-storage device
#

###################
# USE_SUPERMOUNT:
#	If you don't want to use supermount-ng,
#	Set USE_SUPERMOUNT=no

USE_SUPERMOUNT=yes

#
#functions
#

function allAttachedScsiUsb {
    local cdRom=0
    local nonCd=0
    #determine dev name.
    for procEntry in `find /proc/scsi/ -path '/proc/scsi/usb-storage*' -type f | sort`; do
        hostNum=`echo $procEntry | awk -F"[-/]" '{ print $NF }'`
        devType=`grep -A 2 scsi$hostNum /proc/scsi/scsi | awk '/Type/{ print $2 }'`
 
        if [ "$devType" == "CD-ROM" ]; then
            scsiDev="/dev/scd"$cdRom
            cdRom=`expr $cdRom + 1`
        else
            scsiDev=`echo $nonCd | awk '{ print "/dev/sd" substr("abcdefghijklmnopqrstuvwxyz", $1+1, 1) }'`
            nonCd=`expr $nonCd + 1`
        fi
        #dev name & product name is concatenated in 1 word.(for convinience)
        if grep -q 'Attached: Yes' $procEntry; then
            productName=`grep Product $procEntry | cut -b16- | tr " " "_"`
            echo $scsiDev":"$productName
        fi
    done
}

function allPartFromFdisk {
    #fdisk sometimes take a few seconds to get disk info.
    sleep 0.5s
    /sbin/fdisk -l $1 | awk '{print $1}' | grep "/dev/"
}

function allDevFromFstab {

    #extract supermount device
    grep usb-mount /etc/fstab | grep supermount | awk '{print $4}' | \
        awk -F"[-,]" '{ sub("dev=","",$1); print $1}' | cut -b-8

    #extract supermount device
    grep usb-mount /etc/fstab | grep -v supermount | awk '{print $1}'
}

function ckMountable {
    if [ ! -e /tmp/ckmount ]; then
        mkdir -p /tmp/ckmount
    fi
    #temporaryly mount for check mountable
    if mount $1 /tmp/ckmount; then
        umount /tmp/ckmount
        return 0
    else
        return 1
    fi
}

function autoMount {
    local partNum=`echo $1 | cut -b9-`
    local mountPoint="/mnt/""$2""$partNum"

    #create mount dir if not exist
    if [ ! -e $mountPoint ]; then
        mkdir -p $mountPoint
    fi

    #add an fstab entry
    case "$USE_SUPERMOUNT" in
        yes|Yes|YES)
            echo -e \
            "none\t""$mountPoint""\tsupermount\t""dev=$1"",fs=ext2:vfat:iso9660:udf\t0 0\t""usb-mount" \
            >> /etc/fstab;
        ;;
        no|No|NO)
            echo -e \
            "$1\t""$mountPoint\t""auto\t""defaults,users\t""0 0\t""usb-mount" \
            >> /etc/fstab;
        ;;
    esac

    mount $mountPoint
}

#
# main
#


if [ "$ACTION" = "add" ]; then
    for devInfo in $(allAttachedScsiUsb); do
        #separate return value
        scsiDev=`echo $devInfo | awk -F ":" '{ print $1 }'`
        productName=`echo $devInfo | awk -F ":" '{ print $2 }'`
    
        #if device already exist in fstab or already mount,do nothing.
        if ! grep -q "$scsiDev" /proc/mounts && ! grep -q "$scsiDev" /etc/fstab; then
            #try to mount whole disk first
            if ckMountable $scsiDev; then
                autoMount $scsiDev $productName
            else
                #then try to mount each partitions
                for procPart in $(allPartFromFdisk $scsiDev); do
                    if ckMountable $procPart; then
                        autoMount $procPart $productName
                    fi
                done
            fi

            #if all mount was fail, finaly mount whole disk.
            #(This is useful for removable media like flpppy disk.)
            if ! grep -q "$scsiDev" /proc/mounts; then
                autoMount $scsiDev $productName
            fi
        fi
    done

elif [ "$ACTION" = "remove" ]; then
    for fstabEntry in $(allDevFromFstab); do
        devInfo=$(allAttachedScsiUsb)
        if ! echo $devInfo | grep -q "$fstabEntry" ; then
            mountPoint=`grep "$fstabEntry" /etc/fstab | awk '{print $2}'`
            umount -l $mountPoint

            #erase /etc/fstab entry.
            grep -v "$fstabEntry" /etc/fstab > /etc/.fstab.new
            mv -f /etc/.fstab.new /etc/fstab

            #delete mount point.
            if [ -e $mountPoint ]; then
                rmdir $mountPoint
            fi
        fi
    done
fi

