#! /bin/sh
#
#	@(#)Mkfilter	3.21 (motonori/WIDE) 14 Jul 1998
#
#	Script for generation of $CLEANUP and $CONFIG
#

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

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

# extract keywords for configuration from prototype files
KEYWORDS=`cat $* | grep '^#%.*%$' | sed 's/^#%\(.*\)%$/\1/p' | sort -u`

### Generation of $CLEANUP ###

cat << EoF > $TOOLDIR/$CLEANUP
# definitions for final cleanup

# magic token for final cleanup
DFLT='#%DELETE%'

# set the magic token into undefined variables
EoF

for KW in $KEYWORDS
do
	echo ": \${$KW=\$DFLT}" >> $TOOLDIR/$CLEANUP
done


### Generatation of $CONFIG ###

if [ -f /bin/uname ]; then
	UNAME=/bin/uname
elif [ -f /sbin/uname ]; then
	UNAME=/sbin/uname
elif [ -f /usr/bin/uname ]; then
	UNAME=/usr/bin/uname
elif [ -f /usr/sbin/uname ]; then
	UNAME=/usr/sbin/uname
fi

if [ "$UNAME" ]; then
	case "`$UNAME -s` `$UNAME -r`" in
	UnixWare\ 5)	: ${CFSHELL='/usr/bin/ksh'} ;;
	ULTRIX\ 4.*)	: ${CFSHELL=''/bin/sh''} ;;
	esac
fi

: ${CFSHELL='/bin/sh'}

cat << EoF > $CONFIG
#! $CFSHELL

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

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

: \${MASTERDIR=$MASTERDIR}
: \${TOOLDIR=$TOOLDIR}
: \${MKINFO=$MKINFO}
: \${CFSELECT=$CFSELECT}
: \${CLEANUP=$CLEANUP}
: \${DEFAULT=$DEFAULT}

# preserve argument vector
_ARGV="\$@"

# make IFS contain SPC, TAB and EQUAL
OIFS="\$IFS"
IFS='=	 '
# evaluate definition files
while [ "\$1" ]
do
	if [ -f "\$1" ]
	then
		exec < "\$1"
		while read LINE
			do
			case "\$LINE" in
			DEFAULT_FILE=*)	# type of sendmail.cf
				set \$LINE
				DEFAULT_FILE="\$2" ;;
			*) ;;		# anything else should be ignored
			esac
		done
	else
		echo "\$1: no such file." 1>&2
	fi
	shift
done
# restore IFS
IFS="\$OIFS"
# evaluate default configuration
. \$MASTERDIR/\$DEFAULT

# recover argument vector
set "\$_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 again
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

EoF

if [ -f "$PERL" ]
then
# Yep, we can use the PERL

cat << EoF >> $CONFIG

# keyword substitution with PERL
cat << PERL_EOF | $PERL -e '
EoF

# operands for PERL
cat << 'EoF' >> $CONFIG

$proto = pop(@ARGV);
$key = "";
$line = "";
while (<STDIN>) {
	if (/^%PERL_VAR_SEPARATOR%/) {
		chop($line);
		$cf_env{$key} = $line;
		$key = "";
		$line = "";
		next;
	}
	if ($line eq "") {
		if(/^(\w+)=/) {
			$key = $1;
			$line = $'"'"';
		}
	} else {
		$line .= $_;
	}
}

open(PROTO, "<$proto") || die("open failed ($!): $proto");
while (<PROTO>) {
	# replace keywords with values defined in environment variables
	if (/^#%(.*)%/) {
		$val = $cf_env{$1};
		# for compatibility with SED version
		$val =~ s/\\(\W)/\1/g;
		print "$val\n" if ($val ne "");
	} else {
		print $_;
	}
}
close(PROTO);
' $MASTERDIR/$PROTO

EoF

for KW in $KEYWORDS
do
	echo "$KW=\$$KW" >> $CONFIG
	echo "%PERL_VAR_SEPARATOR%" >> $CONFIG
done
echo PERL_EOF >> $CONFIG

else
# Nope.

	echo perl not found. 1>&2
	exit 1

fi

cat << 'EoF' >> $CONFIG

rm -f /tmp/CF.$$*

exit 0
EoF

# make $CONFIG executable
chmod +x $CONFIG

exit 0
