#!/bin/sh
#
# cdrom 1.6 1996/11/27 03:44:44 (David Hinds)
#
# Initialize or shutdown a PCMCIA CD-ROM device
#
# The first argument should be either 'start' or 'stop'.  The second
# argument is the name for the device.
#
# The script passes an extended device address to 'cdrom.opts' in the 
# ADDRESS variable, to retrieve device-specific configuration options.
# The address format is "scheme,socket" where "scheme" is the current
# PCMCIA device configuration scheme, and "socket" is the socket
# number.
#

usage()
{
    echo "usage: cdrom [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 ; 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"
. /etc/pcmcia/cdrom.opts

case "$ACTION" in

'start')
    DEV=/dev/$DEVICE
    if [ "$DO_FSTAB" = "y" ] ; then
	echo "$DEV $MOUNTPT $FSTYPE ${OPTS:-defaults} 0 0" >> /etc/fstab
    fi
    if [ "$DO_MOUNT" = "y" ] ; then
	mount -v ${OPTS:+-o $OPTS} ${FSTYPE:+-t $FSTYPE} $DEV $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
    ;;

'suspend'|'resume')
    ;;

*)
    usage
    ;;

esac

exit 0
