#!/bin/sh
#
# memory 1.17 1996/11/27 03:44:44 (David Hinds)
#
# Initialize or shutdown a PCMCIA memory device using the "new" driver
#
# The first argument should be either 'start' or 'stop'.  The second
# argument is the base name for the device.
#
# This script creates a character device and a block device for
# accessing a single memory region:
#
#	/dev/{name}c	- character device
#	/dev/{name}b	- block device
#
# The script passes an extended device address to 'memory.opts' in the 
# ADDRESS variable, to retrieve device-specific configuration options.
# The address format is "scheme,socket,instance" where "scheme" is the
# current PCMCIA device configuration scheme, "socket" is the socket
# number, and "instance" is used to number multiple partitions on a
# single card.
#

usage()
{
    echo "usage: memory [action] [device name]"
    echo "  actions: start check stop suspend resume"
    exit 1
}

if [ $# -lt 2 ] ; then usage ; fi
ACTION=$1 ; DEVICE=$2

# Get device attributes
INFO=`fgrep "	$DEVICE	" /var/run/stab` || usage
set -- $INFO
SOCKET=$1 ; INSTANCE=$4 ; MAJOR=$6 ; MINOR=$7
if [ -s /var/run/pcmcia-scheme ] ; then
    SCHEME=`cat /var/run/pcmcia-scheme`
else
    SCHEME="default"
fi

# Load site-specific settings
ADDRESS="$SCHEME,$SOCKET,$INSTANCE"
. /etc/pcmcia/memory.opts

case "$ACTION" in

'start')
    mknod /dev/${DEVICE}c c $MAJOR $MINOR
    BLK=/dev/${DEVICE}b
    mknod $BLK b $MAJOR $MINOR
    if [ "$DO_FSTAB" = "y" ] ; then
	echo "$BLK $MOUNTPT $FSTYPE ${OPTS:-defaults} 0 0" \
	    >> /etc/fstab
    fi
    if [ "$DO_FSCK" = "y" ] ; then
	/sbin/fsck -Ta $BLK || exit 1
    fi
    if [ "$DO_MOUNT" = "y" ] ; then
	mount -v ${OPTS:+-o $OPTS} ${FSTYPE:+-t $FSTYPE} $BLK $MOUNTPT
    fi
    ;;

'check')
    fuser -s /dev/${DEVICE}c && exit 1
    fuser -s -m /dev/${DEVICE}b && exit 1
    ;;

'stop')
    fuser -s -k /dev/${DEVICE}c
    test -b /dev/${DEVICE}b || exit 1
    fuser -s -k -m /dev/${DEVICE}b
    if mount | fgrep "/dev/${DEVICE}b on" ; then
	umount -v /dev/${DEVICE}b || exit 1
    fi
    if [ "$DO_FSTAB" = "y" ] ; then
	fgrep -v /dev/${DEVICE} /etc/fstab > /etc/fstab.N
	mv /etc/fstab.N /etc/fstab
    fi
    rm -f /dev/${DEVICE}[bc]
    ;;

'suspend'|'resume')
    ;;

*)
    usage
    ;;

esac

exit 0
