#!/bin/sh
#
# sdcontrol 1.0 2001/8/8 21:33:19 (Hideki Hayami)
#
# Initialize or shutdown a SD card device
#
# The first argument should be either 'insert' of 'eject'.
#

ACTION=$1
DEVICE=/dev/mmcda1
MOUNT_POINT=/mnt/sd
MOUNT_POINT2=/mnt/card

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

wait_release()
{
    count=1
    while true
    do
        umount $MOUNT_POINT2
        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`
    qpe_pid=`cat /var/run/opie.pid`
    #echo qpe_pid = $qpe_pid >> /tmp/sd
    target_pids=`fuser -m $DEVICE | cut -d : -f2`
    #echo $target_pids >> /tmp/sd
    if [ "$target_pids" = "" ]; then
        return
    fi
    is_exist_qpe=`echo $target_pids | grep $qpe_pid` # grep -w won't work
    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 ######


case "$ACTION" in
'insert')
	fsck  $MOUNT_POINT
	mount $MOUNT_POINT
	MOUNT_RES = `mount | grep $DEVICE`
	# If the mount via fstab fails, force it
	if [ "$MOUNT_RES" = "" ]; then
	        mount $DEVICE $MOUNT_POINT
	fi
	mount $MOUNT_POINT2
        #echo mount $? >> /tmp/sd
        ;;
'eject')
        umount $MOUNT_POINT2
        fuser -s -m $DEVICE
        if [ $? = 1 ]; then
                umount $MOUNT_POINT
        fi
        ;;
'compeject')
        is_mount=`mount | grep $DEVICE`
        if [ "$is_mount" = "" ]; then
                exit 0
        fi
        kill_task       # for QPE
        #fuser -k -m $DEVICE > /dev/null
        umount $MOUNT_POINT2
        umount $MOUNT_POINT
        if [ $? != 0 ]; then
                usleep 500000
                umount $MOUNT_POINT2
                umount $MOUNT_POINT
                #echo umount $? >> /tmp/sd
        #else
        #        echo umount >> /tmp/sd
        fi
        ;;
'change')
        $0 compeject
        $0 insert
        ;;
'*')
        exit 1
        ;;
esac

exit 0

