#
# mailertable: generate rules to use mailertable facility
#

REVISION_ID='@(#)mailertable	3.14 (motonori/WIDE) 27 Apr 1997'

#|# use mailertable feature (yes/no/"db_type:db_path"/"db_type:-o db_path")
#|#  default is hash:/etc/mailertable)
#|# how to make DB: makemap hash /etc/mailertable.db < /etc/mailertable
#|#              or makedbm /etc/mailertable /etc/mailertable (ndbm on SunOS4)
#|#MAILERTABLE=no

: ${MAILERTABLE=no}
if [ "$MAILERTABLE" != no ]
then

	if [ "$REVISION_ID_LIST" ]
	then
		REVISION_ID_LIST="$REVISION_ID_LIST\\
# $REVISION_ID"
	else
		REVISION_ID_LIST="# $REVISION_ID"
	fi

	case "$MAILERTABLE" in
	hash:*|btree:*|dbm:*|nis:*)
		MAILERTABLEDB="$MAILERTABLE";;
	[Yy][Ee][Ss])
		MAILERTABLEDB="hash:/etc/mailertable";;
	/*)
		MAILERTABLEDB="hash:$MAILERTABLE";;
	*)
		echo 'mailertable: bad MAILERTABLE database spec.' 1>&2;;
	esac

	MAILERTABLEDB=`echo $MAILERTABLEDB | sed 's/:/ /'`

HDRMAILERTABLE="\\
# mailer table (overriding domains)\\
Kmailertable $MAILERTABLEDB"

S0MAILERTABLE='\
# try mailer table lookup\
R$*<@$+>$*		$:<%$2>:$1<@$2>$3		extract host name\
R<%$+.>:$*		$:<%$1>:$2			strip trailing dot\
R<%$+>:$*		$:<$(mailertable $1$:%$1$)>:$2	lookup\
R<$-:$+>:$*		$@$>88 <$1:$2>.$3		check -- resolved?\
R<$->:$*		$@$>88 <$1>.$2			check -- resolved?\
R<%$+>:$*		$:<$(mailertable +$1$:%$1$)>:$2	lookup\
R<$-:$+>:$*		$@$>88 <$1:$2>.$3		check -- resolved?\
R<$->:$*		$@$>88 <$1>.$2			check -- resolved?\
R<%$+>:$*		$:$>90 <%$1>:$2			try domain'


MAILERTABLEBODY='\
###################################################################\
###  Ruleset 90 -- try domain part of mailertable entry		###\
###################################################################\
\
S90\
R$*<%$-.$+>:$*		$:$1$2<$(mailertable .$3$@$1$2$@$2$:%.$3$)>:$4	lookup\
R$*<$-:$+>:$*		$@$>88 <$2:$3>.$4		check -- resolved?\
R$*<$->:$*		$@$>88 <$2>.$3			check -- resolved?\
R$*$-<%.$+>:$*		$:$1$2<$(mailertable +$3$@$1$2$@$2$:%.$3$)>:$4	lookup\
R$*<$-:$+>:$*		$@$>88 <$2:$3>.$4		check -- resolved?\
R$*<$->:$*		$@$>88 <$2>.$3			check -- resolved?\
R$*<%.$+>:$*		$@$>90 $1.<%$2>:$3		no -- strip/try again\
R$*<%$->:$*		$:<$(mailertable .$@$1$2$@$2$:%.$)>:$3	try "."\
R<$-:$+>:$*		$@$>88 <$1:$2>.$3		"." found?\
R<$->:$*		$@$>88 <$1>.$2			"." found?\
R<$*>:$*		$@$2				no mailertable match'

fi
