:
#
#	Look up the "key" passed as command line argument $1
#	in the faxphone database.  Then generate the following
#	troff business letter macro call sequence:
#
#		.LO SA "Dear <firstname>"
#		.IA "<fullname>"
#		<address lines>
#		.IE
#
#	The only sticky part is coming up with the persons first name,
#	avoiding traps like R. Yu Fong, Mr. Zappa.
#
awk -Ft '
$1==tokey {
	split($2, names, " ")
	lastch1 = substr(names[1], length(names[1]))
	lastch2 = substr(names[2], length(names[2]))
	if (lastch1 != ".") first = names[1]
	else if (lastch2 != "." && names[3] != "") first = names[2]
	else first=$2
	if ( first != "" ) print ".LO SA \"Dear " first ":\""
	else print ".LO SA"
	if ( $2 != "" ) print ".IA \"" $2 "\""
	if ( $3 != "" ) print $3
	if ( $4 != "" ) print $4
	if ( $5 != "" ) print $5
	if ( $6 != "" ) print $6
	if ( $7 != "" ) print $7
	print ".IE"
}
' "tokey=$1" $HOME/.faxphone
