#!/bin/sh

# rc.pcmcia 1.45 2002/08/08 06:43:43 (David Hinds)
#
# This is designed to work in BSD as well as SysV init setups.  See
# the HOWTO for customization instructions.

# Tags for Red Hat init configuration tools
#
# chkconfig: 2345 08 96
# processname: cardmgr
# pidfile: /var/run/cardmgr.pid
# config: /etc/pcmcia/config
# config: /etc/pcmcia/config.opts
# description: PCMCIA support is usually to support things like ethernet \
#              and modems in laptops.  It won't get started unless \
#              configured so it is safe to have it installed on machines \
#              that don't need it.

# Exit if pcmcia-cs is not installed
test -x /sbin/cardmgr || exit 0

# Source function library.
. /etc/init.d/functions

# Save option values passed in through the environment
for N in PCMCIA PCIC PCIC_OPTS CORE_OPTS CARDMGR_OPTS SCHEME ; do
    V=`eval echo '$'$N` ; if [ "$V" ] ; then eval ENV_$N=\"$V\" ; fi
done

# Source PCMCIA configuration, if available
SourceIfNotEmpty /etc/sysconfig/pcmcia || exit

for N in PCMCIA PCIC PCIC_OPTS CORE_OPTS CARDMGR_OPTS SCHEME ; do
    V=`eval echo '$'ENV_$N` ; if [ "$V" ] ; then eval $N=\"$V\" ; fi
done

[ "$PCMCIA" = yes ] || exit 0
KERNEL_VER=`uname -r | cut -d. -f1-2`

LOCKFILE=/var/lock/subsys/pcmcia

usage()
{
    echo "Usage: ${0##*/} {start|stop|status|restart|reload|condrestart|condreload}"
}

cleanup()
{
    while read SN CLASS MOD INST DEV EXTRA ; do
	if [ "$SN" != "Socket" ] ; then
	    /etc/pcmcia/$CLASS stop $DEV 2> /dev/null
	fi
    done
}

start()
{
	echo -n "Starting PCMCIA services: "
	touch "$LOCKFILE" 2>/dev/null

	if [ -d /var/lib/pcmcia ] ; then
	    SC=/var/lib/pcmcia/scheme
	    RUN=/var/lib/pcmcia
	else
	    SC=/var/run/pcmcia-scheme
	    RUN=/var/run
	fi
	if [ -L $SC -o ! -O $SC ] ; then rm -f $SC ; fi
	if [ ! -f $SC ] ; then umask 022 ; touch $SC ; fi
	if [ "$SCHEME" ] ; then umask 022 ; echo $SCHEME > $SC ; fi
	    
	if ! fgrep -q pcmcia /proc/devices ; then
	    if [ -x /sbin/modprobe ] ; then
		/sbin/modprobe pcmcia_core $CORE_OPTS
		/sbin/modprobe $PCIC $PCIC_OPTS >/dev/null 2>&1
		if [ $KERNEL_VER = 2.6 ]; then
		    /sbin/modprobe pcmcia || break
		else
		    /sbin/modprobe ds || break
		fi
	    fi
	fi

	if [ -s /var/run/cardmgr.pid ] && \
	    kill -0 `cat /var/run/cardmgr.pid` 2>/dev/null ; then
	    echo "cardmgr is already running."
	else
	    if [ -r $RUN/stab ] ; then
		cat $RUN/stab | cleanup
	    fi
	    /sbin/cardmgr $CARDMGR_OPTS
	fi
	echo "done."
	EXITCODE=0
}

stop()
{
	echo -n "Shutting down PCMCIA services: "
	/sbin/cardctl eject > /dev/null 2>&1
	if [ -s /var/run/cardmgr.pid ] ; then
	    PID=`cat /var/run/cardmgr.pid`
	    kill $PID
	fi
	killall -q "CardBus Watcher"
	if [ $KERNEL_VER = 2.6 ]; then
	    if fgrep -q "pcmcia " /proc/modules ; then
	    	fgrep -q "^ide[-_]cs" /proc/modules && /sbin/rmmod ide-cs 2>/dev/null
		/sbin/rmmod pcmcia 2>/dev/null
		/sbin/rmmod $PCIC 2>/dev/null
		/sbin/rmmod rsrc_nonstatic 2>/dev/null
		/sbin/rmmod pcmcia_core 2>/dev/null
	    fi
	else
	    if fgrep -q "ds  " /proc/modules ; then
		/sbin/rmmod ds 2>/dev/null
		/sbin/rmmod $PCIC 2>/dev/null
		/sbin/rmmod pcmcia_core 2>/dev/null
	    fi
	fi
	echo "done."
	rm -f "$LOCKFILE"

	# we do this because when we stop the service, the ide stop script
	# gets run before the card disappears
	EXITCODE=0
}

restart()
{
	stop
	sleep 3
	start
	EXITCODE=$?
}

status()
{
	pid=`/sbin/pidof cardmgr`
	if [ -n "$pid" ] ; then
	    echo "cardmgr (pid $pid) is running..."
	    EXITCODE=0
	else
	    echo "cardmgr is stopped"
	    EXITCODE=3
	fi
}

EXITCODE=1
for x in "1" ; do

    if [ "$PCIC" = "" ] ; then
        echo "PCIC module not defined in startup options!"
        break
    fi

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

    case "$action" in

	start)
		if [ ! -e "$LOCKFILE" ]; then
			start
		fi
		;;

	stop)
		stop
		;;

	status)
		status
		;;

	restart|reload)
		restart
		;;

	condstop)
		if [ -e "$LOCKFILE" ]; then
			stop
		fi
		;;
	condrestart)
		if [ -e "$LOCKFILE" ]; then
			restart
		fi
		;;

	*)
		usage
		;;

    esac

done

# Only exit if we're in our own subshell
case $0 in *rc.pcmcia) exit $EXITCODE ;; esac
