:
#	@(#) ttys_update 1.1 89/03/20 
#
#	Copyright (C) The Santa Cruz Operation, 1989.
#	This Module contains Proprietary Information of
#	The Santa Cruz Operation and is Confidential.
#
# ttys_update - Update the terminal database /etc/auth/system/ttys.
#
PATH=/etc:/bin:/usr/bin:
LANG=english_us.ascii
export PATH LANG

tmp="/tmp/tty$$"
c2_ttys="/etc/auth/system/ttys"

# Define return values
: ${OK=0} ${FAIL=1} ${STOP=10} ${HALT=11}

# Function definitions
########################

# Define traps for critical and non critical code.
set_trap()  {	
	trap 'echo "Interrupted! Exiting ..."; cleanup 1' 1 2 3 15
}

unset_trap()  {
	trap '' 1 2 3 15
}

# Remove temp files and exit with the status passed as argument
#
# Usage: cleanup status
# References: $tmp indicates prefix of files to remove
# Notes: cleanup exits with whatever value it is passed.
#
cleanup() {
	trap '' 1 2 3 15
	[ "$tmp" ] && rm -f $tmp*
	exit $1
}

# Print an error message
error() {
	echo "\nError ttys_update: $*" >&2
	return $FAIL
}

# main() 
#########################

set_trap
cd /

[ -f /etc/conf/cf.d/init.base ] || {
	error "No init.base file."
	cleanup $FAIL
}

# Grab all lines that contain getty or uugetty.
if [ -f /etc/conf/init.d/* ]
then
	cat /etc/conf/cf.d/init.base /etc/conf/init.d/* | grep "getty" > $tmp
else
	grep "getty"  /etc/conf/cf.d/init.base > $tmp
fi

# Extract device name.
# This is done by looping through fields in tmp, skipping any that contain 
# the pattern "getty" or fields that are options passed to getty or uugetty. 
awk '
{
	 for (i=1; i<=NF; i++)  {
		if ( index($i,"getty") > 0 ) break
	}
	for (i++; i<=NF; i++)  {
		if ( index($i, "-") > 0 ) continue
		break
	}
	print $i
}
' $tmp >  $tmp.awk  || { 
	error " Awk unable to edit $tmp" 
	cleanup $FAIL
}

# Delete any blank lines, sort and remove duplicates.
sed '/^$/d' $tmp.awk | sort | uniq > $tmp.unq

cp $c2_ttys $tmp.c2
for a_tty in `cat $tmp.unq`
	do grep "${a_tty}:" $c2_ttys > /dev/null || {
		echo "$a_tty:t_devname=$a_tty:chkent:" >> $tmp.c2
	}
done
unset_trap
mv $tmp.c2 $c2_ttys
cleanup $OK	
