#	faxform: Sample script to show how to query a user supplied
#	database for a recipients name and FAX number, and then
#	substitute that into an ASCII text document.  Finally,
#	the document is sent using the 'faxsend' command.
#	The usage of this command is:
#		faxform database_key document_name
#	e.g.	faxform rick letter1
#
key=$1; doc=$2
if [ $# != 2 ]
then
	echo "usage:	faxform database_key document_name"
	exit 1
fi
if [ ! -f $doc ]
then
	echo "Can't find document $doc to send"
	exit 2
fi
#
#	We don't have a DBMS system handy, and they are all different
#	anyhow, so we'll just query our own 'faxphone' database.
#	Obviously, this section needs to be changed for whatever
#	database and DBMS is to be used.
TAB="	"	# a TAB character
#	Get the fields out of the .faxphone database
ofs="$IFS"
IFS="$TAB"
record=`grep "^$key$TAB" $HOME/.faxphone |
	sed -e "s/$TAB$TAB/$TAB $TAB/g" -e "s/$TAB$TAB/$TAB $TAB/g"`
if [ "$record" = "" ]
then
	echo "Couldn't find key $key in database"
	exit 3
fi
set $record
IFS="$ofs"
#	Rename the positional parms with symbolic names for readability
KEY="$1"; NAME="$2"; COMPANY="$3"
ADDR1="$4" ADDR2="$5" ADDR3="$6" ADDR4="$7" ADDR5="$8"
VOICE="$9"; shift; FAX="$9"; shift; EMAIL="$9"
DATE=`date +%x`
#
#	OK, now make a copy of the document and substitute
#	a few things into the document to show how its done
TMP=/tmp/$$
cp $doc $TMP
ed - <<-EOF $TMP
g@<DATE>@s@@$DATE@g
g@<NAME>@s@@$NAME@g
g@<COMPANY>@s@@$COMPANY@g
g@<ADDR1>@s@@$ADDR1@g
g@<ADDR2>@s@@$ADDR2@g
g@<ADDR3>@s@@$ADDR3@g
g@<ADDR4>@s@@$ADDR4@g
g@<ADDR5>@s@@$ADDR5@g
g@<FAX>@s@@$FAX@g
g@<VOICE>@s@@$VOICE@g
w
q
EOF
#
#	OK, now send the document, without a cover sheet in this example
faxsend -q -c $FAX $TMP
rm $TMP
