#!/bin/sh
#
# $Id: ttyswitch,v 1.1 1992/03/28 17:40:49 ejb Exp $
# $Source: /home/ejb/scripts/RCS/ttyswitch,v $
# $Author: ejb $
#
#
# This script is used to turn on or off the ttytab entry for a
# given port.
#
# Usage: ttyswitch port on|off
#

PATH=/bin:/usr/bin:/usr/etc; export PATH

ttytab=/etc/ttytab

usage() {
   echo "Usage: $1 port on | off" 1>&2
   exit 1
}

whoami=`basename $0`

if [ $# -ne 2 ]; then
   usage $whoami
fi

if [ "$2" = "on" ]; then
   old="off"
elif [ "$2" = "off" ]; then
   old="on"
else
   usage $whoami
fi

status=`eval egrep -c "'^$1[ 	]'" $ttytab`
if [ $status -ne 1 ]; then
   echo "There are $status (instead of 1) occurences of $1 in $ttytab." 1>&2;
   exit 1
fi;   

status=`eval egrep -c "'^$1[ 	].*[ 	]$old'" $ttytab`
if [ $status = 0 ]; then
   echo "$1 is already $2; not editing $ttytab or sending -HUP to init" 1>&2;
   exit 1
fi

ed - $ttytab << EOF
/^$1/
s/\([ 	]\)$old/\1$2/p
w
q
EOF

if [ $? -ne 0 ]; then
   echo "Error editing $ttytab; not sending -HUP to init" 1>&2;
   exit 1
fi

echo "Sending -HUP to init"
kill -HUP 1
exit 0
