#!/bin/sh
#
# Initialize or shutdown a SD card device
#
# The first argument should be either 'insert' of 'eject'.
#

ACTION=$1
OPTION=$2
MOUNT_POINT="/mnt/card"
SMB_MOUNT="/home/samba/SD_Card"
INSTALL_DIR="Documents/Install_Files"
#FSTYPE="-t vfat"
FATOPTS="-o noatime,quiet,umask=000,iocharset=utf8"
STAB_FILE="/var/lib/sdcard/stab"
STORAGE_PID_FILE=/var/run/usbdstorage.pid
STORAGE_PID=""
USB_STATUS=""
STORAGE_DEV=""

if [ -r $STORAGE_PID_FILE ]; then
        STORAGE_PID=`cat $STORAGE_PID_FILE`
        USB_STATUS=`cat /proc/usb-storage | grep "USB status" | cut -d : -f 2`
        STORAGE_DEV=`cat /etc/hotplug/usbdstorage.conf`
fi

###### for QPE ######
get_pid()
{
    echo $1
}

wait_release()
{
    count=1
    while true
    do
        umount $MOUNT_POINT
        if [ $? = 0 ]; then
            #echo umount >> /tmp/sd
            return
        fi
        echo count=$count >> /tmp/sd
        if [ `expr $count \>= 500` = 1 ]; then
            #echo time out >> /tmp/sd
            return
        fi
        count=`expr $count + 1`
        usleep 200000
    done
}

kill_task()
{
    ps_line=`ps ax | grep -w 'qpe$'`
    qpe_pid=`get_pid $ps_line`
    #echo qpe_pid = $qpe_pid >> /tmp/sd
    target_pids=`fuser -m /dev/$DEVICE | cut -d : -f2`
    #echo $target_pids >> /tmp/sd
    if [ "$target_pids" = "" ]; then
        return
    fi
    is_exist_qpe=`echo $target_pids | fgrep -w $qpe_pid`
    if [ "$is_exist_qpe" = "" ]; then
	kill -9 $target_pids
        #echo kill -9 $target_pids >> /tmp/sd
    else
        #echo "found qpe!!!" >> /tmp/sd
	target_pids=`echo $target_pids | sed -e "s/$qpe_pid//"`
	if [ "$target_pids" != "" ]; then
            kill -9 $target_pids
            #echo kill -9 $target_pids >> /tmp/sd
        fi
        wait_release
        exit 0
    fi
}
###### for QPE ######

read_stab()
{
	while read SOCKET CLASS DRIVER INSTANCE DEVICE MAJOR MINOR
	do
		if [ "$SOCKET" != "Socket" ]; then
			return 0;
		fi
	done
	return 1
}

if [ "$ACTION" = "eject" -a "$OPTION" = "compeject" ]; then
	ACTION="compeject"
fi

case "$ACTION" in
'insert')
	read_stab < ${STAB_FILE}
	
	if [ "${CLASS}" = "memory" ]; then
	        if [ "$USB_STATUS" = "USB_CONNECT" ]; then
        	        if [ "$STORAGE_DEV" = "/dev/$DEVICE" ]; then
                	        if [ $STORAGE_PID ]; then
                        	        kill -HUP "$STORAGE_PID"
	                        fi
        	                exit 0
	                fi
        	fi
		mount ${FSTYPE} ${FATOPTS} /dev/${DEVICE} ${MOUNT_POINT}
		is_mount=`mount | fgrep /dev/$DEVICE`
		if [ "$is_mount" = "" ]; then
			mount $FSTYPE /dev/$DEVICE $MOUNT_POINT
		fi
		chkmntsh ${MOUNT_POINT}
		#if [  -d $SMB_MOUNT ] ; then
		#	rm -rf $SMB_MOUNT
		#fi
		#ln -s $MOUNT_POINT $SMB_MOUNT
		#mkdir -p $MOUNT_POINT/$INSTALL_DIR
	        if [ "$STORAGE_DEV" = "/dev/$DEVICE" ]; then
        	        if [ $STORAGE_PID ]; then
                	        kill -HUP "$STORAGE_PID"
	                fi
        	fi
    		if [ -f /usr/bin/ipkg-link ]; then
        	    /usr/bin/ipkg-link mount $MOUNT_POINT
    		fi
	fi
	;;
'eject')
	read_stab < ${STAB_FILE}

        if [ "$STORAGE_DEV" = "/dev/$DEVICE" ]; then
                if [ $STORAGE_PID ]; then
                        kill -HUP "$STORAGE_PID"
                fi
        fi
       	fuser -s -m /dev/$DEVICE
       	if [ $? = 1 ]; then
               	umount $MOUNT_POINT
       	        #rm $SMB_MOUNT
	else
		exit 1
        fi
        ;;
'compeject')
	read_stab < ${STAB_FILE}

        if [ "$STORAGE_DEV" = "/dev/$DEVICE" ]; then
                if [ $STORAGE_PID ]; then
                        kill -HUP "$STORAGE_PID"
                fi
        fi
        is_mount=`mount | fgrep /dev/$DEVICE`
       	if [ "$is_mount" = "" ]; then
		exit 0
        fi
       	kill_task       # for QPE
        #fuser -k -m /dev/$DEVICE > /dev/null
       	umount $MOUNT_POINT
        if [ $? != 0 ]; then
       	        usleep 500000
               	umount $MOUNT_POINT
                #echo umount $? >> /tmp/sd
       	#else
        #        echo umount >> /tmp/sd
       	fi
	#rm $SMB_MOUNT
        ;;
'change')
        $0 compeject
        $0 insert
        ;;
'*')
        exit 1
        ;;
esac

exit 0

