#
# shared 1.15 1999/11/23 03:47:47 (David Hinds)
#

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

is_true ()
{
    [ "$1" = "y" -o "$1" = "Y" -o "$1" = "yes" -o "$1" = "YES" ]
}

add_blkdev ()
{
    # usage: add_blkdev [device]
    if [ "$LINK" ] ; then ln -s $1 $LINK || return 1 ; fi
    if is_true $DO_FSTAB ; then
	echo "$1 $MOUNTPT ${FSTYPE:-auto} ${OPTS:-defaults} 0 0" >> /etc/fstab
    fi
    if is_true $DO_FSCK ; then
	/sbin/fsck -Ta $1 || return 1
    fi
    if is_true $DO_MOUNT ; then
	O=${OPTS:+-o $OPTS} ; FS=${FSTYPE:+-t $FSTYPE}
	mount -v $O $FS $1 $MOUNTPT || return 1
    fi
    return 0
}

add_parts ()
{
    # usage: add_parts [address] [parts]
    RET=0
    for PART in ${2:-""} ; do
	# Get mount options for particular partitions
	if [ "$PART" ] ; then
	    ADDRESS="$1,$PART"
	    unset DO_FSTAB DO_FSCK DO_MOUNT LINK
	    . $0.opts
	fi
	add_blkdev /dev/$DEVICE$PART || RET=1
    done
    return $RET
}

rm_blkdev ()
{
    # usage: rm_blkdev [device]
    test -b $1 || return 1
    fuser -k -m $1 > /dev/null
    if mount | fgrep "$1 on" ; then
	umount -v $1 || return 1
    fi
    if is_true $DO_FSTAB ; then
	fgrep -v $1 /etc/fstab > /etc/fstab.N
	mv /etc/fstab.N /etc/fstab
    fi
}

rm_parts ()
{
    # usage: rm_parts [address] [parts]
    BLK=/dev/$DEVICE
    test -b $BLK || return 1
    for PART in ${2:-""} ; do
	if [ "$PART" ] ; then
	    ADDRESS="$1,$PART"
	    unset DO_FSTAB DO_FSCK DO_MOUNT
	    . $0.opts
	fi
	if is_true $DO_FSTAB ; then
	    fgrep -v "$BLK$PART " /etc/fstab > /etc/fstab.N
	    mv /etc/fstab.N /etc/fstab
	fi
    done
    fuser -k -m /dev/${DEVICE}* > /dev/null
    LIST=`mount | sed -ne "s+^\($BLK[0-9]*\) .*+\1+p"`
    if [ "$LIST" ] ; then
	for MT in $LIST ; do
	    umount -v $MT || return 1
	done
    fi
}

chk_simple ()
{
    # usage: chk_simple [new-address]
    ADDRESS=""
    OLD=`set | grep -v '^OLD=' | md5sum`
    ADDRESS=$1
    . $0.opts
    ADDRESS=""
    NEW=`set | grep -v '^OLD=' | md5sum`
    [ "$OLD" = "$NEW" ]
}

chk_parts ()
{
    # usage: chk_parts [new-address]
    OLD_ADDR=$ADDRESS
    chk_simple $1 || return 1
    if [ ! "$PARTS" ] ; then return 0 ; fi
    for PART in $PARTS ; do
	ADDRESS="$OLD_ADDR,$PART"
	. $0.opts
	chk_simple "$1,$PART" || return 1
    done
}

grep_stab ()
{
    # this should be cheaper than invoking "grep"
    local CLASS DEV
    while read SOCKET CLASS DRIVER INSTANCE DEV MAJOR MINOR ; do
	if [ "$1" = "$DEV" ] ; then return 0 ; fi
    done
    return 1
}

get_info ()
{
    if [ -d /var/state/pcmcia ] ; then
	SCHEME=`cat /var/state/pcmcia/scheme`
	STAB=/var/state/pcmcia/stab
    elif [ -d /var/lib/pcmcia ] ; then
	SCHEME=`cat /var/lib/pcmcia/scheme`
	STAB=/var/lib/pcmcia/stab
    else
	SCHEME=`cat /var/run/pcmcia-scheme`
	STAB=/var/run/stab
    fi
    if [ ! "$SCHEME" ] ; then SCHEME="default" ; fi
    grep_stab $1 < $STAB || usage
}

# A bit of stuff to do right away...

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