#! /bin/sh

PATH=/bin:/usr/bin:/usr/ucb
export PATH

case "$CF_DEBUG" in
*config*|*all*)
	exec 2> DEBUG.Configure.$$
	set -vx
	;;
*)	;;
esac

: ${MASTERDIR=Master}
: ${TOOLDIR=Tools}
: ${MKINFO=Mkinfo}
: ${CFSELECT=cf_select}
: ${CLEANUP=Cleanup}

# preserve argument vector
ARGV="$@"

# evaluate definition files
while [ "$1" ]
do
	if [ -f "$1" ]
	then
		case "$1" in
		/*)	. $1 ;;
		*)	. ./$1 ;;
		esac
	else
		echo "$1: no such file." 1>&2
	fi
	shift
done

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

# recover argument vector
set "$ARGV"

# select type of sendmail.cf
. $MASTERDIR/$CFSELECT

# get information of config file creation
. $MASTERDIR/$MKINFO

if [ "$FEATURE" = "" ]
then
	echo "No feature found." 1>&2
	exit 1;
fi

# read list features
exec < $MASTERDIR/$FEATURE
while read LINE
do
	if [ "$LINE" ]
	then
		set $LINE
		case "$1" in
		\#*) ;;	# comment
		*=*) ;;	# assertion
		*)	# feature file
			if [ -f $MASTERDIR/$1 ]
			then
				# save valid filenames
				FEATURES="$FEATURES $MASTERDIR/$1"
			else
				echo "$1: no such feature file." 1>&2
			fi ;;
		esac
	fi
done

# evaluate feature description files
for FT in $FEATURES
do
	. $FT
done

export ADMINSTICKYRULE
export ALIASRULE
export BITNETRULE
export BROKENSMTPPEERS
export CANONACCEPTADDR
export CANONACCEPTLOWER
export CANONRULE1
export CANONRULE2
export CANONRULE3
export CANONRULE4
export CANONRULE5
export CHAR_ALLOCATION_LIST
export CLASS_w
export DECNETRULE
export DOMAINTABLERULE
export DSPTACCEPTLOWER
export DSPTACCEPTRULE
export DSPTERRORRULE
export DSPTLOCALHACK
export DSPTUUCPRULE
export ELIMADDR
export ESMTPMAILER
export ESMTPPEERS
export EXTENDEDUUCPCONV
export FAKEDOMAIN
export FAKEUUCP
export FILECLASS_w
export FMTRECEIVED
export GENERALFROM
export HDRACCEPTADDR
export HDRACCEPTLOWER
export HDRALIASED
export HDRBITNET
export HDRDECNET
export HDRDOMAINTABLE
export HDRFROMADDR
export HDRHUBHOST
export HDRINDIRECTRELAY
export HDRLOCALHACK
export HDRLOCALUSERS
export HDRMAILERTABLE
export HDRNEWS
export HDRNISLOOKUP
export HDRROUTE
export HDRSECONDARY
export HDRSPECIALFROM
export HDRSPOOLHOST
export HDRSTATIC
export HDRUSERTABLE
export HDRUUCP
export HDRUUNAME
export HUBHOSTRULE
export IDENTPEERS
export IGNOREMXNUMERIC
export JUNETRULE
export KNOWNDOMAINS
export LOCALFROM
export LOCALHACKMAILER
export LOCALHACK_LOCALIZE
export LOCALHUBRETRYRULE
export LOCALHUBRULE2
export LOCALMAILER
export LOCALRECEIPIENTRULE
export MACRO_e
export MACRO_j
export MACRO_l
export MACRO_m
export MACRO_n
export MACRO_o
export MACRO_q
export MACRO_w
export MAILERTABLEBODY
export MXDOTTERMINATE
export NEWSMAILER
export NISDELIVERRULE
export NISLOOKUPDELIVERRULE
export NUMERICRULE
export OPTION_7
export OPTION_A
export OPTION_B
export OPTION_C
export OPTION_D
export OPTION_E
export OPTION_F
export OPTION_G
export OPTION_H
export OPTION_I
export OPTION_J
export OPTION_K
export OPTION_L
export OPTION_N
export OPTION_O
export OPTION_P
export OPTION_Q
export OPTION_R
export OPTION_S
export OPTION_T
export OPTION_U
export OPTION_V
export OPTION_W
export OPTION_X
export OPTION_Y
export OPTION_Z
export OPTION_a
export OPTION_b
export OPTION_c
export OPTION_d
export OPTION_e
export OPTION_f
export OPTION_g
export OPTION_h
export OPTION_i
export OPTION_j
export OPTION_k
export OPTION_l
export OPTION_m
export OPTION_n
export OPTION_o
export OPTION_p
export OPTION_q
export OPTION_r
export OPTION_s
export OPTION_t
export OPTION_u
export OPTION_w
export OPTION_x
export OPTION_y
export OPTION_z
export PROGMAILER
export RECIPIENTLOCALHACKRULE
export RECIPIENTUUCPRULE
export REDIRECTRULE
export REJECTONERULE
export RELAYMAILER
export RESRULE
export REVISION_ID_LIST
export ROUTERULE
export S0ACCEPTADDR
export S0ACCEPTLOWER
export S0ALIASRULE
export S0CANONRULE
export S0LOCALHACK_ACCEPT
export S0LOCALHACK_ALIAS
export S0LOCALHACK_DELIVERY
export S0LOCALHACK_LOCAL
export S0LOCALHACK_NUMERIC
export S0LOCALHACK_RESOLV
export S0LOCALHUBRULE
export S0MAILERTABLE
export S0NEWSRULE
export S0STRIPTRAILINGDOT
export S0USERTABLEMAILDROP
export S0UUCPRULE
export S10LOCALRULE
export S11GENERICREWRITE
export S11SECONDARY
export S11SPECIALFROM
export S11SPECIALREWRITE
export S11SPECIALREWRITE2
export S11USERTABLEMAILNAME1
export S11USERTABLEMAILNAME2
export S11UUCPRULE
export S1LOCALHACKRULE
export S1STRIPTRAILINGDOT
export S20LOCALRULE
export S21GENERICREWRITE
export S21SECONDARY
export S21SPECIALFROM
export S21SPECIALREWRITE
export S21SPECIALREWRITE2
export S21USERTABLEMAILNAME
export S29RELAYRULE
export S2LOCALHACKRULE
export S2STRIPTRAILINGDOT
export S3LOCALHACK_FOCUS
export S3LOCALHACK_NO_AT
export S4ACCEPTADDR
export S4LOCALHACK
export S5LOCALHUBRULE
export S5USERTABLEMAILDROP
export SECONDARYRULE
export SENDERLOCALHACKRULE
export SENDERUUCPRULE
export SMTPDELIVERRULE
export SMTPMAILER
export SPECIALFROMRULE
export SPOOLHOSTRULE
export STATICRULE
export STICKYRULE
export STRIPTRAILINGDOT
export TERMINATORS
export TRUSTDEF
export UUCPMAILER
export UUCPRESRULE
export VERSION
export WHAT
export WHEN
export WHERE
export WHO

# keyword substitution with PERL
/usr/local/bin/perl -e '
while (<>) {
	# replace keywords with values defined in environment variables
	if (/^#%(.*)%/) {
		$val = $ENV{$1};
		# for compatibility with SED version
		$val =~ s/\\(\W)/\1/g;
		print "$val\n" if ($val ne "");
	} else {
		print "$_";
	}
}' $MASTERDIR/$PROTO

exit 0
