#!/bin/sh

# Copyright (C) 2007 OpenWrt.org

do_mount() {
    local node=$1
    grep -q /dev/${node} /proc/mounts
    if [ $? = 0 ]; then
        logger -t ${TAG} "${DEVICENAME} - /dev/${node} already mounted, skipping ..."
    else
	mnt=/mnt/usb_${node}
	logger -t ${TAG} "${DEVICENAME} - mounting /dev/${node} on ${mnt} ID $2"
	mkdir -p ${mnt}
	case "$2" in
	    83)
	        mount /dev/${node} ${mnt} -t ext3 -o noatime,nodiratime || mount /dev/${node} ${mnt} -o noatime,nodiratime
	        chmod 777 ${mnt}
	        ;;
	    4|6|b|c|e|f)
	    	mount /dev/${node} ${mnt} -o noatime,nodiratime,uid=65534,gid=65534,umask=0,iocharset=utf8
	        ;;
	    7)
	        if [ -e /usr/bin/ntfs-3g ]; then
	            ntfs-3g /dev/${node} ${mnt}
	        else
	            logger -t ${TAG} "Unsupported partition id $2"
	            return 1
	        fi
	        ;;
	    *)
	        logger -t ${TAG} "Unsupported partition id $2"
	        return 1
	        ;;
	esac
        id=${node}
        if [ -f ${USBSYS}/serial ]; then
            id=$(cat ${USBSYS}/serial)
            id=`echo "${id}" | sed 's/ /_/g'`
            id=${id}_$(expr substr ${node} 4 1)
        fi
        share=${RUN}/share_${id}
        rm -f ${share}
        ln -s ${mnt} ${share}
        echo ${share} > ${RUN}/dev2share.${node}
        echo ${node} > ${RUN}/${DEVICENAME}.${node}
        echo ${DEVICENAME} > ${RUN}/usbdev.${node}
        [ -f ${USBSYS}/product ] && cp ${USBSYS}/product ${RUN}/info.${node}
    fi
}

TAG=`basename $0`
logger -t ${TAG} "${DEVICENAME}: usb device is mass storage"
i=0
m=0

USBID=$(echo ${DEVICENAME} | awk -F: '{print $1}')
USBSYS=/sys/bus/usb/devices/${USBID}
RUN=/var/run/usb

[ -d ${RUN} ] || mkdir -p ${RUN}

PID=$$

while [ $i -le 15 ]; do
	i=$(($i+1))
	cd /sys${DEVPATH}
        BLK=$(find /sys${DEVPATH}/host* -type l -name block:\* 2>/dev/null)
	[ -n "${BLK}" ] && {
            #logger -t ${TAG} "Have some block devices: ${BLK}"
	    for blk in ${BLK}; do
                #logger -t ${TAG} "Investigate ${blk}"
		cd ${blk}
		#sleep 2
		#ls /dev/ > /tmp/d
                nodes=$(find sd* -type d 2>/dev/null| grep -v "/")
                #[ -n "${nodes}" ] && logger -t ${TAG} "Found device nodes: ${nodes}"
                [ -n "${nodes}" ] ||{
                    do_mount ${blk##*block:} c
                    exit 0
                }		
                for node in ${nodes}; do
                    m=$(($m+1))
	            if [ -e /sbin/fdisk ]; then
                        # The second parameter from fdisk (*) is optional - remove it with sed
	                partition_id=$(/sbin/fdisk -l | grep $node | sed -e 's/*/ /g' | awk '{print $5}')
	                if [ -n $partition_id ]; then
                            do_mount $node $partition_id
                        else
                            logger -t ${TAG} "Unable to get partition ID"
                        fi
                    else
                        # defaults to FAT file system
                        do_mount $node c
                    fi
		done
	    done
            if [ $m -gt 0 ]; then
                logger -t ${TAG} "Did $m mount attempt(s)"
                exit 0
            fi
        }
	logger -t ${TAG} "waiting on usb drive ${DEVICENAME}: $i ..."
	sleep 2
done
logger -t ${TAG} "No (new) block devices mounted"
