#!/bin/sh
#
# serial 1.14 1996/06/08 14:22:29 (David Hinds)
#
# Initialize or shutdown a PCMCIA serial device
#
# 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 'serial.opts' in the 
# ADDRESS variable, to retrieve device-specific configuration options.
# The address format is "scheme,socket,instance" where "scheme" is the
# PCMCIA configuration scheme, "socket" is the socket number, and
# "instance" is used to number multiple ports on a single card.  
#

usage()
{
    echo "usage: serial [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=`grep "	$DEVICE	" /var/run/stab` || usage
set -- $INFO
SOCKET=$1 ; INSTANCE=$4
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/serial.opts

DIALOUT=cua${DEVICE#ttyS}

case "$ACTION" in

'start')
    if [ ! -c /dev/${DEVICE} ] ; then
	cd /dev
	./MAKEDEV ${DEVICE}
    fi
    if [ "$LINK" != "" ] ; then
	rm -f $LINK
	ln -s /dev/$DIALOUT $LINK
	if [ "$SERIAL_OPTS" != "" ] ; then
	    setserial /dev/$DEVICE $SERIAL_OPTS
	fi
    fi
    ;;

'check')
    fuser -s /dev/$DEVICE && exit 1
    fuser -s /dev/$DIALOUT && exit 1
    if [ "$LINK" != "" ] ; then
	fuser -s $LINK && exit 1
    fi
    ;;

'stop')
    if [ "$LINK" != "" ] ; then
	fuser -s -k $LINK
	rm -f $LINK
    fi
    ;;

'suspend')
    ;;

'resume')
    if [ "$SERIAL_OPTS" != "" ] ; then
	setserial /dev/$DEVICE $SERIAL_OPTS
    fi
    ;;

*)
    usage
    ;;

esac

exit 0
