#
# ttyoff:	Turn inittab entry for tty?? from "respawn" to "off"
#		and kill getty or login.
#
# Modification History:
# 1.0  - xx-xx-86 by DF&DK - Original creation.
# 1.1  - 07-06-87 by ACW - Added checks to ensure that inittab is not completely
#		lost in the event of some unforseen circumstance.
# 1.2  - 12-03-87 by ACW - Added test to verify that tty device exists

if [ "$1" = "-n" ]
then
	kern=$2
	shift 2
else
	kern=/unix
fi
if [ ! -r $kern ]
then 
	echo namelist not readable
	exit 1
fi
if [ $# -ne 1 ]
then
	echo 'usage: ttyoff [ -n kernel ] tty??'
	exit 1
fi

if [ ! -c /dev/$1 ]
then
  echo "$0: device /dev/$1 does not exist. Exiting..."
  exit 1
fi

# change inittab entry to off

if cp /etc/inittab /etc/oinittab
then
  sed -e "/$1/s/respawn/off/" /etc/inittab > /tmp/off$$
  if mv /tmp/off$$ /etc/inittab
  then
    :
  else
    if cp /etc/oinittab /etc/inittab
    then
      echo "$0: old inittab restored. Exiting..."
      exit 1
    else
      echo "$0: unable to restore inittab. Exiting..."
      exit 1
    fi
  fi
else
  echo "$0: unable to make copy of inittab. Exiting..."
  exit 1
fi

# kill getty on tty

a=`ps -e -n $kern|egrep "$1.*(getty|login)" |\
    sed -e "s/\(......\).*/kill -15 \1 > \/dev\/null /"`

if [ "$a" ]
then
	eval $a 2> /dev/null
else
	echo "$0: no getty or login on $1. Exiting..."
	exit 1
fi
exit 0
