#!/bin/sh
#
# Generate a file recording the configuration used to build SM
#
# Usage: config $(CC) $(CFLAGS)
#
if [ X"$*" = "" ]; then
	exit 0
fi
#
outfile="config$$" ;
trap "rm -f $outfile.c /tmp/$outfile" 0 3
#
if [ -f .config ]; then
	sed -n -e '2p' .config > /tmp/$outfile	# set_opts info
	sed -e '1,/^end_defines$/d' .config >> /tmp/$outfile
else
	touch .config
fi
sed -e '/^# *define/{
		h
		s/\/\*.*//
		p
		g
		s/^# *define *\(.*\)/define_\1/
	}' src/options.h > $outfile.c
date > .config
sed -n -e '1p' /tmp/$outfile >> .config
grep version_string AA_Version.h >> .config
echo CC: $* >> .config
sed -n -e '/^typedef .*REAL;/p' src/options.h >> .config
eval $* -E $outfile.c | \
	sed -n -e '/INC_OPTIONS_H/d' -e 's/define_\(.*\)/#define \1/p' \
	>> .config
echo "end_defines" >> .config
if test -r /etc/hosts; then
	hostname=""
	for f in /bin /usr/bin /usr/bsd; do
		if [ -f $f/hostname ]; then
			hostname=`$f/hostname`
			break
		fi
	done
	if [ "$hostname" = "" ]; then
		echo `whoami` "@ " >> /tmp/$outfile
	else
		echo `whoami` "@" \
		     `grep $hostname /etc/hosts | sed '/./q'` >> /tmp/$outfile
	fi
	sed -e '1d' /tmp/$outfile | uniq >> .config
fi
#
# And put it into a macro called config
#
sed	\
	-e '1,$s/\(......................................................................\).*/\1/'		\
	-e '1,$s/.*/		write $p "$!2" "&"/' \
	-e '1s/.*/config 02	# Print config info/' -e 1h \
	-e '1s/.*/		if(!$?1) { define 1 0 }/' -e 1H \
	-e '1s/.*/		if($?2){local define p {+}}else{ local define p " " define 2{standard} }/' -e 1H \
	-e '1s/.*/		write $p "$!2" $version/' -e 1H -e 1g \
	-e 's/write $p "$!2" "end_defines"/if(!$1) { return }/' \
	.config > macro/config; echo "		#" >> macro/config

exit 0
