#!/bin/sh
#
# chkconfig:	2345 15 85
# 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.

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

# Source networking configuration.
. /etc/sysconfig/network

# PCMCIA configuration
. /etc/sysconfig/pcmcia

if is_no "${PCMCIA}" ; then exit 0 ; fi

if [ "$PCIC" = "" ] ; then
	echo "PCIC not defined in /etc/sysconfig/pcmcia"
	exit 1
fi

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

case "$1" in
    start)
	SC=/var/lib/pcmcia/scheme
	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
	fgrep -q pcmcia /proc/devices
	if [ $? -ne 0 ] ; then
		show "Loading PCMCIA modules"
		/sbin/modprobe $PCIC $PCIC_OPTS
		/sbin/modprobe ds
		deltext
		ok
	fi
	if [ -s /var/run/cardmgr.pid ] && \
		kill -0 `cat /var/run/cardmgr.pid` 2>/dev/null ; then
		msg_Already_Running cardmgr
	else
		msg_starting cardmgr
		if [ -r /var/lib/pcmcia/stab ] ; then
			cat /var/lib/pcmcia/stab | cleanup
		fi
		daemon /sbin/cardmgr $CARDMGR_OPTS
	fi
	touch /var/lock/subsys/pcmcia
	;;
    stop)
	if [ -s /var/run/cardmgr.pid ] ; then
		msg_stopping cardmgr
		killproc cardmgr
		rm -f /var/lock/subsys/pcmcia
	else
		msg_Not_Running cardmgr
	fi
	show "Unloading PCMCIA modules"
	busy
	if fgrep -q "ds  " /proc/modules ; then
		/sbin/rmmod ds
		/sbin/rmmod $PCIC
		/sbin/rmmod pcmcia_core
	fi
	deltext
	ok
	;;
    status)
	status cardmgr
	exit $?
	;;
    restart|reload)
	$0 stop
	$0 start
	exit $?
	;;
    *)
	msg_Usage "$0 {start|stop|status|restart|reload}"
	exit 1
	;;
esac

exit 0
