:
#	@(#) 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`
DEVASG=/etc/auth/system/devassign

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

umask 077

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

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

# Find which linked devices are present (ttyp0-9 are not linked).
(cd /dev;/bin/ls tty[pqrstuvwxyz][0-9abcdef]) | /bin/sed '/ttyp[0-9]/d' > $TMP1

# Create a file mapping each present linked device to its standard device.
for i in p q r s t u v w x y z
do	for j in 0 1 2 3 4 5 6 7 8 9 a b c d e f
	do	echo tty$i$j
	done
done |
/usr/bin/awk 'BEGIN {
	getline nextdev < "'$TMP1'"
	node = 0
    }
    {	if ($0 == nextdev) {
	    print $0, "ttyp" node
	    getline nextdev < "'$TMP1'"
	}
	node = node + 1
    }' > $TMP2

# Extract the standard ptty terminal names (ttyp10 onwards) from the device
# assignment database, ensuring duplicate lines occur only once.
/bin/sed -n 's/^\(ttyp[1-9][0-9][0-9]*\):.*/\1/p' $DEVASG | /bin/sort -u >$TMP1

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

# For each tty in $TMP2 which is not in $TMP1, add to the -t file,
# and print a message
/usr/bin/awk 'BEGIN {
	changed = 0
	getline nextdev < "'$TMP1'"
    }
    {
	if ($2 == nextdev) {
	    getline nextdev < "'$TMP1'"
	} else {
	    changed = 1
	    printf "%s:v_devs=/dev/%s,/dev/%s:v_type=terminal:chkent:\n", $2, $2, $1 >> "'$DEVASG'-t"
	    printf "Adding device %s to the Device Assignment database\n", $2
	}
    }
    END {
	if (changed == 1)
		exit 0
	exit 2
    }' < $TMP2
status=$?

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