#/bin/sh
#
# Create a database of possible controlling terminals
#
# Outputs a structure definition holding the three fields
#
#	Major Number
#	Minor Number
#	TTY name
#
# This is used to search for tty names, given major and minor numbers
#
# The structure is populated with data as follows
#
#  Output the structure definition
#  list /dev
#  Filter out just character special files
#  filter out files with tty or pts in them (This could be more robust)
#  Extract just the major, minor numbers and the name 
#  Sort them in numeric order Major # - primary key, Minor # - secondary key 
#  Re-order the fields so we can extract duplicate entries(keep first)
#  finally formatting as a table entry
#  Lastly finish up structure declaration.

FILE="ttydb.h"

echo "struct _ttydb {" >$FILE
echo "   int  major_no;"  >>$FILE
echo "   int  minor_no;"  >>$FILE
echo "   char *name;"  >>$FILE
echo "};"  >>$FILE
echo  >>$FILE

echo "struct _ttydb Ttydb[] = {"  >>$FILE

/bin/ls -l /dev | sed -n '/^c/p' | fgrep -e tty -e pty -e pts | awk -F "[ 	,]*" '{print $5,$6,$10 }' | sort  -n -k 1,1 -k 2,2 | awk ' { print $3, $1, $2 }' | uniq -f 1 | awk '{print "{", $2, ",", $3, ",", "\""$1"\"", "}"","}'   >>$FILE

echo "{ -1, -1, \"\" },"  >>$FILE

echo "};"  >>$FILE

