#!/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

[ -z "$PCIC" ] && PCIC="yenta_socket"

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
		_modprobe single $PCIC $PCIC_OPTS
		_modprobe single ds
		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
		touch /var/lock/subsys/pcmcia
	fi
	;;
  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
	ok
	;;
  status)
	status cardmgr
	exit $?
	;;
  restart|force-reload)
	$0 stop
	$0 start
	exit $?
	;;
  *)
	msg_usage "$0 {start|stop|restart|force-reload|status}"
	exit 3
	;;
esac

exit 0
