#!/bin/sh
# Mount and view pen drive
# Tested on Kernel 2.4.27
# needs sudo permission for mount and umount
# Dependes on dmesg output like this:
#
# Attached scsi removable disk sda at scsi0, channel 0, id 0, lun 0
# SCSI device sda: 240640 512-byte hdwr sectors (123 MB)
# sda: Write Protect is off
# sda: sda1                               <<== detected device (sda1)
# WARNING: USB Mass Storage data integrity not assured
# USB Mass Storage device found at 2      <<== identified pattern
#
# (c) Eko M. Budi, 2004
# (c) Vector Linux, 2004
#
# License : GNU GPL

MNT_PENDRIVE=${MNT_PENDRIVE:-/mnt/usb-storage}
case `uname -r` in
    2.2*|2.4*)	OPT="umask=0,shortname=mixwd,quiet" ;;
    *) OPT="dmask=0,fmask=111,shortname,mixed,quiet" ;;
esac

# DO NOT USE Konqueror !, it does not comeback"
export EXPLORER_LIST="rox emelfm xnc"

# Check if already mounted (should be by usb-mount)
LINE=`mount | grep -e "^.* on $MNT_PENDRIVE"`
if [ "$LINE" ]; then
    echo "$MNT_PENDRIVE is already mounted, exploring ..."
    explorer $MNT_PENDRIVE
    exit 0
fi

/sbin/modprobe usb-storage
sleep 1
LINE=`dmesg | grep -B 5 -e "^USB Mass Storage device found" | grep -e '^ sd.:'`
if [ "$LINE" ]; then
    DEV=`echo $LINE | cut -f2 -d:`
    DEV=`echo $DEV`
    echo "Pendrive found on $DEV"
    mkdir -p $MNT_PENDRIVE
    if sudo /bin/mount /dev/$DEV $MNT_PENDRIVE -t vfat -o $OPT; then
	echo "Pendrive mounted on $MNT_PENDRIVE"
	explorer $MNT_PENDRIVE
	sudo /bin/umount $MNT_PENDRIVE
	echo "Pendrive unmounted"
	exit 0
    fi
    message "ERROR" "Could not mount pendrive $DEV" 
    exit 1
fi

message "ERROR" "Could not find pendrive" 
exit 1

