#! /bin/bash
# Copyright (c) 1996-2004 SuSE Linux AG, Germany.  All rights reserved.
#
# Author: Hubert Mantel <mantel@suse.de>, 1996
#         Christian Zoz <zoz@suse.de>, 2000
#         Joachim Gleissner <jg@suse.de>, 2004
#
# /etc/init.d/pcmcia
#
### BEGIN INIT INFO
# Provides:       pcmcia
# Required-Start: $local_fs
# X-UnitedLinux-Should-Start: coldplug
# Required-Stop:
# X-UnitedLinux-Should-Stop:
# Default-Start:  2 3 5
# Default-Stop:
# Description:    Loads pcmcia base modules and starts cardmgr
### END INIT INFO

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

. /etc/rc.status
rc_reset

# If booting with pcmcia locks the system, 
# set NOPCMCIA at the Boot-Prompt
if [ -n "$NOPCMCIA" ] ; then
  echo -n "PCMCIA: Will not be loaded due to NOPCMCIA=$NOPCMCIA" 
  rc_failed 5
  rc_status -v
  rc_exit
fi

. /etc/sysconfig/pcmcia

PIDFILE=/var/run/cardmgr.pid
STAB=/var/lib/pcmcia/stab
SC=/var/lib/pcmcia/scheme

# external PCMCIA is a relict from kernel 2.4
if [ "$2" = "external" ]; then
	echo -n "ERROR: external PCMCIA not supported anymore"
	rc_failed
	rc_status -v
	rc_exit
fi

# using yenta_socket in case no driver is given, will work in most cases
test -z "$PCMCIA_PCIC" && PCMCIA_PCIC=yenta_socket


case "$1" in
    start)
        echo -n "Starting PCMCIA "
        if checkproc /sbin/cardmgr; then
                echo -n "(cardmgr was already running)"
                rc_status -v
                exit
        fi
        if [ -f "$STAB" ] ; then
            echo "(... cleaning up left-over devices ...)"
	    echo -n "                       "
            cat $STAB | cleanup
        fi
        # Setting up schemes
        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 [ -s "$SC" ] ; then 
            echo -n "(using scheme: "
            echo -n "`cat $SC`)" 
        fi
	# Loading kernel modules (may be loaded already by hotplug, so we do
	# not moan if modprobe fails
	/sbin/modprobe pcmcia_core >/dev/null 2>&1 
	/sbin/modprobe $PCMCIA_PCIC >/dev/null 2>&1
	/sbin/modprobe ds >/dev/null 2>&1
	test "$PCMCIA_BEEP" = "no" && CARGS="$CARGS -q"
	# I don't like that usleep here, but due to runtime-problems it is 
	# mandatory to avoid crashing pcmcia -sb
	usleep 300000
	startproc -q /sbin/cardmgr $CARGS
	rc_check
#  removed, because we had lock-ups in this lines :(  -sb
#	if ! rc_status; then
#		/sbin/modprobe -r ds
#		/sbin/modprobe -r $PCMCIA_PCIC
#		/sbin/modprobe -r pcmcia_core
#	fi
        rc_status -v
        ;;
    stop)
        echo -n "Ejecting PCMCIA cards "
        if ! /sbin/cardctl eject 2>/dev/null; then
          echo -n "... card(s) still in use "
          rc_failed
        else
          rc_reset
          rc_status -v
          echo -n "Shutting down PCMCIA "
          test -x /usr/X11R6/bin/cardinfo \
            && killproc /usr/X11R6/bin/cardinfo 2>/dev/null
          CD=`lsmod | (while read a b c d; do
                         test "$a" = pcmcia || continue
                         echo ${d/,/ }
                       done)`
          sleep 1
          killproc /sbin/cardmgr
          if rc_check || test $? -eq 7; then 
            rc_reset
            for a in $CD pcmcia i82092 i82365 pd6729 tcic \
                     yenta_socket rsrc_nonstatic pcmcia_core; do
              if lsmod | grep -q -s "^$a"; then
                /sbin/modprobe -r $a
                rc_check
              fi
            done
          fi
        fi
        rc_status -v
        ;;
    try-restart)
        $0 status && $0 restart
        rc_check
        ;;
    restart)
        $0 stop 
        $0 start $2
        rc_check
        ;;
    reload|force-reload)
        echo -n "Reload PCMCIA "
        killproc -SIGHUP /sbin/cardmgr
        if rc_check; then
          sleep 1
          grep -s cardmgr /var/log/messages | \
              tail -n 1 | \
              grep -s -q "config error"
          if [ "$?" = "0" ] ; then
              echo -n "(config error: see /var/log/messages)"
              rc_failed
          fi
        fi
        rc_status -v
        ;;
    status)
        echo -n "Checking for PCMCIA: "
        for a in i82092 i82365 pd6729 tcic yenta_socket; do
          b=`lsmod | grep "^$a" | cut -d" " -f1`
	  test -n "$b" && echo -n "used driver: $b "
        done
        checkproc /sbin/cardmgr
        rc_status -v
        ;;
    probe)
        ## Optional: Probe for the necessity of a reload,
        ## give out the argument which is required for a reload.

        test /etc/pcmcia/config -nt $PIDFILE && echo reload
        ;;
    *)
        echo "Usage: $0 {start|stop|status|[try-]restart|[force-]reload|probe}"
        exit 1
esac

rc_exit
