#!/bin/sh
#
# sdcontrol 1.0 2001/8/8 21:33:19 (Hideki Hayami)
# corrected 6/4/2004 to find swapfiles
# Initialize or shutdown a SD card device
#
# The first argument should be either 'insert' of 'eject'.
#
# 2007-01-31 GROG! (uber.grog@gmail.com) Mount all sd card filesystems listed in /etc/fstab unless specified otherwise

ACTION=$1
DEVICE=/dev/mmcda1
MOUNT_POINT=/mnt/card
SMB_MOUNT=/home/samba/SD_Card
INSTALL_DIR=Documents/Install_Files
FSTYPE="-t auto"
FSOPTS="-o noatime"
FATTYPE="-t vfat"
FATOPTS="-o noatime,quiet,umask=000,uid=500,gid=500,iocharset=utf8"

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
        swapoff  ${MOUNT_POINT}/.swapfile 
        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 $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
}

mount_fn () 
{
  # This will first attempt to mount the SD device with the FAT options if that
  # fails then we will move to the auto mounting with the default FS options

  mount $FATTYPE $FATOPTS $DEVICE $MOUNT_POINT > /dev/null 2>&1

  if [ $? -ne 0 ]; then
	echo "The Fat mounting failed for $MOUNT_POINT let's try the generic option"
	mount $FSTYPE $FSOPTS $DEVICE $MOUNT_POINT  > /dev/null 2>&1
  else
    echo "$MOUNT_POINT Mounted as fat, updating permissions"
	chmod -R a+w $MOUNT_POINT/*
  fi
}


###### for QPE ######

mount_card_func() {

#set -x # TESTING

case "$ACTION" in
'insert')
        if [ "$USB_STATUS" = "USB_CONNECT" ]; then
                if [ "$STORAGE_DEV" = "$DEVICE" ]; then
                        if [ $STORAGE_PID ]; then
                                kill -HUP "$STORAGE_PID"
                        fi
                        exit 0
                fi
        fi
        
        mount_fn

        if [ -f ${MOUNT_POINT}/.swapfile ]; then
              swapon  ${MOUNT_POINT}/.swapfile
        fi
	
#	MOUNT_RES = `mount | grep $DEVICE`
#	if [ "$MOUNT_RES" = "" ]; then
#
#	        mount_fn
#
#                if [ -f ${MOUNT_POINT}/.swapfile ]; then
#                   swapon  ${MOUNT_POINT}/.swapfile
#                fi
#	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
        #echo mount $? >> /tmp/sd
        if [ "$STORAGE_DEV" = "$DEVICE" ]; then
                if [ $STORAGE_PID ]; then
                        kill -HUP "$STORAGE_PID"
                fi
        fi
        ;;
'eject')
        if [ "$STORAGE_DEV" = "$DEVICE" ]; then
                if [ $STORAGE_PID ]; then
                        kill -HUP "$STORAGE_PID"
                fi
        fi
	
        if [ -f ${MOUNT_POINT}/.swapfile ]; then
           swapoff  ${MOUNT_POINT}/.swapfile
        fi
	
        fuser -s -m $DEVICE
        if [ "$?" = "1" ]; then
                umount $MOUNT_POINT
                rm $SMB_MOUNT
	else
		exit 1
        fi
        ;;
'compeject')
        if [ "$STORAGE_DEV" = "$DEVICE" ]; then
                if [ $STORAGE_PID ]; then
                        kill -HUP "$STORAGE_PID"
                fi
        fi
        is_mount=`mount | fgrep $DEVICE`
        if [ "$is_mount" = "" ]; then
                exit 0
        fi
        kill_task       # for QPE
        #fuser -k -m $DEVICE > /dev/null
        swapoff  ${MOUNT_POINT}/.swapfile 
        umount $MOUNT_POINT
        if [ $? != 0 ]; then
                usleep 500000
                swapoff  ${MOUNT_POINT}/.swapfile 
                umount $MOUNT_POINT
                #echo umount $? >> /tmp/sd
        #else
        #        echo umount >> /tmp/sd
        fi
		rm $SMB_MOUNT
        ;;
'change')
        $0 compeject
        $0 insert
        ;;
'*')
        exit 1
        ;;
esac

} # mount_card_func

#set -x # TESTING

if [ $# -eq 1 ]; then
   # mount/umount all filesystems from /etc/fstab
   while read fstab_line; do
       set -- $fstab_line
       case $1 in *mmcda*)
           DEVICE=$1
           MOUNT_POINT=$2
           FATOPTS="-o $4"
           mount_card_func
       esac
   done < /etc/fstab
else
   DEVICE=$1
   MOUNT_POINT=$2
   mount_card_func
fi

exit 0

