#!/bin/sh
#
# fixed 1.13 1996/11/27 03:44:44 (David Hinds)
#
# Initialize or shutdown a PCMCIA ATA/IDE adapter
#
# The first argument should be either 'start' or 'stop'.  The second
# argument is the base name for the device.
#
# The script passes an extended device address to 'fixed.opts' in the
# ADDRESS variable, to retrieve device-specific configuration options.
# The address format is "scheme,socket,serial_no,[,part]" where
# "scheme" is the PCMCIA configuration scheme, "socket" is the socket
# number, "serial_no" is the card's serial number if available, and
# "part" is, optionally, the partition number.
#
# The script first calls fixed.opts for the entire device.  If
# fixed.opts returns with the PARTS variable set, then this variable
# contains a list of partitions for which options should be read.
#

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

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

INFO=`fgrep "	$DEVICE	" /var/run/stab` || usage
set -- $INFO ; SOCKET=$1
eval `/sbin/ide_info /dev/$DEVICE` || usage
if [ -s /var/run/pcmcia-scheme ] ; then
    SCHEME=`cat /var/run/pcmcia-scheme`
else
    SCHEME="default"
fi

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

case "$ACTION" in

'start')
    FAIL=0
    for PART in ${PARTS:-"."} ; do
	# Get mount options for particular partitions
	if [ $PART != "." ] ; then
	    ADDRESS="$SCHEME,$SOCKET,$PART"
	    . /etc/pcmcia/fixed.opts
	fi
	DEV=/dev/$DEVICE${PARTS:+$PART}
	if [ "$DO_FSTAB" = "y" ] ; then
	    echo "$DEV $MOUNTPT $FSTYPE ${OPTS:-defaults} 0 0" \
		>> /etc/fstab
	fi
	if [ "$DO_FSCK" = "y" ] ; then
	    /sbin/fsck -Ta $DEV
	    if [ $? -ne 0 ] ; then FAIL=1 ; continue ; fi
	fi
	if [ "$DO_MOUNT" = "y" ] ; then
	    mount -v ${OPTS:+-o $OPTS} ${FSTYPE:+-t $FSTYPE} $DEV \
		$MOUNTPT || FAIL=1
	fi
    done
    exit $FAIL
    ;;

'check')
    if [ -b /dev/$DEVICE ] ; then
	fuser -s -m /dev/${DEVICE}* && exit 1
    else
	fuser -s /dev/${DEVICE}* && exit 1
    fi
    ;;

'stop')
    for PART in ${PARTS:-"."} ; do
	if [ $PART != "." ] ; then
	    ADDRESS="$SCHEME,$SOCKET,$PART"
	    . /etc/pcmcia/fixed.opts
	fi
	if [ "$DO_FSTAB" = "y" ] ; then
	    fgrep -v "/dev/$DEVICE${PARTS:+$PART} " /etc/fstab > /etc/fstab.N
	    mv /etc/fstab.N /etc/fstab
	fi
    done
    if [ -b /dev/$DEVICE ] ; then
	fuser -s -k -m /dev/${DEVICE}*
	list=`mount | grep "/dev/${DEVICE}[0-9]* on" | cut -d" " -f3`
	if [ "$list" != "" ] ; then
	    for fs in $list ; do
		umount -v $fs || exit 1
	    done
	fi
    else
	fuser -s -k /dev/${DEVICE}*
    fi	
    ;;

'suspend'|'resume')
    ;;

*)
    usage
    ;;

esac

exit 0
