#!/bin/sh
#
# serial 1.20 1997/07/03 06:06:00 (David Hinds)
#
# modified 1997/11/10 03:37 (G D Rijhwani)
#
# 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.  
#

. ./shared

test "$display" && \
    echo "=====================================================================" > $display

# Get device attributes
get_info $DEVICE

# Load site-specific settings
ADDRESS="$SCHEME,$SOCKET,$INSTANCE"
. $0.opts

DIALOUT=cua${DEVICE#ttyS}

case "$ACTION" in

'start')
    test "$display" && \
       echo "Start serial card" > $display

    if [ ! -c /dev/${DEVICE} ] ; then
	cd /dev
	./MAKEDEV ${DEVICE}
    fi
    if [ "$LINK" != "" ] ; then
	rm -f $LINK
	ln -s /dev/$DIALOUT $LINK
        test "$display" && \
            echo "/dev/$DIALOUT linked to $LINK" > $display
    fi
    if [ "$SERIAL_OPTS" != "" ] ; then
	setserial /dev/$DEVICE $SERIAL_OPTS
    fi
    if [ "$INITTAB" != "" ] ; then
	echo "${DEVICE#tty}:12345:respawn:$INITTAB $DEVICE" >> /etc/inittab
	telinit q
    fi
    ;;

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

'cksum')
    chk_simple "$3,$SOCKET,$INSTANCE" || exit 1
    ;;
    
'stop')
    test "$display" && \
       echo "Stop serial card" > $display

    if [ "$INITTAB" != "" ] ; then
	fgrep -v $DEVICE /etc/inittab > /etc/inittab.new
	mv /etc/inittab.new /etc/inittab
	telinit q
    fi
#   fuser -s -k /dev/$DEVICE /dev/$DIALOUT $LINK
    if [ -L $LINK ] ; then
        rm -f $LINK
        if [ "$LINK" = "/dev/modem" ] ; then
            ln -s /dev/cua0 $LINK
            test "$display" && \
                echo "/dev/cua0 linked to $LINK" > $display
        else
            test "$display" && \
                echo "alias $LINK removed" > $display
        fi
    fi
    ;;

'suspend')
    fuser -s -k -STOP /dev/$DEVICE /dev/$DIALOUT
    ;;

'resume')
    if [ "$SERIAL_OPTS" != "" ] ; then
	setserial /dev/$DEVICE $SERIAL_OPTS
    fi
    fuser -s -k -CONT /dev/$DEVICE /dev/$DIALOUT $LINK
    ;;

*)
    usage
    ;;

esac

test "$display" && \
    echo "---------------------------------------------------------------------" > $display

exit 0
