:
#	@(#) termupd 23.3 91/11/10 
#
#	Copyright (C) 1990-1991 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 an entry to the temporary terminal database
# (/etc/auth/system/ttys-t) for each terminal configured with
# a getty or uugetty in /etc/inittab which is not already
# entered in the Terminal Control database. 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

COMMANDNAME=`basename $0`
INITTAB=/etc/inittab
TTYS=/etc/auth/system/ttys
DEVASSIGN=/etc/auth/system/devassign

TMP1=/tmp/termupd1.$$
TMP2=/tmp/termupd2.$$
TMP3=/tmp/termupd3.$$

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 "$COMMANDNAME: 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 "$COMMANDNAME: file $TTYS is missing or not readable"
	exit 1
fi

# Make sure the inittab file exists and is not empty.
if [ ! -s $INITTAB ]
then
	echo "$COMMANDNAME: file $INITTAB is missing or is empty"
	exit 1
fi

# Make sure the inittab file is readable.
if [ ! -r $INITTAB ]
then
	echo "$COMMANDNAME: file $INITTAB is not readable"
	exit 1
fi

# Extract the terminal names from /etc/inittab. This is done by looping
# through /etc/inittab, extracting the terminal name from the lines
# containing the string "getty" in the process field. Care is taken to
# ensure any options passed to getty (or uugetty) are skipped before
# finding the terminal name.
/usr/bin/awk '
/^#/ { next }
/^.*:.*:.*:[^ 	]*getty/ {
	for (i=2; i<=NF; i++)  {
		if (index($i, "-") == 1) {
			if ($i == "-t")
				i++
		}
		else {
			print $i
			next
		}
	}
}' $INITTAB >$TMP1

# If a terminal name is an alias (in the device assignment database),
# substitute the real terminal name in place of the alias. Then sort
# the terminal names, removing any duplicates.
if [ -s $DEVASSIGN -a -r $DEVASSIGN ]
then
	/usr/bin/awk '
	FS=":"
	/v_type=terminal/ {
		num = split (substr($2, 8, length($2) - 7), list, ",")
		for (i = 1; i <= num; i++) {
			members = split (list[i], element, "/")
			if (element[members] != $1 && list[i] == "/dev/" element[members])
				printf "s/%s/%s/\n", element[members], $1
		}
	}' $DEVASSIGN > $TMP3
	if [ -s $TMP3 ]
	then
		/bin/sed -f $TMP3 $TMP1 | /bin/sort -u > $TMP2
	else
		/bin/sort -u $TMP1 > $TMP2
	fi
	/bin/rm -f $TMP1 $TMP3
else
	/bin/sort -u $TMP1 > $TMP2
	/bin/rm -f $TMP1
fi

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

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

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

# 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
