#!/bin/sh

# rc.sd 1.00 2001/08/08 22:40:44 (Hideki Hayami)
#
# Tags for init configuration tools
#
# processname: sdmgr
# pidfile: /var/run/sdmgr.pid
# control script: /etc/sdcontrol
# description: currently SD support is only for memory devices \
#              which is used as block device.

KERNEL=`/bin/uname -r`
SD_MODULE=sharp_mmcsd_m
MODULE=/lib/modules/$KERNEL/kernel/drivers/block/$SD_MODULE.o
STAB_DIR="/var/lib/sdcard/"
STAB_FILE="stab"
SDMGR="/sbin/sdmgr"
SDMGR_OPT="-d /dev/sd_slotstat -c /etc/sdcard/sd.conf"
# DEBUG MODE
#MODULE=/mnt/cf/$SD_MODULE.o
#SDMGR="/mnt/cf/sdmgr"
#SDMGR_OPT="-d /dev/sd_slotstat -c /mnt/cf/sd.conf -D"

SDMGR_PID="/var/run/sdmgr.pid"

usage()
{
    echo "Usage: $0 {start|stop|status|restart|reload}"
}


if [ $# -lt 1 ] ; then usage ; break ; fi
action=$1

case "$action" in

start)
#    echo -n "Starting SD services:"

    if [ -d $STAB_DIR ]; then
	rm -rf $STAB_DIR 2>/dev/null
    fi
    /bin/mkdir -p $STAB_DIR 2>/dev/null
	
    if [ -f $MODULE ] ; then
	/sbin/insmod $MODULE 2>/dev/null >/dev/null
	if [ -s ${SDMGR_PID} ] && \
	    kill -0 `cat ${SDMGR_PID}` 2>/dev/null ; then
	    echo " sdmgr is already running."
	else
#	    echo " sdmgr."
	    ${SDMGR} ${SDMGR_OPT}
	fi
    fi
    sleep 1
    ;;

stop)
#    echo -n "Shutting down SD services:"
    if [ -s ${SDMGR_PID} ] ; then
	PID=`cat ${SDMGR_PID}`
	kill $PID 2>/dev/null
	echo -n " sdmgr"
	# Give cardmgr a few seconds to handle the signal
	kill -0 $PID 2>/dev/null && sleep 2 && \
	kill -0 $PID 2>/dev/null && sleep 2 && \
	kill -0 $PID 2>/dev/null && sleep 2 && \
	kill -0 $PID 2>/dev/null
    fi
    if grep -q $SD_MODULE /proc/modules ; then
	echo -n " modules"
	/sbin/rmmod $SD_MODULE
    fi
    echo "."
    ;;

status)
    ;;

restart|reload)
    $0 stop
    $0 start
    ;;

*)
    usage
    ;;

esac

exit 0
