:
#	@(#) mkpttycmds 25.1 92/09/29 
#
#	Copyright (C) 1991-1992 The Santa Cruz Operation, Inc.
#	All Rights Reserved.
#	The information in this file is provided for the exclusive use of
#	the licensees of The Santa Cruz Operation, Inc.  Such users have the
#	right to use, modify, and incorporate this code into other products
#	for purposes authorized by the license agreement provided they include
#	this notice and the associated copyright notice with any such product.
#	The information in this file is provided "AS IS" without warranty.
#
# Script to add entries to the temporary terminal database
# (/etc/auth/system/ttys-t) for newly created pseudo-tty devices.
# This shell script is called by /tcb/bin/ale which handles the
# updating of the actual terminal database. The script returns zero
# if entries are added, 1 if there is an error and 2 if there are no
# entries to add.
# This script should be installed in the /tcb/lib/auth_scripts
# directory with the owner, group and permissions specified in
# the File Control database.

IFS=" 	
"
PATH=/bin:/usr/bin:/etc
LC_COLLATE=english_us.ascii
export PATH IFS LC_COLLATE

CMDNAME=`basename $0`
TTYS=/etc/auth/system/ttys

TMP1=/tmp/pttyupd$$a
TMP2=/tmp/pttyupd$$b

umask 077

# Make sure the temporary terminal database exists. If it doesn't,
# this script was not run by the correct program.
if [ ! -w $TTYS-t ]
then
	echo "$CMDNAME: temporary terminal database file is missing or not writable"
	exit 1
fi

# Make sure the terminal database file exists and is readable.
if [ ! -r $TTYS ]
then
	echo "$CMDNAME: file $TTYS is missing or not readable"
	exit 1
fi

# If /etc/conf/node.d/spt does not exist, we assume that there will be no
# pseudo-ttys in /dev, so there's nothing to do.
test -f /etc/conf/node.d/spt || exit 2

# Extract the terminal names by looking in /etc/conf/node.d/spt
/usr/bin/awk '$1 == "spt" { print $2 }' /etc/conf/node.d/spt > $TMP1

# Extract the terminal names from the terminal database, ensuring 
# duplicate lines occur only once.
/bin/sed -n 's/^\([^	][^:]*\):.*/\1/p' $TTYS | /bin/sort -u >$TMP2

# Copy the existing terminal database file to the temporary database file.
/bin/cp $TTYS $TTYS-t

# Any lines in $TMP1 but not in $TMP2 are terminals to be added to the
# terminal control database.
/usr/bin/comm -23 $TMP1 $TMP2 |

# Add each terminal to the temporary terminal database and output a
# message for each terminal added.
/usr/bin/awk '
BEGIN {
	changed = 0
}
{
	changed = 1
	printf "%s:t_devname=%s:chkent:\n", $0, $0 >> "'$TTYS'-t"
	printf "Adding terminal %s to the Terminal Control database\n", $0
}
END {
	if (changed == 1)
		exit 0
	exit 2
}'
status=$?

# Tidy up and exit. 
/bin/rm -f $TMP1 $TMP2
exit $status
