#!/bin/sh
#
# ftl 1.15 1996/11/27 03:44:44 (David Hinds)
#
# Initialize or shutdown an FTL memory device
#
# The first argument should be the action to be performed, and the
# second, the base name for the device.
#
# The script passes an extended device address to 'ftl.opts' in the 
# ADDRESS variable, to retrieve device-specific configuration options.
# The address format is "scheme,socket,instance" where "scheme" is the
# current PCMCIA configuration scheme, "socket" is the socket number,
# and "instance" is used to number multiple partitions on a single
# card.
#

usage()
{
    echo "usage: ftl [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/ftl.opts

case "$ACTION" in

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

'check')
    fuser -s -m /dev/$DEVICE && exit 1
    ;;

'stop')
    test -b /dev/$DEVICE || exit 1
    fuser -s -k -m /dev/$DEVICE
    if mount | fgrep "/dev/${DEVICE} on" ; then
	umount -v /dev/$DEVICE || 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
    ;;

'suspend'|'resume')
    ;;

*)
    usage
    ;;

esac

exit 0
